我想知道如何在Flutter Search委托中为查询设置默认值,以便在启动查询时,用户可以更改默认值。
我尝试在query
@override
中设置buildLeading()
,但是当这样设置时,用户无法更改该值。
提前谢谢
class TheSearch extends SearchDelegate<String>{
TheSearch({this.contextPage,this.controller,this.compressionRateSearch});
BuildContext contextPage;
WebViewController controller;
final suggestions1 = ["https://www.google.com"];
@override
String get searchFieldLabel => "Enter a web address";
@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) {
}
@override
Widget buildSuggestions(BuildContext context) {
final suggestions = query.isEmpty ? suggestions1 : [];
return ListView.builder(itemBuilder: (content,index) => ListTile(
leading:Icon(Icons.arrow_left),
title:Text(suggestions[index])
),);
}
}
答案 0 :(得分:3)
尝试一下,
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
void main() {
runApp(MaterialApp(home: Home()));
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Future<void> _showSearch() async {
await showSearch(
context: context,
delegate: TheSearch(),
query: "any query",
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Search Demo"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: _showSearch,
),
],
),
);
}
}
class TheSearch extends SearchDelegate<String> {
TheSearch({this.contextPage, this.controller});
BuildContext contextPage;
WebViewController controller;
final suggestions1 = ["https://www.google.com"];
@override
String get searchFieldLabel => "Enter a web address";
@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) {
return null;
}
@override
Widget buildSuggestions(BuildContext context) {
final suggestions = query.isEmpty ? suggestions1 : [];
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (content, index) => ListTile(
leading: Icon(Icons.arrow_left), title: Text(suggestions[index])),
);
}
}