Line data Source code
1 : import 'package:flutter/material.dart'; 2 : import 'package:widgetbook/src/constants/radii.dart'; 3 : import 'package:widgetbook/src/providers/organizer_provider.dart'; 4 : import 'package:widgetbook/src/utils/utils.dart'; 5 : 6 : class SearchBar extends StatefulWidget { 7 1 : const SearchBar({ 8 : Key? key, 9 : required this.theme, 10 : this.organizerProvider, 11 1 : }) : super(key: key); 12 : 13 : final ThemeMode theme; 14 : final OrganizerProvider? organizerProvider; 15 : 16 1 : @override 17 1 : _SearchBarState createState() => _SearchBarState(); 18 : } 19 : 20 : class _SearchBarState extends State<SearchBar> { 21 : TextEditingController controller = TextEditingController(); 22 : 23 1 : OrganizerProvider _getProvider() { 24 2 : return widget.organizerProvider ?? OrganizerProvider.of(context)!; 25 : } 26 : 27 1 : @override 28 : Widget build(BuildContext context) { 29 3 : final fillColor = widget.theme == ThemeMode.light 30 : ? Styles.lightSurface 31 : : Styles.darkSurface; 32 3 : final onFillColor = widget.theme == ThemeMode.light 33 : ? Styles.onLightSurface 34 : : Styles.onDarkSurface; 35 : 36 : const border = OutlineInputBorder( 37 : borderSide: BorderSide( 38 : color: Colors.transparent, 39 : ), 40 : borderRadius: Radii.defaultRadius, 41 : ); 42 : 43 1 : return TextField( 44 2 : key: Key('$SearchBar.$TextField'), 45 1 : controller: controller, 46 : cursorWidth: 3, 47 : cursorColor: onFillColor, 48 1 : decoration: InputDecoration( 49 : prefixIcon: const Icon(Icons.search), 50 : hintText: 'search', 51 : suffixIcon: 52 4 : controller.text.isNotEmpty ? _buildCancelSearchButton() : null, 53 : filled: true, 54 : fillColor: fillColor, 55 : border: border, 56 : enabledBorder: border, 57 : focusedBorder: border, 58 : ), 59 1 : onChanged: (value) { 60 2 : setState(() {}); 61 2 : _getProvider().filter(value); 62 : }, 63 : ); 64 : } 65 : 66 1 : Widget _buildCancelSearchButton() { 67 1 : return IconButton( 68 2 : key: Key('$SearchBar.CancelSearchButton'), 69 : icon: const Icon(Icons.close), 70 : splashRadius: 20, 71 1 : onPressed: () { 72 1 : setState( 73 1 : () { 74 2 : controller = TextEditingController(); 75 : }, 76 : ); 77 : 78 2 : _getProvider().resetFilter(); 79 : }, 80 : ); 81 : } 82 : }