Flutter 颤振搜索无法覆盖某些主题样式

Flutter 颤振搜索无法覆盖某些主题样式,flutter,flutter-layout,flutter-theme,Flutter,Flutter Layout,Flutter Theme,我正试图使用一个自定义设置来定制搜索的外观,但是覆盖appBarTheme似乎只会更新某些主题样式。我可以更改应用程序栏的颜色和文本样式,但我似乎忽略了其他设置,如标高和装饰 如何自定义搜索代理输入装饰和应用程序栏高程 我错过什么了吗?这是故意的行为吗 导入“包装:颤振/材料.省道”; void main()=>runApp(MyApp()); 类MyApp扩展了StatefulWidget{ @凌驾 _MyAppState createState()=>\u MyAppState(); } 类

我正试图使用一个自定义设置来定制搜索的外观,但是覆盖
appBarTheme
似乎只会更新某些主题样式。我可以更改应用程序栏的颜色和文本样式,但我似乎忽略了其他设置,如标高和装饰

如何自定义搜索代理输入装饰和应用程序栏高程

我错过什么了吗?这是故意的行为吗

导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类_MyAppState使用WidgetsBindingObserver扩展状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“标题”,
家:脚手架(
appBar:appBar(
行动:[
建筑商(
生成器:(上下文)=>IconButton(
图标:图标(Icons.search),
onPressed:()=>showSearch(上下文:上下文,委托:CustomSearchDelegate()),
),
),
],
),
),
);
}
}
类CustomSearchDelegate扩展了SearchDelegate{
@凌驾
列出buildActions(BuildContext上下文)=>[
if(query.isNotEmpty)
图标按钮(
图标:图标(Icons.close),
已按下:(){
query=“”;
展示建议(背景);
},
)
];
@凌驾
小部件buildLeading(BuildContext上下文)=>IconButton(
工具提示:“返回”,
图标:AnimatedIcon(图标:AnimatedIcons.menu_箭头,进度:transitionAnimation),
onPressed:()=>close(上下文,null),
);
@凌驾
Widget buildSuggestions(BuildContext上下文)=>文本(“建议转到此处”);
@凌驾
小部件buildResults(BuildContext上下文)=>文本(“结果转到此处”);
@凌驾
主题数据应用程序(构建上下文){
最终主题数据主题=theme.of(上下文);
返回theme.copyWith(
原色:颜色。白色,
PrimaryContheme:theme.PrimaryContheme.copyWith(颜色:Colors.green),
PrimaryColor亮度:亮度。暗,
textTheme:theme.textTheme.copyWith(
标题:TextStyle(fontWeight:fontWeight.normal),
),
//这些↓ 不行☹️
appBarTheme:theme.appBarTheme.copyWith(颜色:Colors.black12,标高:0),
inputDecorationTheme:theme.inputDecorationTheme.copyWith(边框:UnderlineInputBorder()),
);
}
}

我通过添加
appBarTheme:appBarTheme(标高:0.0,颜色:Colors.black12)将标高设置为零。
。我不能让输入装饰以同样的方式工作,我确实在应用程序主题代码的根中添加了一行,但它似乎不起作用

代码根主题代码如下所示:

  class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          backgroundColor: Colors.white,
          appBarTheme: AppBarTheme(elevation: 0.0, color: Colors.black12),//elevation did work
          inputDecorationTheme:
              InputDecorationTheme(border: UnderlineInputBorder()),//this did not imply
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}
@override
  ThemeData appBarTheme(BuildContext context) {
    assert(context != null);
    final ThemeData theme = Theme.of(context);
    assert(theme != null);
    return theme.copyWith(
      primaryColor: Colors.white,
      primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.green),
      primaryColorBrightness: Brightness.dark,
      textTheme: theme.textTheme.copyWith(
        title: TextStyle(fontWeight: FontWeight.normal),
      ),
    );
  }
SearchDelegate中的主题如下:

  class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          backgroundColor: Colors.white,
          appBarTheme: AppBarTheme(elevation: 0.0, color: Colors.black12),//elevation did work
          inputDecorationTheme:
              InputDecorationTheme(border: UnderlineInputBorder()),//this did not imply
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}
@override
  ThemeData appBarTheme(BuildContext context) {
    assert(context != null);
    final ThemeData theme = Theme.of(context);
    assert(theme != null);
    return theme.copyWith(
      primaryColor: Colors.white,
      primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.green),
      primaryColorBrightness: Brightness.dark,
      textTheme: theme.textTheme.copyWith(
        title: TextStyle(fontWeight: FontWeight.normal),
      ),
    );
  }

希望这有帮助

我找到了解决办法

  • 删除建筑小部件
  • 您需要重写buildActions以放置自定义输入
  • 列出构建操作(构建上下文){
    //TODO:实现构建操作
    返回[
    扩展(弹性:1),
    扩大(弹性系数:7),
    扩展(弹性:2),
    ];
    }
    
  • 第一个扩展的小部件是“后退按钮”
  • 第二个扩展的小部件是“TextFormField”,然后您可以覆盖装饰:InputDecoration
  • 第三个扩展小部件是清除textformfield输入

  • 修改了adhithya Shetty的答案。 这是为那些想要改变提示颜色和光标颜色的人准备的

    Color primaryColor = Color(0xff673BB7);  // My Custom Color 
    
     @override
       ThemeData appBarTheme(BuildContext context) {
         assert(context != null);
         final ThemeData theme = Theme.of(context);
         assert(theme != null);
         return theme.copyWith(
           appBarTheme: theme.appBarTheme.copyWith(backgroundColor: primaryColor),  // appbar background color 
           primaryColor: primaryColor,
           textSelectionTheme: TextSelectionThemeData(
             cursorColor: Colors.white ),  // cursor color
           hintColor: Colors.white,       //hint text color 
           primaryIconTheme: theme.primaryIconTheme.copyWith(color: Colors.white), //icons color
           primaryColorBrightness: Brightness.dark,
           textTheme: theme.textTheme.copyWith(
             headline6: TextStyle(fontWeight: FontWeight.normal,color: Colors.white),  // query Color 
           ),
         );
       }
    

    尝试appBarTheme:appBarTheme(标高:4)获取appbar标高。这似乎没有什么区别@AMITPRAJAPATIS可以显示更多与该搜索和appbar相关的代码吗?我更新了代码片段,以包含一个可执行的示例应用程序@AmitPrajapatiIt显示一些样式在颤振源代码中是硬编码的