Advertisement
Guest User

Untitled

a guest
Aug 20th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.73 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:random_color/random_color.dart';
  3. import 'package:we_help/models/tag.dart';
  4.  
  5. class UserPreview extends StatelessWidget {
  6.   /// Creates an article preview widget.
  7.   /// Used in the home screen and search screen.
  8.   final String name;
  9.   final String surname;
  10.   final double rating;
  11.   final String photo;
  12.   final String description;
  13.   final List<Tag> tags;
  14.   final Function press;
  15.   final String contentKey;
  16.  
  17.   const UserPreview({
  18.     Key key,
  19.     this.name,
  20.     this.surname,
  21.     this.rating,
  22.     this.photo,
  23.     this.description,
  24.     this.tags,
  25.     this.press,
  26.     this.contentKey,
  27.   }) : super(key: key);
  28.  
  29.   @override
  30.   Widget build(BuildContext context) {
  31.     Size size = MediaQuery.of(context).size;
  32.  
  33.     Container container = Container(
  34.       // The wrapper for the entire widget.
  35.       padding: EdgeInsets.only(
  36.         left: size.width * 0.05,
  37.         right: size.width * 0.05,
  38.       ),
  39.       margin: EdgeInsets.only(
  40.         bottom: size.height * 0.03,
  41.       ),
  42.       width: size.width * 0.90,
  43.       height: size.height * 0.13,
  44.       decoration: BoxDecoration(
  45.         border: Border.all(
  46.           width: 2,
  47.           color: Color(0xff0073FF),
  48.         ),
  49.         borderRadius: BorderRadius.all(
  50.           Radius.circular(20.0),
  51.         ),
  52.       ),
  53.       child: Row(
  54.         // Main column
  55.         mainAxisAlignment: MainAxisAlignment.start,
  56.         crossAxisAlignment: CrossAxisAlignment.start,
  57.         children: <Widget>[
  58.           _photoAndRating(photo, rating, size.height),
  59.           SizedBox(
  60.             width: size.width * 0.04,
  61.           ),
  62.           _userInfo(size),
  63.         ],
  64.       ),
  65.     );
  66.     return container;
  67.   }
  68.  
  69.   Widget _userInfo(Size size) {
  70.     return Column(
  71.       mainAxisAlignment: MainAxisAlignment.center,
  72.       crossAxisAlignment: CrossAxisAlignment.start,
  73.       children: [
  74.         _userName(name, surname),
  75.         SizedBox(
  76.           height: size.height * 0.01,
  77.         ),
  78.         _descriptionText(description, size.width),
  79. //        _tagsRow(size, tags)
  80.       ],
  81.     );
  82.   }
  83.  
  84.   Widget _userName(String name, String surname) {
  85.     return Text(
  86.       "$surname $name",
  87.       style: TextStyle(fontSize: 18, color: Color(0xff0D0D0D)),
  88.       overflow: TextOverflow.fade,
  89.     );
  90.   }
  91.  
  92.   Widget _descriptionText(String description, double screenWidth) {
  93.     return ConstrainedBox(
  94.         constraints: BoxConstraints(
  95.             minWidth: screenWidth * 0.4, maxWidth: screenWidth * 0.6),
  96.         child: Text(
  97.           description,
  98.           overflow: TextOverflow.ellipsis,
  99.           maxLines: 2,
  100.           style: TextStyle(
  101.             fontSize: 14,
  102.             color: Color(0xff0D0D0D),
  103.           ),
  104.         ));
  105.   }
  106.  
  107.   Widget _tagsRow(Size size, List<Tag> tagList) {
  108.     return ConstrainedBox(
  109.       constraints: BoxConstraints(
  110.         maxHeight: size.height * 0.03,
  111.         minHeight: size.height * 0.02,
  112.       ),
  113.       child: ListView(
  114.         scrollDirection: Axis.horizontal,
  115.         children: buildTagWidgets(size.width, tagList),
  116.       ),
  117.     );
  118.   }
  119.  
  120.   Widget _photoAndRating(String photo, double rating, double screenHeight) {
  121.     return Column(
  122.       mainAxisAlignment: MainAxisAlignment.center,
  123.       crossAxisAlignment: CrossAxisAlignment.start,
  124.       children: [
  125.         CircleAvatar(
  126.           backgroundColor: RandomColor().randomColor(
  127.             colorBrightness: ColorBrightness.light,
  128.           ),
  129.           foregroundColor: Colors.black,
  130.           radius: screenHeight * 0.028,
  131.         ),
  132.         Row(
  133.           children: [
  134.             Icon(
  135.               Icons.star,
  136.               color: Colors.amber,
  137.               size: 20,
  138.             ),
  139.             SizedBox(
  140.               height: screenHeight * 0.04,
  141.             ),
  142.             Text(
  143.               rating.toString(),
  144.               style: TextStyle(
  145.                 color: Colors.black,
  146.                 fontSize: 12,
  147.               ),
  148.             )
  149.           ],
  150.         )
  151.       ],
  152.     );
  153.   }
  154.  
  155.   static List<Widget> buildTagWidgets(double screenWidth, List<Tag> tagList) {
  156.     /// Returns a list of tag widgets with text, background and padding.
  157.     return tagList
  158.         .map(
  159.           (tag) => Container(
  160.             padding: EdgeInsets.symmetric(horizontal: screenWidth * 0.03),
  161.             margin: EdgeInsets.only(right: screenWidth * 0.02),
  162.             decoration: BoxDecoration(
  163.               color: Color(0xffD3D6DA),
  164.               borderRadius: BorderRadius.all(
  165.                 Radius.circular(6.0),
  166.               ),
  167.             ),
  168.             child: Text(
  169.               tag.name,
  170.               style: TextStyle(color: Colors.black),
  171.             ),
  172.           ),
  173.         )
  174.         .toList();
  175.   }
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement