Flutter ThemeData Primary color not changing from theme when trying to add a primary color - flutter

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),
),
);
}
}

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

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(),
);

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),
);

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(),
);
}
}

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
),
)

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(),
);
}
}

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(),
);

theme: ThemeData.dark().copyWith(
colorScheme: ColorScheme.light(
primary: Color(0xFF0A0E21),
),
scaffoldBackgroundColor: Color(0xFF0A0D22),
),

U can Use :
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.red,
)
)

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(),
);

#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.light()
.copyWith(primary: Colors.red, secondary: Colors.red))),
home: InputPage(),
);
}

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

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.

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,
),
),

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(),
);
}
}

Related

How to change the `primary color` for ThemeData dark in flutter?

I am using flutter 2.8.0 to create a simple app.
But when I am trying to change the primary color for the App it does not work with me.
This is the code I am working on:
class BMICalculator extends StatelessWidget {
const BMICalculator({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
primaryColor: const Color(0xFF0A0E21),
scaffoldBackgroundColor: const Color(0xFF0A0E21),
),
home: const InputPage(),
);
}
}
#Mrinal already mentioned you to use primarySwatch. Flutter team intended to build clean up the Theme system and make everything more consistent and based off of the ColorScheme colors. They recommend using color scheme instead of primary color
theme: ThemeData(
primaryColor: ColorsX.primary,
colorScheme: ColorScheme.light().copyWith(primary: ColorsX.primary),
);
For dark theme
theme: ThemeData.dark().copyWith(
colorScheme: ColorScheme.light().copyWith(primary: Colors.tealAccent),
),
output:
For more read this article article
Use primarySwatch
theme: ThemeData(
primarySwatch: Colors.red,
),
I found a solution for me..
MaterialColor _primartSwatch = Colors.purple;
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MyApp',
themeMode: _themeMode,
theme: ThemeData(
primarySwatch: _primartSwatch,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: _primartSwatch,
),
home: SplashScreen(),
);
}

Flutter TabBar wrong labelColor when use ThemeData

I try to change labelColor value for TabBar via ThemeData.
child: MaterialApp(
theme: ThemeData.light().copyWith(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.deepPurple,
).copyWith(
secondary: Colors.amber,
),
tabBarTheme: ThemeData.light().tabBarTheme.copyWith(
labelColor: ThemeData.light().colorScheme.secondary,
),
),
home: const RootContainer(),),);
So my tabs should have shade of amber color. Instead they are some blue ... (picture below).
What I'm doing wrong and how to fix it ?
Result:
The color doesn't change because the ThemeData.light() returns ThemeData that is not initilized.
For using the color of the theme, wrap your widget by Builder with Theme.of(context) like below.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.deepPurple,
).copyWith(
secondary: Colors.amber,
),
),
home: Builder(builder: (context) {
final theme = Theme.of(context);
return Theme(
data: theme.copyWith(
tabBarTheme: theme.tabBarTheme.copyWith(
labelColor: theme.colorScheme.secondary,
),
),
child: const RootContainer(),
);
}),
);
}
}

MaterialApp's theme primary color doesn't work

On this flutter sample secondary color works fine but primary color doesn't seem to work why is that?
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final ThemeData theme = ThemeData();
return MaterialApp(
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(
primary: Colors.blue,
secondary: Colors.orange,
)
),
home: MyHomePage(),
);
}
}
Try this
theme: ThemeData( primaryColor: Colors.lightBlue[800], accentColor: Colors.yellow, ),
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final ThemeData theme = ThemeData();
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: Colors.blue,
).copyWith(
secondary: Colors.green,
),
home: MyHomePage(),
));
}
}

Unable to override ThemeData

I want to have a different background color in MenuPage. I'm trying to override the themeData - I've followed the themeData tutorial from official flutter page but unable to get the result. do you guys have any suggestion?
void main() => runApp(MyApp());
final ThemeData _themeData = new ThemeData( // app theme
scaffoldBackgroundColor: darkNavy,
canvasColor: lightNavy,
accentColor: lightNavy,
primarySwatch: Colors.grey,
appBarTheme: AppBarTheme(
color: darkNavy,
brightness: Brightness.dark,
elevation: 10,
iconTheme: IconThemeData(
color: lightGrey,
),
textTheme: TextTheme(
headline6: GoogleFonts.crimsonPro(
color: darkGrey,
fontSize: 20,
fontWeight: FontWeight.w400))),
);
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: _themeData,
home: HomeScreen(),
);
}
}
class MenuDashBoard extends StatefulWidget {
#override
_MenuDashBoardState createState() => _MenuDashBoardState();
}
class _MenuDashBoardState extends State<MenuDashBoard> {
#override
Widget build(BuildContext context) {
return Theme(
data: ThemeData(
canvasColor: lightNavy,
scaffoldBackgroundColor: lightNavy
),
child: Scaffold(
body: Container(
padding: EdgeInsets.only(top: 30, bottom: 30, left: 20),
child: Column(
My main homeScreen has a dark navy background i want to have a light navy in my Menu page.
You can use the attribute 'scaffoldBackgroundColor' in theme data, setting to the color you want With this, if you are using the scaffold widget, your background app will have the color you want. See:
MaterialApp(
title: 'App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.blue,
),
home: YourScreen()
),

Using AppBarTheme causes default text size?

I'm trying to acheive transparent appbars everywhere in my app using AppBarTheme in my MaterialApp's theme. But it's causing the text size to be the default of 14.0 instead of title size.
I guess it's something to do with TextStyle inheritance, but I don't know much about that.
Example code:
class ExampleScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
ThemeData theme = ThemeData();
return MaterialApp(
theme: theme.copyWith(
appBarTheme: AppBarTheme(
color: Colors.transparent,
brightness: Brightness.light,
elevation: 0,
//I want the defaults, which is why I'm copying an 'empty' ThemeData
//perhaps there's a better way to do this?
textTheme: theme.textTheme,
iconTheme: theme.iconTheme,
),
),
home: Scaffold(
appBar: AppBar(
title: Text('AppBar!'),
),
body: Text('Some text'),
),
);
}
}
This can be achieved by specifying the textTheme inside of the AppBarTheme.
Indeed, the AppBarTheme() has a fully customisable parameter which takes a TextTheme. You almost had it in your question.
Try:
class ExampleScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
ThemeData theme = ThemeData();
return MaterialApp(
theme: theme.copyWith(
appBarTheme: AppBarTheme(
color: Colors.transparent,
brightness: Brightness.light,
elevation: 0,
//I want the defaults, which is why I'm copying an 'empty' ThemeData
//perhaps there's a better way to do this?
textTheme: theme.textTheme.copyWith(
title: theme.textTheme.title.copyWith(fontSize: 20.0),
),
iconTheme: theme.iconTheme,
),
),
home: Scaffold(
appBar: AppBar(
title: Text('AppBar!'),
),
body: Text('Some text'),
),
);
}
}
The textTheme has a title parameter inside of which you can set the fontSize. The default fontSize for titles is 20.0.
Notice the lines:
textTheme: theme.textTheme.copyWith(
title: theme.textTheme.title.copyWith(fontSize: 20.0),
),
You can read more about the TextTheme class here
Each of the parameters is customisable which shows the power of Flutter.
Used the workaround from https://github.com/flutter/flutter/issues/38716
class ExampleScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
ThemeData theme = ThemeData();
var localizedTheme = ThemeData.localize(theme, theme.typography.geometryThemeFor(ScriptCategory.englishLike));
theme = theme.copyWith(
appBarTheme: theme.appBarTheme.copyWith(
color: Colors.transparent,
brightness: Brightness.light,
elevation: 0,
textTheme: localizedTheme.textTheme,
),
);
return MaterialApp(
theme: theme,
home: Scaffold(
appBar: AppBar(
title: Text('AppBar!'),
),
body: Text('Some text'),
),
);
}
}
You could use Theme.of(context) like this:
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: Theme.of(context).copyWith(
appBarTheme: AppBarTheme(
color: Colors.transparent,
brightness: Brightness.light,
elevation: 0,
textTheme: Theme.of(context).textTheme,
iconTheme: Theme.of(context).iconTheme,
),
),
home: Scaffold(
appBar: AppBar(
title: Text('AppBar!'),
),
body: Text('Some text'),
),
);
}
I found a solution. MaterialApp->builder function.
https://api.flutter.dev/flutter/material/MaterialApp/builder.html
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, widget) {
var baseTheme = Theme.of(context);
return Theme(
data: baseTheme.copyWith(
appBarTheme: AppBarTheme(color: Colors.transparent),
),
child: widget);
},
home: HomeView());
}