23

Im following the BMI Calculator app from the London App Brewery on LinkedIn Learning. when attempting to set the primaryColor to red, my emulator still shows the Light Blue default AppBar even though i am overriding the Primary Color. here's the code

    import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.red,
      ),
      home: const InputPage(),
    );
  }
}

class InputPage extends StatefulWidget {
  const InputPage({Key? key}) : super(key: key);

  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BMI CALCULATOR'),
      ),
      body: const Center(
        child: Text('Body Text'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: const Icon(Icons.add),
      ),
    );
  }
}
  • Your code is still good ! You may pay attention on reinstalling your app or check if hot reload is working – F Perroch Sep 13 '21 at 21:38

18 Answers18

23

Use primarySwatch

theme: ThemeData(
    primarySwatch: Colors.red,
  ),
adamu_fura
  • 773
  • 1
  • 6
  • 14
22

I am also attending same training from LondonAppBrewery. This code fixed the problem.

Widget build(BuildContext context) {
return MaterialApp(
  title: "BMI Calculator",
  debugShowCheckedModeBanner: false,
  theme: ThemeData.dark().copyWith(
    appBarTheme:AppBarTheme(
      backgroundColor: Color(0xff0a0e21),
    ),
    scaffoldBackgroundColor: Color(0xff0a0e21),
  ),
  home: InputPage(),
);
Divy Bhatnagar
  • 221
  • 1
  • 2
11

This issue has been pointed at flutter github page. They say

We will eventually be moving all components away from ThemeData.primaryColor

So you can use

 theme: ThemeData(
     colorScheme: ColorScheme.light().copyWith(primary: Colors.red),
 );
okatarismet
  • 276
  • 2
  • 5
4

Using the following approach you can have full control over the individual properties in Themedata

class BMICalculator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.pink,
        appBarTheme: AppBarTheme(
          backgroundColor: Colors.orangeAccent,
        ),
      ),
      home: InputPage(),
    );
  }
}

enter image description here

Mohtashim
  • 359
  • 1
  • 3
  • 15
1

I'm undergoing the same training program. As Mohtashim mentioned above, I tried to tweak the background app theme code and it worked as expected.

theme: ThemeData(
  primarySwatch: Colors.pink,
  appBarTheme: AppBarTheme(
    backgroundColor: Color(0xFF101427), //use your hex code here
  ),
)
Troll
  • 1,895
  • 3
  • 15
  • 34
1

I also figured out just like you guys answered above. However, in dark design there was a shadowColor missing, so I added it.

@override
Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  theme: ThemeData(
    appBarTheme: const AppBarTheme(
      backgroundColor: Color(0xFF0a0e21),
      elevation: 5.0,
      shadowColor: Colors.black87,
    ),
    primaryColor: const Color(0xFF0A0E21),
    colorScheme: ColorScheme.fromSwatch().copyWith(
      secondary: const Color(0xFF101427),
    ),
    scaffoldBackgroundColor: const Color(0xFF0A0E21),
    ),
    home: const MainPage(),
  );
 }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '22 at 14:39
1

If you want to add default colors that provide by flutter you can change like this.

theme: ThemeData(
    primaryColor: Colors.red,
    primarySwatch: Colors.red,
  ),

If you want to use custom colors, you can use like this

 static const Color primaryColor = Color(0xFF623CEA);

  static MaterialColor primaryColorSwatch = MaterialColor(
    primaryColor.value,
    const <int, Color>{
      50: Color(0xFF623CEA),
      100: Color(0xFF623CEA),
      200: Color(0xFF623CEA),
      300: Color(0xFF623CEA),
      400: Color(0xFF623CEA),
      500: Color(0xFF623CEA),
      600: Color(0xFF623CEA),
      700: Color(0xFF623CEA),
      800: Color(0xFF623CEA),
      900: Color(0xFF623CEA),
    },
  );

MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: primaryColor,
        primarySwatch: primaryColorSwatch,
      ),
      home: Demo(),
    );
K K Muhammed Fazil
  • 668
  • 1
  • 5
  • 12
0
theme: ThemeData.dark().copyWith(
        colorScheme: ColorScheme.light(
          primary: Color(0xFF0A0E21),
        ),
        scaffoldBackgroundColor: Color(0xFF0A0D22),
      ),
  • 1
    Welcome to Stack Overflow, and thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. That's especially important when there's already four other answers that have been submitted. Under what conditions might your approach be preferred? Are you taking advantage of different capabilities? E.g., why do you prefer `colorScheme` over `primarySwatch`? – Jeremy Caney Nov 07 '21 at 00:48
0

U can Use :

    theme: ThemeData(
        colorScheme: ColorScheme.fromSwatch(
          primarySwatch: Colors.red,
        )
    )
0

Try this code for changing app bar color worked for me,replace the color code as per ur need

 Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.red,
        appBarTheme: AppBarTheme(
          backgroundColor: Color(0xFF0A0E21),
        ),
        accentColor: Colors.purple,
      ),
      home: InputPage(),
    );
Arsh Ansari
  • 11
  • 1
  • 3
0
@override
Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          colorScheme: ColorScheme.light()
              .copyWith(primary: Colors.red, secondary: Colors.red))),
      home: InputPage(),
    );
  }
Tushar Katyal
  • 412
  • 5
  • 12
0

This worked for me.

class BMICalculator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(appBarTheme: AppBarTheme(color: Color(0xff0a0e21))),
      home: InputPage(),
    );
  }
}
0

So for hex color, we need to use MaterialColor() of primarySwatch. And for Material color, there are two arguments required, hex color and Map data for the shades of the color. First create a Map variable, color, outside the stateless widget:

Map<int, Color> color =
{
  50:Color.fromRGBO(136,14,79, .1),
  100:Color.fromRGBO(136,14,79, .2),
  200:Color.fromRGBO(136,14,79, .3),
  300:Color.fromRGBO(136,14,79, .4),
  400:Color.fromRGBO(136,14,79, .5),
  500:Color.fromRGBO(136,14,79, .6),
  600:Color.fromRGBO(136,14,79, .7),
  700:Color.fromRGBO(136,14,79, .8),
  800:Color.fromRGBO(136,14,79, .9),
  900:Color.fromRGBO(136,14,79, 1),
};

And then: primarySwatch: MaterialColor(0xFF0A0E21,color),

This will work.

0

The most complete way to do it would be to set the colorScheme property inside ThemeData().

In the ColorScheme class itself you can either decide to set manually all of the color groups, like so:

theme: ThemeData(
   colorScheme: ColorScheme(
       brightness: Brightness.light,
       primary: Colors.red,
       onPrimary: Colors.white,
       secondary: Colors.green,
       onSecondary: Colors.white,
       error: Colors.yellow,
       onError: Colors.black,
       background: Colors.white,
       onBackground: Colors.black,
       surface: Colors.grey,
       onSurface: Colors.black,
   ),
),

Or you can decide to use the ColorScheme.fromSwatch() constructor to create a swatch:

theme: ThemeData(
  colorScheme: ColorScheme.fromSwatch(
    primarySwatch: Colors.green,
    accentColor: Colors.amber,
  ),
),
0

also, you can try colorScheme

 theme: ThemeData(
        colorScheme: ColorScheme.light(
          primary: Colors.red,
          secondary: Colors.purple,
        ),
      ),
0
theme: ThemeData(
        colorSchemeSeed: const Color(0xFF0A0E21))

This is updated snippet for using primary color

0
theme: ThemeData(
        useMaterial3: false,

Make this false - useMaterial3: false,

*Make this false - useMaterial3: false *I had an issue where I was not able to change color inside "theme: *ThemeData( *primaryColor: Color(0xff24386e), *primarySwatch: MaterialColor( *---- *----

*it was started because of a flutter (3.13.0-7.0.pre.46 ), OS (16.5.1) of *IOS and OS (13.4.1 (c)) of Macbook which I upgrade to the latest.

I am using Dart 3.1.0

-1

I follow the same course. This is the code that helped me. Also thank you guys for answering the questions above. I nearly pulled out my hair. If anyone knows why flutter changes and deprecates syntax so dramatically please explain. It would be nice to know.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          backgroundColor: Color(0xff0a0e21),
        ),
        primaryColor: Color(0xFF0A0E21),
        scaffoldBackgroundColor: Color(0xFF0A0E21),
        colorScheme: ColorScheme.fromSwatch().copyWith(
          secondary: Colors.purple,
        ),
      ),
      home: InputPage(),
    );
  }
}