How to change text color of AppBar, icon color of FAB universally using theme?

后端 未结 9 1592
渐次进展
渐次进展 2020-12-14 13:50

I am able to set the background color of AppBar to Colors.amber. This automatically sets the text color to Black. I am aware of the accessibility i

9条回答
  •  被撕碎了的回忆
    2020-12-14 14:49

    In the widget that runs when you first call main.dart file, you can add a named parameter theme which enables you to add global styles

    In the build method of the widget,

    Widget build(BuildContext context) {
    
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: _buildLightTheme(),
        title: 'title of app',
        home: LoginPage(app: app),
        initialRoute: '/login',
        routes: {
          "/login": (BuildContext context) => LoginPage(app: app,)
        });
     }
    

    Here I have created a separate method for my themes called _buildLightTheme

    ThemeData _buildLightTheme() {
       final ThemeData base = ThemeData.light();
        return base.copyWith(
         accentColor: kUndaGreen,
         scaffoldBackgroundColor: kUndaWhite,
         cardColor: Colors.white,
         textSelectionColor: Colors.amberAccent,
         errorColor: Colors.green,
         textSelectionHandleColor: Colors.black,
        appBarTheme:_appBarTheme()
       );
    }
    

    For the appBarTheme I have a separate method _appBarTheme

    AppBarTheme _appBarTheme(){
      return AppBarTheme( 
         elevation: 0.0,
         color: kUndaGreen,
         iconTheme: IconThemeData(
           color: Colors.white,
         ),);
     }
    

提交回复
热议问题