Advertisement
DVS_studio

searchBar ex

May 22nd, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.82 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import "package:flutter/material.dart";
  4. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  5. import 'package:shop_spy/classes/shop.dart';
  6. import 'package:shop_spy/components/Drawer/mainDrawer.dart';
  7. import 'package:shop_spy/components/Search/searchBar.dart';
  8. import 'package:shop_spy/screens/Shops/shops_list.dart';
  9. import 'package:shop_spy/services/database.dart';
  10. import 'package:shop_spy/services/send_data.dart';
  11. import 'package:shop_spy/services/utils.dart';
  12.  
  13. class ScreenShops extends StatefulWidget {
  14.   const ScreenShops({Key key}) : super(key: key);
  15.  
  16.   @override
  17.   ScreenShopsState createState() => new ScreenShopsState();
  18. }
  19.  
  20. class ScreenShopsState extends State<ScreenShops> {
  21.   void showInSnackBar(String value) {
  22.     _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value)));
  23.   }
  24.  
  25.   final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  26.  
  27.   String searchPhrase;
  28.  
  29.   List<Shop> items = [];
  30.  
  31.   getShops() async {
  32.     items = await loadShops();
  33.     if (searchPhrase == null && items.length == 0) {
  34.       await fetchShops();
  35.     }
  36.     setState(() {});
  37.   }
  38.  
  39.   fetchShops() async {
  40.     try {
  41.       if (await Shop.fetch()) items = await loadShops();
  42.     } catch (e) {
  43.       Utils.showInSnackBar(_scaffoldKey, e.toString());
  44.     }
  45.     setState(() {});
  46.   }
  47.  
  48.   Future<List<Shop>> loadShops() async {
  49.     await new Future.delayed(new Duration(seconds: 1));
  50.     var db = new DataBase();
  51.     List<Shop> _items = await db.orderBy("name").get<Shop>("shops", callback: (Map item) => new Shop.fromJson(item));
  52.  
  53.     List<Shop> ret = [];
  54.     if (searchPhrase != null && searchPhrase.length > 0) {
  55.       for (var shop in _items) {
  56.         shop.order = Utils.compResult(shop.name, searchPhrase);
  57.         if (shop.order > 10) ret.add(shop);
  58.       }
  59.       ret.sort((a, b) => a.order > b.order ? -1 : a.order < b.order ? 1 : 0);
  60.     } else
  61.       ret = _items;
  62.     return ret;
  63.   }
  64.  
  65.   final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();
  66.  
  67.   SearchBar searchBar;
  68.  
  69.   AppBar buildAppBar(BuildContext context) {
  70.     return new AppBar(
  71.       title: new Text('Выберите магазин'),
  72.       actions: [searchBar.getSearchAction(context)],
  73.       backgroundColor: Colors.orange,
  74.     );
  75.   }
  76.  
  77.   @override
  78.   void initState() {
  79.     super.initState();
  80.     searchBar = new SearchBar(
  81.         inBar: true,
  82.         setState: setState,
  83.         onType: onSearchType,
  84.         onSubmitted: onSearchType,
  85.         onClear: onSearchClear,
  86.         buildDefaultAppBar: buildAppBar);
  87.     getShops();
  88.   }
  89.  
  90.   void onSearchType(String value) {
  91.     searchPhrase = value;
  92.   }
  93.  
  94.   void onSearchClear() {
  95.     searchPhrase = "";
  96.   }
  97.  
  98.   @override
  99.   Widget build(BuildContext context) {
  100.     return new Scaffold(
  101.       key: _scaffoldKey,
  102.       drawer: new DrawerMain(
  103.         sendWidget: new ListTile(
  104.           leading: const Icon(FontAwesomeIcons.telegramPlane),
  105.           title: new Text('Отправить изменения'),
  106.           onTap: () => openSendModal(true),
  107.         ),
  108.       ),
  109.       floatingActionButton: new FloatingActionButton(
  110.         backgroundColor: Colors.deepOrange,
  111.         child: new Icon(FontAwesomeIcons.telegramPlane, color: Colors.white),
  112.         onPressed: () => openSendModal(false),
  113.       ),
  114.       appBar: searchBar.build(context),
  115.       body: new RefreshIndicator(
  116.         key: _refreshIndicatorKey,
  117.         onRefresh: () => fetchShops(),
  118.         child: new ShopsList(shops: items),
  119.       ),
  120.     );
  121.   }
  122.  
  123.   openSendModal(bool pop) async {
  124.     var ret = await SendData.sendProducts(context);
  125.     if (ret != null && ret is String) {
  126.       if (pop) Navigator.pop(context);
  127.       _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(ret)));
  128.     }
  129.   }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement