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.

96 lines
3.2KB

  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 urlSupply;
  7. final int? selectedId;
  8. WidgetSearch({required this.urlSupply, 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(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: Icon(
  45. Icons.search,
  46. size: 30,
  47. ),
  48. onPressed: () {
  49. FocusScope.of(context).requestFocus(FocusNode());
  50. BlocProvider.of<SupplyBloc>(_blocContext!).add(
  51. OnSearch(
  52. searchString: _searchController.text,
  53. urlSupply: widget.urlSupply,
  54. selectedId: widget.selectedId ?? -1,
  55. ),
  56. );
  57. }),
  58. hintText: 'Tìm kiếm ...',
  59. hintStyle: TextStyle(color: Colors.grey[500])),
  60. onSubmitted: (value) {
  61. FocusScope.of(context).requestFocus(FocusNode());
  62. BlocProvider.of<SupplyBloc>(_blocContext!).add(
  63. OnSearch(
  64. searchString: value,
  65. urlSupply: widget.urlSupply,
  66. selectedId: widget.selectedId ?? -1,
  67. ),
  68. );
  69. },
  70. ),
  71. ),
  72. ),
  73. ),
  74. ),
  75. ],
  76. ),
  77. );
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. _blocContext = context;
  82. return Container(child: getSearchBarUI());
  83. }
  84. @override
  85. void dispose() {
  86. _searchController.dispose();
  87. super.dispose();
  88. }
  89. }