gpt4 book ai didi

Flutter 搜索委托(delegate),在文本输入中设置一个值或加载上次搜索的值

转载 作者:行者123 更新时间:2023-12-03 03:15:37 28 4
gpt4 key购买 nike

我想知道如何在 Flutter Search 委托(delegate)中为查询设置默认值,以便在它启动时,用户可以更改默认值。

我试图在 @override buildLeading() 中设置 query 但是当这样设置时,用户无法更改值.

提前致谢


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])
),);
}

}

最佳答案

试试这个,

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])),
);
}
}

关于Flutter 搜索委托(delegate),在文本输入中设置一个值或加载上次搜索的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59984082/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com