Advertisement
Guest User

FilterSearch

a guest
Aug 20th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.16 KB | None | 0 0
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/painting.dart';
  4. import 'package:we_help/components/question_preview.dart';
  5. import 'package:we_help/components/search_field.dart';
  6. import 'package:we_help/components/user_preview.dart';
  7. import 'package:we_help/models/public_question.dart';
  8. import 'package:we_help/models/tag.dart';
  9. import 'package:we_help/models/user.dart';
  10. import 'package:we_help/services/rest_api.dart';
  11. import 'package:we_help/services/stabilizer.dart';
  12.  
  13. class SearchScreen extends StatefulWidget {
  14.   @override
  15.   State<StatefulWidget> createState() {
  16.     return new SearchScreenState();
  17.   }
  18. }
  19.  
  20. class SearchScreenState extends State<SearchScreen> {
  21.   static bool _isQuestions = true;
  22.   static bool _isPeople = false;
  23.   static bool _isArticles = false;
  24.   static String _searchRequest;
  25.   final _stabilizer = Stabilizer(milliseconds: 500);
  26.   static List<Widget> _childWidget;
  27.  
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     Size size = MediaQuery.of(context).size;
  31.     return FutureBuilder<List<dynamic>>(
  32.       future: _searchByFilter(_searchRequest),
  33.       builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
  34.         if (snapshot.hasData) {
  35.           _childWidget = _parseGetRequest(snapshot.data);
  36.         } else {
  37.           _childWidget = [];
  38.         }
  39.         return Scaffold(
  40.           body: Center(
  41.             child: ListView(
  42.               padding: EdgeInsets.only(top: size.height * 0.03),
  43.               children: <Widget>[
  44.                 Column(
  45.                   crossAxisAlignment: CrossAxisAlignment.center,
  46.                   mainAxisAlignment: MainAxisAlignment.center,
  47.                   children: <Widget>[
  48.                     _searchInputField(),
  49.                     _filterRow(),
  50.                     SizedBox(
  51.                       height: size.height * 0.05,
  52.                     ),
  53.                     _searchResults(_childWidget),
  54.                   ],
  55.                 ),
  56.               ],
  57.             ),
  58.           ),
  59.         );
  60.       },
  61.     );
  62.   }
  63.  
  64.   Widget _searchInputField() {
  65.     return SearchInputField(
  66.       hintText: "Что вы хотите найти?",
  67.       width: 0.85,
  68.       onChanged: (value) {
  69.         _searchRequest = value;
  70.         _stabilizer.run(
  71.           () {
  72.             _searchByFilter(_searchRequest);
  73.           },
  74.         );
  75.       },
  76.     );
  77.   }
  78.  
  79.   Widget _filterRow() {
  80.     Color activeColor = Color(0xff0073FF);
  81.     Color inactiveTextColor = Color(0xff60626D);
  82.     return Row(
  83.       crossAxisAlignment: CrossAxisAlignment.center,
  84.       mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  85.       children: <Widget>[
  86.         FlatButton(
  87.           color: _isQuestions ? activeColor : Colors.transparent,
  88.           textColor: _isQuestions ? Colors.white : inactiveTextColor,
  89.           shape: RoundedRectangleBorder(
  90.             borderRadius: BorderRadius.circular(100.0),
  91.             side: BorderSide(color: Colors.blueAccent, width: 2),
  92.           ),
  93.           child: Text("Вопросы"),
  94.           onPressed: () => setState(
  95.             () {
  96.               _clearFilter();
  97.               _isQuestions = !_isQuestions;
  98.             },
  99.           ),
  100.         ),
  101.         FlatButton(
  102.           color: _isPeople ? activeColor : Colors.transparent,
  103.           textColor: _isPeople ? Colors.white : inactiveTextColor,
  104.           shape: RoundedRectangleBorder(
  105.             borderRadius: BorderRadius.circular(100.0),
  106.             side: BorderSide(color: Colors.blueAccent, width: 2),
  107.           ),
  108.           child: Text("Люди"),
  109.           onPressed: () => setState(
  110.             () {
  111.               _clearFilter();
  112.               _isPeople = !_isPeople;
  113.             },
  114.           ),
  115.         ),
  116.         FlatButton(
  117.           color: _isArticles ? activeColor : Colors.transparent,
  118.           textColor: _isArticles ? Colors.white : inactiveTextColor,
  119.           shape: RoundedRectangleBorder(
  120.             borderRadius: BorderRadius.circular(100.0),
  121.             side: BorderSide(color: Colors.blueAccent, width: 2),
  122.           ),
  123.           child: Text("Посты"),
  124.           onPressed: () => setState(
  125.             () {
  126.               _clearFilter();
  127.               _isArticles = !_isArticles;
  128.             },
  129.           ),
  130.         ),
  131.       ],
  132.     );
  133.   }
  134.  
  135.   Widget _searchResults(List<Widget> data) {
  136.     return Column(
  137.       crossAxisAlignment: CrossAxisAlignment.start,
  138.       mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  139.       children: data,
  140.     );
  141.   }
  142.  
  143.   Future<List<dynamic>> _searchByFilter(String searchRequest) async {
  144.     if (_isPeople) {
  145.       _stabilizer.run(() async {
  146.         return await RestApi.searchUsers(searchRequest);
  147.       });
  148.     } else if (_isQuestions) {
  149.       return await RestApi.searchQuestions(searchRequest);
  150.     } else
  151.       return await RestApi.searchArticle(searchRequest);
  152.   }
  153.  
  154.   List<Widget> _parseGetRequest(List<dynamic> snapshotData) {
  155.     if (_isPeople)
  156.       return userToPreview(snapshotData);
  157.     else if (_isQuestions) return questionToPreview(snapshotData);
  158.   }
  159.  
  160.   void _clearFilter() {
  161.     _childWidget = [];
  162.     _isQuestions = false;
  163.     _isArticles = false;
  164.     _isPeople = false;
  165.   }
  166.  
  167.   static List<QuestionPreview> questionToPreview(
  168.       List<PublicQuestion> questions) {
  169.     List<QuestionPreview> previews = [];
  170.     for (final question in questions) {
  171.       print(question.content);
  172.       previews.add(
  173.         QuestionPreview(
  174.           authorName: question.author.name,
  175.           authorSurname: question.author.surname,
  176.           title: question.title,
  177.           description: question.content,
  178.           tags: question.tags,
  179.           answersCount: 189,
  180.         ),
  181.       );
  182.     }
  183.     return previews;
  184.   }
  185.  
  186.   static List<UserPreview> userToPreview(List<User> users) {
  187.     List<UserPreview> previews = [];
  188.     for (final user in users) {
  189.       print(user);
  190.       previews.add(
  191.         UserPreview(
  192.           name: user.name,
  193.           surname: user.surname,
  194.           rating: user.rating,
  195.           photo: "google",
  196.           description: user.aboutMe,
  197.           tags: user.tags,
  198.         ),
  199.       );
  200.     }
  201.     return previews;
  202.   }
  203. }
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement