Create SearchAnchor and SearchViewTheme Widget (#123256)
This commit is contained in:
124
examples/api/lib/material/search_anchor/search_anchor.0.dart
Normal file
124
examples/api/lib/material/search_anchor/search_anchor.0.dart
Normal file
@@ -0,0 +1,124 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for [SearchAnchor.bar].
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const SearchBarApp());
|
||||
|
||||
class SearchBarApp extends StatefulWidget {
|
||||
const SearchBarApp({super.key});
|
||||
|
||||
@override
|
||||
State<SearchBarApp> createState() => _SearchBarAppState();
|
||||
}
|
||||
|
||||
class _SearchBarAppState extends State<SearchBarApp> {
|
||||
Color? selectedColorSeed;
|
||||
List<ColorLabel> searchHistory = <ColorLabel>[];
|
||||
|
||||
Iterable<Widget> getHistoryList(SearchController controller) {
|
||||
return searchHistory.map((ColorLabel color) => ListTile(
|
||||
leading: const Icon(Icons.history),
|
||||
title: Text(color.label),
|
||||
trailing: IconButton(icon: const Icon(Icons.call_missed), onPressed: () {
|
||||
controller.text = color.label;
|
||||
controller.selection = TextSelection.collapsed(offset: controller.text.length);
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
Iterable<Widget> getSuggestions(SearchController controller) {
|
||||
final String input = controller.value.text;
|
||||
return ColorLabel.values.where((ColorLabel color) => color.label.contains(input))
|
||||
.map((ColorLabel filteredColor) =>
|
||||
ListTile(
|
||||
leading: CircleAvatar(backgroundColor: filteredColor.color),
|
||||
title: Text(filteredColor.label),
|
||||
trailing: IconButton(icon: const Icon(Icons.call_missed), onPressed: () {
|
||||
controller.text = filteredColor.label;
|
||||
controller.selection = TextSelection.collapsed(offset: controller.text.length);
|
||||
}),
|
||||
onTap: () {
|
||||
controller.closeView(filteredColor.label);
|
||||
handleSelection(filteredColor);
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
void handleSelection(ColorLabel selectedColor) {
|
||||
setState(() {
|
||||
selectedColorSeed = selectedColor.color;
|
||||
if (searchHistory.length >= 5) {
|
||||
searchHistory.removeLast();
|
||||
}
|
||||
searchHistory.insert(0, selectedColor);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final ThemeData themeData = ThemeData(useMaterial3: true, colorSchemeSeed: selectedColorSeed);
|
||||
final ColorScheme colors = themeData.colorScheme;
|
||||
|
||||
return MaterialApp(
|
||||
theme: themeData,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Search Bar Sample')),
|
||||
body: Align(
|
||||
alignment: Alignment.topCenter,
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
SearchAnchor.bar(
|
||||
barHintText: 'Search colors',
|
||||
suggestionsBuilder: (BuildContext context, SearchController controller) {
|
||||
if (controller.text.isEmpty) {
|
||||
if (searchHistory.isNotEmpty) {
|
||||
return getHistoryList(controller);
|
||||
}
|
||||
return <Widget>[ Center(child: Text('No search history.', style: TextStyle(color: colors.outline))) ];
|
||||
}
|
||||
return getSuggestions(controller);
|
||||
},
|
||||
),
|
||||
cardSize,
|
||||
Card(color: colors.primary, child: cardSize),
|
||||
Card(color: colors.onPrimary, child: cardSize),
|
||||
Card(color: colors.primaryContainer, child: cardSize),
|
||||
Card(color: colors.onPrimaryContainer, child: cardSize),
|
||||
Card(color: colors.secondary, child: cardSize),
|
||||
Card(color: colors.onSecondary, child: cardSize),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
SizedBox cardSize = const SizedBox(width: 80, height: 30,);
|
||||
|
||||
enum ColorLabel {
|
||||
red('red', Colors.red),
|
||||
orange('orange', Colors.orange),
|
||||
yellow('yellow', Colors.yellow),
|
||||
green('green', Colors.green),
|
||||
blue('blue', Colors.blue),
|
||||
indigo('indigo', Colors.indigo),
|
||||
violet('violet', Color(0xFF8F00FF)),
|
||||
purple('purple', Colors.purple),
|
||||
pink('pink', Colors.pink),
|
||||
silver('silver', Color(0xFF808080)),
|
||||
gold('gold', Color(0xFFFFD700)),
|
||||
beige('beige', Color(0xFFF5F5DC)),
|
||||
brown('brown', Colors.brown),
|
||||
grey('grey', Colors.grey),
|
||||
black('black', Colors.black),
|
||||
white('white', Colors.white);
|
||||
|
||||
const ColorLabel(this.label, this.color);
|
||||
final String label;
|
||||
final Color color;
|
||||
}
|
||||
87
examples/api/lib/material/search_anchor/search_anchor.1.dart
Normal file
87
examples/api/lib/material/search_anchor/search_anchor.1.dart
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for pinned [SearchAnchor] while scrolling.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const PinnedSearchBarApp());
|
||||
}
|
||||
|
||||
class PinnedSearchBarApp extends StatefulWidget {
|
||||
const PinnedSearchBarApp({super.key});
|
||||
|
||||
@override
|
||||
State<PinnedSearchBarApp> createState() => _PinnedSearchBarAppState();
|
||||
}
|
||||
|
||||
class _PinnedSearchBarAppState extends State<PinnedSearchBarApp> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorSchemeSeed: const Color(0xff6750a4)
|
||||
),
|
||||
home: Scaffold(
|
||||
body: SafeArea(
|
||||
child: CustomScrollView(
|
||||
slivers: <Widget>[
|
||||
SliverAppBar(
|
||||
clipBehavior: Clip.none,
|
||||
shape: const StadiumBorder(),
|
||||
scrolledUnderElevation: 0.0,
|
||||
titleSpacing: 0.0,
|
||||
backgroundColor: Colors.transparent,
|
||||
floating: true, // We can also uncomment this line and set `pinned` to true to see a pinned search bar.
|
||||
title: SearchAnchor.bar(
|
||||
suggestionsBuilder: (BuildContext context, SearchController controller) {
|
||||
return List<Widget>.generate(5, (int index) {
|
||||
return ListTile(
|
||||
titleAlignment: ListTileTitleAlignment.center,
|
||||
title: Text('Initial list item $index'),
|
||||
);
|
||||
});
|
||||
}
|
||||
),
|
||||
),
|
||||
// The listed items below are just for filling the screen
|
||||
// so we can see the scrolling effect.
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: SizedBox(
|
||||
height: 100.0,
|
||||
child: ListView.builder(
|
||||
scrollDirection: Axis.horizontal,
|
||||
itemCount: 10,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return SizedBox(
|
||||
width: 100.0,
|
||||
child: Card(
|
||||
child: Center(child: Text('Card $index')),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||
child: Container(
|
||||
height: 1000,
|
||||
color: Colors.deepPurple.withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
65
examples/api/lib/material/search_anchor/search_anchor.2.dart
Normal file
65
examples/api/lib/material/search_anchor/search_anchor.2.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for [SearchAnchor].
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const SearchBarApp());
|
||||
|
||||
class SearchBarApp extends StatefulWidget {
|
||||
const SearchBarApp({super.key});
|
||||
|
||||
@override
|
||||
State<SearchBarApp> createState() => _SearchBarAppState();
|
||||
}
|
||||
|
||||
class _SearchBarAppState extends State<SearchBarApp> {
|
||||
final SearchController controller = SearchController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final ThemeData themeData = ThemeData(useMaterial3: true);
|
||||
|
||||
return MaterialApp(
|
||||
theme: themeData,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Search Anchor Sample')),
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
SearchAnchor(
|
||||
searchController: controller,
|
||||
builder: (BuildContext context, SearchController controller) {
|
||||
return IconButton(
|
||||
icon: const Icon(Icons.search),
|
||||
onPressed: () {
|
||||
controller.openView();
|
||||
},
|
||||
);
|
||||
},
|
||||
suggestionsBuilder: (BuildContext context, SearchController controller) {
|
||||
return List<ListTile>.generate(5, (int index) {
|
||||
final String item = 'item $index';
|
||||
return ListTile(
|
||||
title: Text(item),
|
||||
onTap: () {
|
||||
setState(() {
|
||||
controller.closeView(item);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
),
|
||||
Center(
|
||||
child: controller.text.isEmpty
|
||||
? const Text('No item selected')
|
||||
: Text('Selected item: ${controller.value.text}'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user