在SearchDelegate
的当前实现中,没有任何选项可以更改提示文本。当查询为空时,搜索屏幕将在查询字段中显示“搜索” 作为提示文本。
当前在第395行定义提示文本,如下所示:
final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;
但是有一个existing issue to this subject reported。
我无法为此提出任何解决方案。 您知道该问题的任何解决方法吗?
答案 0 :(得分:11)
当前SearchDelegate具有可选成员“ searchFieldLabel”,用于指定搜索字段的标签。 它应该看起来像这样:
@override
String get searchFieldLabel => 'custom label';
答案 1 :(得分:7)
有一个解决方法,可以创建您自己的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";
}
答案 2 :(得分:4)
只需将此行插入您要扩展SearchDelegate的Class
class MyClass extends SearchDelegate {
@override
String get searchFieldLabel => "Your HINT here";
...
}
答案 3 :(得分:2)
class SongSearch extends SearchDelegate<String> {
SongSearch({
String hintText = "Song Search",
}) : super(
searchFieldLabel: hintText,
keyboardType: TextInputType.text,
textInputAction: TextInputAction.search,
);
.....
答案 4 :(得分:1)
使用空值安全,您应该执行以下操作:
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'),
);
答案 5 :(得分:0)
就提示颜色而言,如果您仍在寻找解决方案,则HintColor将不起作用。像这样使用ThemeData的InputDecoration属性:
inputDecorationTheme: InputDecorationTheme(hintStyle: Theme.of(context).textTheme.title.copyWith(color: Colors.white),)
答案 6 :(得分:0)
您可以扩展源类并在构造函数中覆盖其默认字段,以为该字段定义自己的值?
例如:
class CustomSearch extends SearchDelegate<String> {
CustomSearch() : super(searchFieldLabel: "My own hint");
}