| } catch (_) { | } catch (_) { | ||||
| yield SupplyFailure(); | yield SupplyFailure(); | ||||
| } | } | ||||
| } else if (event is OnSearch) { | |||||
| try { | |||||
| final response = await repository.getSupplies(event.type); | |||||
| bool query(Supply supply) => | |||||
| event.searchString.isEmpty || | |||||
| supply.name | |||||
| .toLowerCase() | |||||
| .contains(event.searchString.toLowerCase()); | |||||
| final result = response.where(query).toList(); | |||||
| List<Supply> supplies = result.map((supply) { | |||||
| if (supply.id == event.selectedId) { | |||||
| supply.isSelected = true; | |||||
| } | |||||
| return supply; | |||||
| }).toList(); | |||||
| yield SupplySuccess(items: supplies); | |||||
| } catch (_) { | |||||
| yield SupplyFailure(); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| final String type; | final String type; | ||||
| OnRefresh({this.selectedId, @required this.type}); | OnRefresh({this.selectedId, @required this.type}); | ||||
| } | } | ||||
| class OnSearch extends SupplyEvent { | |||||
| final String searchString; | |||||
| final int selectedId; | |||||
| final String type; | |||||
| OnSearch({this.searchString, this.selectedId, this.type}); | |||||
| } |
| import 'package:farm_tpf/data/repository/repository.dart'; | import 'package:farm_tpf/data/repository/repository.dart'; | ||||
| import 'package:farm_tpf/models/Supply.dart'; | import 'package:farm_tpf/models/Supply.dart'; | ||||
| import 'package:farm_tpf/models/index.dart'; | |||||
| import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart'; | import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart'; | ||||
| import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart'; | import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart'; | ||||
| import 'package:farm_tpf/presentation/screens/resources/bloc/supply_bloc.dart'; | import 'package:farm_tpf/presentation/screens/resources/bloc/supply_bloc.dart'; | ||||
| import 'package:farm_tpf/presentation/screens/resources/widget_search.dart'; | |||||
| import 'package:farm_tpf/utils/const_string.dart'; | import 'package:farm_tpf/utils/const_string.dart'; | ||||
| import 'package:flutter/material.dart'; | import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | import 'package:flutter_bloc/flutter_bloc.dart'; | ||||
| return Scaffold( | return Scaffold( | ||||
| key: _scaffoldKey, | key: _scaffoldKey, | ||||
| appBar: AppBar(title: Text("Chọn $titleName")), | appBar: AppBar(title: Text("Chọn $titleName")), | ||||
| body: InfinityView( | |||||
| selectedId: selectedId, | |||||
| type: type, | |||||
| body: Column( | |||||
| children: <Widget>[ | |||||
| WidgetSearch( | |||||
| type: type, | |||||
| selectedId: selectedId, | |||||
| ), | |||||
| Expanded( | |||||
| child: InfinityView( | |||||
| selectedId: selectedId, | |||||
| type: type, | |||||
| )) | |||||
| ], | |||||
| )); | )); | ||||
| } | } | ||||
| } | } |
| import 'package:farm_tpf/models/Supply.dart'; | |||||
| import 'package:farm_tpf/presentation/screens/resources/bloc/supply_bloc.dart'; | |||||
| import 'package:farm_tpf/utils/const_color.dart'; | |||||
| import 'package:flutter/material.dart'; | |||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | |||||
| class WidgetSearch extends StatefulWidget { | |||||
| final String type; | |||||
| final int selectedId; | |||||
| WidgetSearch({this.type, this.selectedId}); | |||||
| @override | |||||
| _WidgetSearchState createState() => _WidgetSearchState(); | |||||
| } | |||||
| class _WidgetSearchState extends State<WidgetSearch> { | |||||
| BuildContext _blocContext; | |||||
| TextEditingController _searchController = TextEditingController(); | |||||
| @override | |||||
| void initState() { | |||||
| super.initState(); | |||||
| _searchController.addListener(() { | |||||
| final keyword = _searchController.text; | |||||
| if (keyword.isNotEmpty) { | |||||
| //search when text change | |||||
| } | |||||
| }); | |||||
| } | |||||
| Widget getSearchBarUI() { | |||||
| _searchController.text = ""; | |||||
| return Padding( | |||||
| padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 0), | |||||
| child: Row( | |||||
| children: <Widget>[ | |||||
| Expanded( | |||||
| child: Padding( | |||||
| padding: const EdgeInsets.only(right: 8, top: 8, bottom: 0), | |||||
| child: Container( | |||||
| decoration: BoxDecoration( | |||||
| color: Colors.white, | |||||
| borderRadius: const BorderRadius.all( | |||||
| Radius.circular(38.0), | |||||
| ), | |||||
| boxShadow: <BoxShadow>[ | |||||
| BoxShadow( | |||||
| color: Colors.grey.withOpacity(0.2), | |||||
| offset: const Offset(0, 2), | |||||
| blurRadius: 8.0), | |||||
| ], | |||||
| ), | |||||
| child: Padding( | |||||
| padding: const EdgeInsets.only( | |||||
| left: 16, right: 16, top: 4, bottom: 4), | |||||
| child: TextField( | |||||
| textInputAction: TextInputAction.done, | |||||
| controller: _searchController, | |||||
| onChanged: (String txt) {}, | |||||
| cursorColor: COLOR_CONST.GRAY1, | |||||
| decoration: InputDecoration( | |||||
| border: InputBorder.none, | |||||
| hintText: 'Tìm kiếm ...', | |||||
| ), | |||||
| onSubmitted: (value) { | |||||
| FocusScope.of(context).requestFocus(FocusNode()); | |||||
| BlocProvider.of<SupplyBloc>(_blocContext).add(OnSearch( | |||||
| searchString: value, | |||||
| type: widget.type, | |||||
| selectedId: widget.selectedId)); | |||||
| }, | |||||
| ), | |||||
| ), | |||||
| ), | |||||
| ), | |||||
| ), | |||||
| ], | |||||
| ), | |||||
| ); | |||||
| } | |||||
| @override | |||||
| Widget build(BuildContext context) { | |||||
| _blocContext = context; | |||||
| return Container(child: getSearchBarUI()); | |||||
| } | |||||
| @override | |||||
| void dispose() { | |||||
| _searchController.dispose(); | |||||
| super.dispose(); | |||||
| } | |||||
| } |