【问题标题】:How to customize SearchDelegate(Create custom search Field)如何自定义 SearchDelegate(创建自定义搜索字段)
【发布时间】:2021-06-05 02:26:06
【问题描述】:

我一直在尝试将 Flutter SearchDelgate 自定义为我想要的搜索字段类型。它有一个名为appBarTheme 的方法,返回类型为ThemeData。通常使用ThemeData 您可以更改应用栏主题,但在我的情况下它没有任何改变。我可以自定义提示文本样式 searchFieldStyle 方法,但仅此而已。

代码如下:

class CustomSearchDelegate extends SearchDelegate<Country> {
  @override
  ThemeData appBarTheme(BuildContext context) {
    return ThemeData(
      appBarTheme: AppBarTheme(
        elevation: 0,
        color: themeColor,
        //app bar color I wanted
      ),
    );
  }

  @override
  TextStyle get searchFieldStyle => TextStyle(
        color: whiteTextColor,
        fontWeight: FontWeight.w600,
        fontFamily: GoogleFonts.poppins().fontFamily,
      );

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
        IconButton(
          icon: Icon(
            Icons.close_rounded,
            color: Colors.white,
          ),
          onPressed: () => query = '',
        ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(
        Icons.arrow_back_ios,
        color: Colors.white,
      ),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Column(
      children: [],
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Column(
      children: [],
    );
  }
}

如果有人能帮我解决这个问题,那将非常有帮助。

另外,之前有人提出过类似的问题,但从未得到回答 Flutter create custom search UI extends SearchDelegate

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    很遗憾,您无法完全控制SearchDelegateAppBar 的主题,因为您在appBarTheme 中指定的某些主题属性值未分配给@ 中使用的应用栏小部件987654324@。你可以看看源代码。它仅采用 MaterialApp 主题属性中指定的 ThemeData 中指定的值。就我而言,我需要更改光标颜色,但更改 MaterialApp 中的颜色也会修改其他地方使用的 TextFields 中的颜色。

    一种解决方案是您可以在打开SearchDelegate 之前更改颜色,即在showSearch 之前,然后在从showSearch 导航回来后再次将其更改回原始颜色。

    【讨论】:

    • 是的,我注意到它采用了您在 MaterialApp 中指定的 ThemeData 属性,但即使在我更改了我的应用程序原色之后,AppBar 颜色也设置为颤振应用程序的常用原色......感谢回答。
    • 是的,你是对的。抱歉,我没有注意到您想更改 appBar 颜色。在源代码中未正确分配属性。 ThemeData中的primaryColorSearchDelegate中appBar的颜色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多