dev
dart 126 lines 3.71 KB
Raw
1 import 'package:cake_wallet/entities/country.dart';
2 import 'package:cake_wallet/src/widgets/search_bar_widget.dart';
3 import 'package:cake_wallet/utils/debounce.dart';
4 import 'package:flutter/material.dart';
5
6 class CakePaySearchBar extends StatefulWidget {
7 const CakePaySearchBar(
8 {required this.initialQuery,
9 required this.onSearch,
10 required this.onFilter,
11 this.onCountryPick,
12 this.controller,
13 this.selectedCountry});
14
15 final String initialQuery;
16 final ValueChanged<String> onSearch;
17 final VoidCallback onFilter;
18 final VoidCallback? onCountryPick;
19 final Country? selectedCountry;
20 final TextEditingController? controller;
21
22 @override
23 State<CakePaySearchBar> createState() => _CakePaySearchBarState();
24 }
25
26 class _CakePaySearchBarState extends State<CakePaySearchBar> {
27 late final TextEditingController _searchController =
28 widget.controller ?? TextEditingController(text: widget.initialQuery);
29 final _searchFocusNode = FocusNode();
30 final _debounce = Debounce(const Duration(milliseconds: 500));
31
32 @override
33 void initState() {
34 super.initState();
35 _searchController
36 .addListener(() => _debounce.run(() => widget.onSearch(_searchController.text)));
37 }
38
39 @override
40 void dispose() {
41 _searchController.dispose();
42 _searchFocusNode.dispose();
43 super.dispose();
44 }
45
46 @override
47 Widget build(BuildContext context) {
48 return SizedBox(
49 height: 32,
50 child: Row(
51 children: [
52 Expanded(child: SearchBarWidget(searchController: _searchController)),
53 const SizedBox(width: 5),
54 _CakePayFilterButton(onFilter: widget.onFilter),
55 if (widget.selectedCountry != null)
56 _CountryPickerWidget(
57 onTap: widget.onCountryPick,
58 selectedCountry: widget.selectedCountry!,
59 ),
60 ],
61 ),
62 );
63 }
64 }
65
66 class _CakePayFilterButton extends StatelessWidget {
67 const _CakePayFilterButton({required this.onFilter});
68
69 final VoidCallback onFilter;
70
71 @override
72 Widget build(BuildContext context) {
73 return GestureDetector(
74 onTap: onFilter,
75 child: Container(
76 width: 32,
77 padding: const EdgeInsets.symmetric(vertical: 7),
78 decoration: BoxDecoration(
79 color: Theme.of(context).colorScheme.surfaceContainer,
80 borderRadius: BorderRadius.circular(10)),
81 child: Image.asset('assets/images/filter_icon.png',
82 color: Theme.of(context).colorScheme.onSurface)),
83 );
84 }
85 }
86
87 class _CountryPickerWidget extends StatelessWidget {
88 const _CountryPickerWidget({required this.selectedCountry, this.onTap});
89
90 final Country selectedCountry;
91 final VoidCallback? onTap;
92
93 @override
94 Widget build(BuildContext context) {
95 return Padding(
96 padding: const EdgeInsets.only(left: 5),
97 child: GestureDetector(
98 onTap: onTap,
99 child: Container(
100 height: 32,
101 padding: const EdgeInsets.symmetric(horizontal: 8),
102 decoration: BoxDecoration(
103 color: Theme.of(context).colorScheme.surfaceContainer,
104 borderRadius: BorderRadius.circular(10)),
105 child: Row(
106 children: [
107 Image.asset(selectedCountry.iconPath,
108 width: 24,
109 height: 24,
110 errorBuilder: (_, __, ___) => const SizedBox(width: 24, height: 24)),
111 const SizedBox(width: 6),
112 Text(
113 selectedCountry.countryCode,
114 style: TextStyle(
115 fontSize: 16,
116 fontWeight: FontWeight.w700,
117 color: Theme.of(context).colorScheme.onSurface,
118 ),
119 ),
120 ],
121 ),
122 ),
123 ),
124 );
125 }
126 }