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.

98 lines
3.4KB

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