dart - 如何在搜索委托上添加提示文本?
问题描述
如何在 Flutter showSearch() 上更改提示文本和提示文本颜色
AppBar(
backgroundColor: Colors.blue,
centerTitle: true,
title: appBarIcon(context),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: CustomSearchDelegateAssets(_searchdata, widget.regId));
})
],
),
解决方案
根据方法的来源,showSearch
它只是推了一条新的路线—— _SearchPageRoute
.
这个内部定义的路由提供了一个有状态的小部件 ( _SearchPage
),它由一个新建的AppBar
带有一个文本字段的小部件组成。
您需要做的是基于_SearchPageRoute
返回 custom创建一个自定义路由_SearchPage
。路由需要根据showSearch
逻辑通过自己的方法推送。
在你的自定义中实现自定义hintText
& hintStyle
-修改。InputDecoration
_SearchPageState
例如MySearchPageRoute
class MySearchPageRoute<T> extends PageRoute<T> {
// ...
@override
Widget buildPage(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) {
return MySearchPage<T>( // KEY PROP: Custom stateful widget based on `_SearchPage`
delegate: delegate,
animation: animation,
);
}
// ...
}
我的搜索页面:
class MySearchPage<T> extends StatefulWidget {
// ...
@override
State<StatefulWidget> createState() => MySearchPageState<T>();
}
class MySearchPageState<T> extends State<MySearchPage<T>> {
// ...
@override
Widget build(BuildContext context) {
// ...
return Semantics(
// ...
child: Scaffold(
appBar: AppBar(
// ...
title: TextField(
// ...
decoration: InputDecoration(
border: InputBorder.none,
hintText: "My Custom Search Label", // KEY PROP
hintStyle: TextStyle(color: Colors.red), // KEY PROP
),
),
// ...
),
// ...
)
);
}
// ...
}
推送路线:
Future<T> showMySearch<T>({
@required BuildContext context,
@required SearchDelegate<T> delegate,
String query = '',
}) {
// ...
return Navigator.of(context).push(MySearchPageRoute<T>( // KEY PROP
delegate: delegate,
));
}
换句话说 - 简单地从源_SearchPageRoute
文件中复制、、_SearchPage
和。然后在我用 标记的行中进行更改。_SearchPageState
showSearch
// KEY PROP
推荐阅读
- php - 将单选按钮输入保存到数组中
- webrtc - webrtc 应用程序显示我的 Turn 服务器已损坏,但它可以工作
- python - GUI 控件的行和列不起作用
- python - 在 SQLAlchemy 中过滤时访问关系的反向引用
- javascript - 如何在纸张js中缩放
- dart - flatButton 需要标签
- c# - 可以使用 C# 以编程方式反编译存储过程
- ansible - Ansible win_lineinfile 模块失败
- asp.net-mvc - .NET MVC DisplayModeProvider 回退
- pagination - 如何使用 Github Pages 在 Jekyll 中对类别进行分页?