You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.1KB

  1. import 'package:farm_tpf/presentation/screens/resources/bloc/supply_bloc.dart';
  2. import 'package:farm_tpf/utils/const_color.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. class WidgetSearch extends StatefulWidget {
  6. final String type;
  7. final int selectedId;
  8. WidgetSearch({this.type, this.selectedId});
  9. @override
  10. _WidgetSearchState createState() => _WidgetSearchState();
  11. }
  12. class _WidgetSearchState extends State<WidgetSearch> {
  13. BuildContext _blocContext;
  14. TextEditingController _searchController = TextEditingController();
  15. @override
  16. void initState() {
  17. super.initState();
  18. _searchController.addListener(() {
  19. final keyword = _searchController.text;
  20. if (keyword.isNotEmpty) {
  21. //search when text change
  22. }
  23. });
  24. }
  25. Widget getSearchBarUI() {
  26. _searchController.text = "";
  27. return Padding(
  28. padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 0),
  29. child: Row(
  30. children: <Widget>[
  31. Expanded(
  32. child: Padding(
  33. padding: const EdgeInsets.only(right: 8, top: 8, bottom: 0),
  34. child: Container(
  35. child: Padding(
  36. padding: const EdgeInsets.only(
  37. left: 16, right: 16, top: 4, bottom: 4),
  38. child: TextField(
  39. textInputAction: TextInputAction.done,
  40. controller: _searchController,
  41. onChanged: (String txt) {},
  42. cursorColor: AppColors.GRAY1,
  43. decoration: InputDecoration(
  44. suffixIcon: IconButton(
  45. icon: Icon(
  46. Icons.search,
  47. size: 30,
  48. ),
  49. onPressed: () {
  50. FocusScope.of(context).requestFocus(FocusNode());
  51. BlocProvider.of<SupplyBloc>(_blocContext).add(
  52. OnSearch(
  53. searchString: _searchController.text,
  54. type: widget.type,
  55. selectedId: widget.selectedId));
  56. }),
  57. hintText: 'Tìm kiếm ...',
  58. hintStyle: TextStyle(color: Colors.grey[500])),
  59. onSubmitted: (value) {
  60. FocusScope.of(context).requestFocus(FocusNode());
  61. BlocProvider.of<SupplyBloc>(_blocContext).add(OnSearch(
  62. searchString: value,
  63. type: widget.type,
  64. selectedId: widget.selectedId));
  65. },
  66. ),
  67. ),
  68. ),
  69. ),
  70. ),
  71. ],
  72. ),
  73. );
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. _blocContext = context;
  78. return Container(child: getSearchBarUI());
  79. }
  80. @override
  81. void dispose() {
  82. _searchController.dispose();
  83. super.dispose();
  84. }
  85. }