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.

85 lines
2.9KB

  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({required this.type, required this.selectedId});
  9. @override
  10. _WidgetSearchState createState() => _WidgetSearchState();
  11. }
  12. class _WidgetSearchState extends State<WidgetSearch> {
  13. late 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(left: 16, right: 16, top: 4, bottom: 4),
  37. child: TextField(
  38. textInputAction: TextInputAction.done,
  39. controller: _searchController,
  40. onChanged: (String txt) {},
  41. cursorColor: AppColors.GRAY1,
  42. decoration: InputDecoration(
  43. suffixIcon: IconButton(
  44. icon: const Icon(
  45. Icons.search,
  46. size: 30,
  47. ),
  48. onPressed: () {
  49. FocusScope.of(context).requestFocus(FocusNode());
  50. BlocProvider.of<SupplyBloc>(_blocContext)
  51. .add(OnSearch(searchString: _searchController.text, type: widget.type, selectedId: widget.selectedId));
  52. }),
  53. hintText: 'Tìm kiếm ...',
  54. hintStyle: TextStyle(color: Colors.grey[500])),
  55. onSubmitted: (value) {
  56. FocusScope.of(context).requestFocus(FocusNode());
  57. BlocProvider.of<SupplyBloc>(_blocContext).add(OnSearch(searchString: value, type: widget.type, selectedId: widget.selectedId));
  58. },
  59. ),
  60. ),
  61. ),
  62. ),
  63. ),
  64. ],
  65. ),
  66. );
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. _blocContext = context;
  71. return Container(child: getSearchBarUI());
  72. }
  73. @override
  74. void dispose() {
  75. _searchController.dispose();
  76. super.dispose();
  77. }
  78. }