Flutter -更改SearchDelegate的搜索提示文本

pkbketx9  于 2022-12-14  发布在  Flutter
关注(0)|答案(7)|浏览(202)

SearchDelegate的当前实现中,没有更改提示文本的选项。当查询为空时,搜索屏幕在查询字段中显示**“Search”**作为提示文本。
提示文本当前在第395行定义如下:

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

但是,有一个existing issue to this subject reported
我无法想出任何解决方案。您知道该问题的任何变通方法吗?

kkbh8khc

kkbh8khc1#

目前SearchDelegate有一个可选成员“searchFieldLabel”来指定搜索字段的标签。它应该如下所示:

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

xdyibdwo2#

class SongSearch extends SearchDelegate<String> {
  SongSearch({
    String hintText = "Song Search",
  }) : super(
          searchFieldLabel: hintText,
          keyboardType: TextInputType.text,
          textInputAction: TextInputAction.search,
        );
.....
vaj7vani

vaj7vani3#

解决方法是创建您自己的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";
}
tpgth1q7

tpgth1q74#

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

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

dbf7pr2w5#

使用null安全,您应该执行类似以下的操作:

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'),
);
azpvetkf

azpvetkf6#

您可以只扩展源类并在构造函数中覆盖它的默认字段,以便为该字段定义您自己的值吗?
例如:

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

mi7gmzs67#

使用“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),
        );
.
.
}

相关问题