[英]Flutter - Change search hint text of SearchDelegate
在SearchDelegate
的当前实现中,没有更改提示文本的选项。 当查询为空时,搜索屏幕在查询字段中显示“搜索”作为提示文本。
提示文本目前在第 395 行定义如下:
final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;
但是,已报告该主题存在一个问题。
我无法为此提出任何解决方案。 您知道该问题的任何解决方法吗?
目前 SearchDelegate 有一个可选成员“searchFieldLabel”来指定搜索字段的标签。 它应该是这样的:
@override
String get searchFieldLabel => 'custom label';
class SongSearch extends SearchDelegate<String> {
SongSearch({
String hintText = "Song Search",
}) : super(
searchFieldLabel: hintText,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
);
.....
通过创建您自己的DefaultMaterialLocalizations
类并将其传递到MaterialApp
小部件,有一种解决方法:
void main() => runApp(SearchApp());
class SearchApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
CustomLocalizationDelegate(),
],
home: Scaffold(
appBar: AppBar(
title: Text('Search demo'),
),
body: Center(
child: Builder(
builder: (context) => MaterialButton(
child: Text('Search'),
onPressed: () => showSearch(
context: context,
delegate: DummyDelegate(),
),
),
),
),
),
);
}
}
class DummyDelegate extends SearchDelegate<String> {
@override
List<Widget> buildActions(BuildContext context) => [];
@override
Widget buildLeading(BuildContext context) => IconButton(
icon: Icon(Icons.close),
onPressed: () => Navigator.of(context).pop(),
);
@override
Widget buildResults(BuildContext context) => Text('Result');
@override
Widget buildSuggestions(BuildContext context) => Text('Suggestion');
}
class CustomLocalizationDelegate extends LocalizationsDelegate<MaterialLocalizations> {
const CustomLocalizationDelegate();
@override
bool isSupported(Locale locale) => locale.languageCode == 'en';
@override
Future<MaterialLocalizations> load(Locale locale) => SynchronousFuture<MaterialLocalizations>(const CustomLocalization());
@override
bool shouldReload(CustomLocalizationDelegate old) => false;
@override
String toString() => 'CustomLocalization.delegate(en_US)';
}
class CustomLocalization extends DefaultMaterialLocalizations {
const CustomLocalization();
@override
String get searchFieldLabel => "My hint text";
}
在SearchDelegate
当前实现中,没有更改提示文本的选项。 当查询为空时,搜索屏幕在查询字段中显示“搜索”作为提示文本。
提示文本当前在第 395 行定义如下:
final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;
但是,报告了此主题存在的问题。
我无法为此提出任何解决方案。 您知道该问题的任何解决方法吗?
就提示颜色而言,如果您仍在寻找解决方案,则 HintColor 将不起作用。 像这样使用 ThemeData 的 InputDecoration 属性:
inputDecorationTheme: InputDecorationTheme(hintStyle: Theme.of(context).textTheme.title.copyWith(color: Colors.white),)
您可以扩展源类并在构造函数中覆盖其默认字段来定义您自己的字段值吗?
例如:
class CustomSearch extends SearchDelegate<String> {
CustomSearch() : super(searchFieldLabel: "My own hint");
}
使用 null-safety ,您应该执行以下操作:
class MyDelegate extends SearchDelegate<String> {
final String? hintText;
MyDelegate({this.hintText});
@override
String? get searchFieldLabel => hintText;
// Other overrides...
}
用法:
showSearch<String>(
context: context,
delegate: MyDelegate(hintText: 'My hint'),
);
使用“searchFieldLabel”属性自定义搜索框的占位符,也可以修改下面列出的 SearchDelegate 属性:
SearchDelegate({
this.searchFieldLabel,
this.searchFieldStyle,
this.searchFieldDecorationTheme,
this.keyboardType,
this.textInputAction = TextInputAction.search,
})........
为了显示:
class SearchScreen extends SearchDelegate {
SearchScreen({
String hintText = "অনুসন্ধান",
}) : super(
searchFieldLabel: hintText,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
searchFieldStyle: TextStyle(
color: Color.fromARGB(178, 255, 255, 255),
fontWeight: FontWeight.bold),
);
.
.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.