关键词SearchDelegate
效果
main.dart
import 'package:flutter/material.dart';
import 'search_bar_demo.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData.light(),
home: SearchBarDemo(),
);
}
}
search_bar_demo.dart
import 'package:flutter/material.dart';
import 'asset.dart';
class SearchBarDemo extends StatefulWidget {
@override
_SearchBarDemoState createState() => _SearchBarDemoState();
}
class _SearchBarDemoState extends State<SearchBarDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Serarch Bar Demo'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed:(){
// print('点击了一下搜索');
showSearch(context: context, delegate: searchBarDelegate());
},
)
],
),
);
}
}
class searchBarDelegate extends SearchDelegate<String>{
@override
List<Widget> buildActions(BuildContext context){
return [
IconButton(
icon: Icon(Icons.clear),
onPressed:()=>query == "",)
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow,progress:transitionAnimation),
onPressed: ()=>close(context,null),
);
}
@override
Widget buildResults(BuildContext context) {
// TODO: implement buildResults
return Container(
width: 100.0,
height: 100.0,
child: Card(
color: Colors.redAccent,
child: Center(
child: Text(query),
),
),
);
}
@override
Widget buildSuggestions(BuildContext context) {
// TODO: implement buildSuggestions
final suggestionList = query.isEmpty ? recentSuggest : searchList.where((input) => input.startsWith(query)).toList();
return ListView.builder(itemCount:suggestionList.length ,itemBuilder: (context,index)=>ListTile(title: RichText(
text: TextSpan(
text:suggestionList[index].substring(0,query.length),
style: TextStyle(
color: Colors.black,fontWeight: FontWeight.bold
),
children: [
TextSpan(
text: suggestionList[index].substring(query.length),
style: TextStyle(color: Colors.grey)
)
]
),
),));
}
}
asset.dart
const searchList = [
"jiejie-大长腿",
"jiejie-水蛇腰",
"gege-帅气欧巴",
"gege-小鲜肉",
];
const recentSuggest = [
"推荐-1",
"推荐-2",
"推荐-3",
];
SearchDelegate源码
SearchDelegate({
this.searchFieldLabel,
this.searchFieldStyle,
this.keyboardType,
this.textInputAction = TextInputAction.search,
});
/// Suggestions shown in the body of the search page while the user types a
/// query into the search field.
///
/// The delegate method is called whenever the content of [query] changes.
/// The suggestions should be based on the current [query] string. If the query
/// string is empty, it is good practice to show suggested queries based on
/// past queries or the current context.
///
/// Usually, this method will return a [ListView] with one [ListTile] per
/// suggestion. When [ListTile.onTap] is called, [query] should be updated
/// with the corresponding suggestion and the results page should be shown
/// by calling [showResults].
Widget buildSuggestions(BuildContext context);
/// The results shown after the user submits a search from the search page.
///
/// The current value of [query] can be used to determine what the user
/// searched for.
///
/// This method might be applied more than once to the same query.
/// If your [buildResults] method is computationally expensive, you may want
/// to cache the search results for one or more queries.
///
/// Typically, this method returns a [ListView] with the search results.
/// When the user taps on a particular search result, [close] should be called
/// with the selected result as argument. This will close the search page and
/// communicate the result back to the initial caller of [showSearch].
Widget buildResults(BuildContext context);
/// A widget to display before the current query in the [AppBar].
///
/// Typically an [IconButton] configured with a [BackButtonIcon] that exits
/// the search with [close]. One can also use an [AnimatedIcon] driven by
/// [transitionAnimation], which animates from e.g. a hamburger menu to the
/// back button as the search overlay fades in.
///
/// Returns null if no widget should be shown.
///
/// See also:
///
/// * [AppBar.leading], the intended use for the return value of this method.
Widget buildLeading(BuildContext context);
/// Widgets to display after the search query in the [AppBar].
///
/// If the [query] is not empty, this should typically contain a button to
/// clear the query and show the suggestions again (via [showSuggestions]) if
/// the results are currently shown.
///
/// Returns null if no widget should be shown.
///
/// See also:
///
/// * [AppBar.actions], the intended use for the return value of this method.
List<Widget> buildActions(BuildContext context);