在SearchDelegate
的当前实现中,没有更改提示文本的选项。当查询为空时,搜索屏幕在查询字段中显示“搜索”作为提示文本。
提示文本当前在第395行定义如下:
final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;
不过,还有一个existing issue to this subject reported。
我想不出任何解决办法。
你知道这个问题的解决方法吗?
最佳答案
通过创建自己的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";
}
关于search - Flutter - 更改SearchDelegate的搜索提示文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54518741/