Flutter-更改SearchDelegate的搜索提示文本 [英] Flutter - Change search hint text of SearchDelegate

查看:361
本文介绍了Flutter-更改SearchDelegate的搜索提示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SearchDelegate的当前实现中,没有任何选项可以更改提示文本.当查询为空时,搜索屏幕将在查询字段中显示搜索" 作为提示文本.

In current implementation of SearchDelegate, there is no option to change the hint text. When the query is empty, search screen is displaying "Search" in the query field as a hint text.

当前在第395行上定义的提示文本如下:

Hint text is currently defined on line 395 as follows:

final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;

但是,报告了与此主题相关的存在的问题.

There is, however, an existing issue to this subject reported.

我无法为此提出任何解决方案. 您知道该问题的任何解决方法吗?

I wasn't able to come up with any solution for this. Do you know any workaround for the issue?

推荐答案

为此,有一个解决方法,方法是创建自己的DefaultMaterialLocalizations类并将其传递到MaterialApp小部件中:

There is a workaround for this by creating your own DefaultMaterialLocalizations class and passing it into the MaterialApp widget:

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";
}

这篇关于Flutter-更改SearchDelegate的搜索提示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆