Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
颤振-更改SearchDelegate的搜索提示文本_Search_Flutter_Hint - Fatal编程技术网

颤振-更改SearchDelegate的搜索提示文本

颤振-更改SearchDelegate的搜索提示文本,search,flutter,hint,Search,Flutter,Hint,在当前的SearchDelegate实现中,没有更改提示文本的选项。当查询为空时,搜索屏幕将在查询字段中显示“搜索”作为提示文本 提示文本当前在第395行定义如下: final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel; 然而,有一个问题 我没能想出任何解决办法。 您知道该问题的解决方法吗?通过创建自己的DefaultMaterialLocalizations类并将其传递到Mater

在当前的
SearchDelegate
实现中,没有更改提示文本的选项。当查询为空时,搜索屏幕将在查询字段中显示“搜索”作为提示文本

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

final String searchFieldLabel = MaterialLocalizations.of(context).searchFieldLabel;
然而,有一个问题

我没能想出任何解决办法。
您知道该问题的解决方法吗?

通过创建自己的
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";
}
void main()=>runApp(SearchApp());
类SearchApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
本地化授权:[
CustomLocalizationDelegate(),
],
家:脚手架(
appBar:appBar(
标题:文本(“搜索演示”),
),
正文:中(
孩子:建筑工人(
生成器:(上下文)=>MaterialButton(
子项:文本(“搜索”),
onPressed:()=>showSearch(
上下文:上下文,
委托:DummyDelegate(),
),
),
),
),
),
);
}
}
类DummyDelegate扩展了SearchDelegate{
@凌驾
列出buildActions(BuildContext上下文)=>[];
@凌驾
小部件buildLeading(BuildContext上下文)=>IconButton(
图标:图标(Icons.close),
onPressed:()=>Navigator.of(context.pop(),
);
@凌驾
小部件buildResults(BuildContext上下文)=>Text('Result');
@凌驾
小部件构建建议(BuildContext上下文)=>文本(“建议”);
}
类CustomLocalizationDelegate扩展LocalizationsDelegate{
const CustomLocalizationDelegate();
@凌驾
bool isSupported(Locale)=>Locale.languageCode=='en';
@凌驾
未来加载(区域设置)=>SynchronousFuture(const CustomLocalization());
@凌驾
bool shouldlreload(CustomLocalizationDelegate old)=>false;
@凌驾
字符串toString()=>“CustomLocalization.delegate(en_US)”;
}
类CustomLocalization扩展了DefaultMaterialLocalization{
const CustomLocalization();
@凌驾
字符串get-searchFieldLabel=>“我的提示文本”;
}

当前SearchDelegate有一个可选成员“searchFieldLabel”,用于指定搜索字段的标签。 它应该是这样的:

  @override
  String get searchFieldLabel => 'custom label';

就提示颜色而言,如果您仍在寻找解决方案,HintColor将无法工作。使用主题数据的InputDecoration属性,如下所示:

inputDecorationTheme: InputDecorationTheme(hintStyle: Theme.of(context).textTheme.title.copyWith(color: Colors.white),)
使用以下代码:)
class SongSearch扩展了SearchDelegate{
歌曲搜索({
String hintText=“歌曲搜索”,
}):超级(
searchFieldLabel:hintText,
键盘类型:TextInputType.text,
textInputAction:textInputAction.search,
);
.....

您可以扩展源类并在构造函数中重写其默认字段,以便为字段定义自己的值

例如:

class CustomSearch extends SearchDelegate<String> {
    CustomSearch() : super(searchFieldLabel: "My own hint");
}
class CustomSearch扩展了SearchDelegate{
CustomSearch():super(searchFieldLabel:“我自己的提示”);
}

只需将这一行插入到要扩展的SearchDelegate类中即可

class MyClass extends SearchDelegate {
    @override
    String get searchFieldLabel => "Your HINT here";

    ...
} 
输出:

棒极了。您能建议我们如何更改搜索栏的提示文本颜色和光标颜色吗?因为它不采用主应用程序ThemeData中定义的值。是的,ThemeData有一个
hintColor
属性。是的,似乎没有办法覆盖光标颜色。我成功地覆盖了颜色以匹配主题的应用程序栏,但光标未更改。这应作为正确答案进行检查。第一个是一个解决方法,当应用程序中有两个或更多搜索时,该方法不起作用。更改提示文本样式的一些提示?@riccardogobbo I打开了一个PR()因为颤振目前缺少使用构造函数设置颤振的正确方法。目前只能使用以下解决方法:简单易用的解决方案!
class MyClass extends SearchDelegate {
    @override
    String get searchFieldLabel => "Your HINT here";

    ...
}