| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:flutter/material.dart'; |
| 6 | |
| 7 | /// Flutter code sample for [SearchBar]. |
| 8 | |
| 9 | void main() => runApp(const SearchBarApp()); |
| 10 | |
| 11 | class SearchBarApp extends StatefulWidget { |
| 12 | const SearchBarApp({super.key}); |
| 13 | |
| 14 | @override |
| 15 | State<SearchBarApp> createState() => _SearchBarAppState(); |
| 16 | } |
| 17 | |
| 18 | class _SearchBarAppState extends State<SearchBarApp> { |
| 19 | bool isDark = false; |
| 20 | |
| 21 | @override |
| 22 | Widget build(BuildContext context) { |
| 23 | final ThemeData themeData = ThemeData(brightness: isDark ? Brightness.dark : Brightness.light); |
| 24 | |
| 25 | return MaterialApp( |
| 26 | theme: themeData, |
| 27 | home: Scaffold( |
| 28 | appBar: AppBar(title: const Text('Search Bar Sample' )), |
| 29 | body: Padding( |
| 30 | padding: const EdgeInsets.all(8.0), |
| 31 | child: SearchAnchor( |
| 32 | builder: (BuildContext context, SearchController controller) { |
| 33 | return SearchBar( |
| 34 | controller: controller, |
| 35 | padding: const WidgetStatePropertyAll<EdgeInsets>( |
| 36 | EdgeInsets.symmetric(horizontal: 16.0), |
| 37 | ), |
| 38 | onTap: () { |
| 39 | controller.openView(); |
| 40 | }, |
| 41 | onChanged: (_) { |
| 42 | controller.openView(); |
| 43 | }, |
| 44 | leading: const Icon(Icons.search), |
| 45 | trailing: <Widget>[ |
| 46 | Tooltip( |
| 47 | message: 'Change brightness mode' , |
| 48 | child: IconButton( |
| 49 | isSelected: isDark, |
| 50 | onPressed: () { |
| 51 | setState(() { |
| 52 | isDark = !isDark; |
| 53 | }); |
| 54 | }, |
| 55 | icon: const Icon(Icons.wb_sunny_outlined), |
| 56 | selectedIcon: const Icon(Icons.brightness_2_outlined), |
| 57 | ), |
| 58 | ), |
| 59 | ], |
| 60 | ); |
| 61 | }, |
| 62 | suggestionsBuilder: (BuildContext context, SearchController controller) { |
| 63 | return List<ListTile>.generate(5, (int index) { |
| 64 | final String item = 'item $index' ; |
| 65 | return ListTile( |
| 66 | title: Text(item), |
| 67 | onTap: () { |
| 68 | setState(() { |
| 69 | controller.closeView(item); |
| 70 | }); |
| 71 | }, |
| 72 | ); |
| 73 | }); |
| 74 | }, |
| 75 | ), |
| 76 | ), |
| 77 | ), |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |