【问题标题】:Change font style of searchFieldLabel in SearchDelegate Flutter在 SearchDelegate Flutter 中更改 searchFieldLabel 的字体样式
【发布时间】:2020-05-17 16:20:37
【问题描述】:

我使用 Material 的 SearchDelegate 实现了一个 SearchAppBar,并使用来自 this question 的响应更改了 Search Delegate 的默认提示文本。

但是,我遇到了无法更改搜索字段中提示文本的字体样式的问题,如下图所示:

我已经有了想要实现的样式,并且正在我的应用程序的其他字体中使用这种样式,但我也想将此样式应用于此提示文本,以免违反应用程序的样式指南。

我用于更改 Search Delegate 的默认消息的代码:

class SearchRoomAppBar extends SearchDelegate {

    SearchRoomAppBar() : super(
        searchFieldLabel: "Search user",
        keyboardType: TextInputType.text,
        textInputAction: TextInputAction.search,
    );   
}

【问题讨论】:

    标签: flutter dart material-design


    【解决方案1】:

    您可以覆盖 SearchDelegate appBarTheme 方法:

      @override
      ThemeData appBarTheme(BuildContext context) {
          assert(context != null);
          final ThemeData theme = Theme.of(context);
          assert(theme != null);
          return theme;
      }
    

    https://dartpad.dartlang.org/69724799fd6653ea4cf650a5a758c3d1

    【讨论】:

      【解决方案2】:

      SearchDelegate 应用栏文本的样式设置为 ThemeData.textTheme.headline6 的任何样式。

      默认情况下,SearchDelegate 使用您应用的 ThemeData,但您可以覆盖 ThemeData appBarTheme(BuildContext context) 方法以返回自定义版本。

        @override
        ThemeData appBarTheme(BuildContext context) {
          // You can use Theme.of(context) directly too
          var superThemeData = super.appBarTheme(context);
      
          return superThemeData.copyWith(
            textTheme: superThemeData.textTheme.copyWith(
              headline6: /* Whatever style you want to apply to your text */,
            ),
          );
        }
      

      【讨论】:

        猜你喜欢
        • 2020-07-26
        • 2019-12-06
        • 1970-01-01
        • 2019-06-28
        • 2011-12-02
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 2011-11-22
        相关资源
        最近更新 更多