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



在当前的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";
}

提示颜色而言,如果您仍在寻找解决方案,HintColor 将不起作用。使用 ThemeData 的 InputDecoration 属性,如下所示:

inputDecorationTheme: InputDecorationTheme(hintStyle: Theme.of(context).textTheme.title.copyWith(color: Colors.white),)

对于 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),
        );
.
.
}

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

例如:

class CustomSearch extends SearchDelegate<String> {
    CustomSearch() : super(searchFieldLabel: "My own hint");
}