how to change theme data in flutter? - flutter

class _NavigationBarState extends State<NavigationBar> {
int _currentIndex = 0;
final List<Widget> tabs = [
CustomerAccountPage(),
HomePage(),
AppInforamtionPage(),
CategoriesPage(),
];
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
primaryColor: Colors.amber,
accentColor: Colors.black
),
debugShowCheckedModeBanner: false,
home: Scaffold(
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: true,
unselectedItemColor: Theme.of(context).primaryColor,
selectedItemColor: Theme.of(context).accentColor,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("account"),
),
BottomNavigationBarItem(
icon: Icon(Icons.play_for_work),
title: Text("shop"),
),
BottomNavigationBarItem(
icon: Icon(Icons.all_out),
title: Text("more info "),
),
BottomNavigationBarItem(
icon: Icon(Icons.all_out),
title: Text(" categories"),
),
],
onTap: (index) {
setState(() {
_currentIndex = index;
});
}),
),
centerTitle: true,
backgroundColor: Colors.white,
),
body: tabs[_currentIndex],
floatingActionButton: FloatingActionButton(onPressed: null, backgroundColor: Theme.of(context).primaryColor,),
),
);
}
}
I'm using flutter , and trying to set the theme data, but it doesn't work.
I tried to change the theme in this way, but I can't see the result in my app, I don't know what is the problem, can anyone help me!
stackoverflow want from me to explain more, but I haven't anything else to explain, thanks in advance!

The context used in Theme.of(context).primaryColor is not the right context. You need to put the MaterialApp in another widget wrapping the current widget, e.g.,
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.light()
.copyWith(primaryColor: Colors.amber, accentColor: Colors.black),
home: NavigationBar(),
);
}
}
And the build method of the _NavigationBarState:
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: true,
unselectedItemColor: Theme.of(context).primaryColor,
selectedItemColor: Theme.of(context).accentColor,
...

After trying very hard to understand Material Design, I found the following solution which is easy and clean. Here is my complete code.
color_scheme.dart
import 'package:flutter/material.dart';
const lightColorScheme = ColorScheme(
brightness: Brightness.light,
primary: Color(0xFF687DAF),
onPrimary: Color(0xFFFFFFFF),
secondary: Color(0xFFf37b67),
onSecondary: Color(0xFFFFFFFF),
error: Color(0xFFBA1A1A),
onError: Color(0xFFFFFFFF),
background: Color(0xFFFEFFFF),
onBackground: Color(0xFF3b3b3b),
surface: Color(0xFFFEFFFF),
onSurface: Color(0xFF3b3b3b),
);
const darkColorScheme = ColorScheme(
brightness: Brightness.dark,
primary: Color(0xFFADC6FF),
onPrimary: Color(0xFF002E69),
secondary: Color(0xFFBBC6E4),
onSecondary: Color(0xFF253048),
error: Color(0xFFFFB4AB),
onError: Color(0xFF690005),
background: Color(0xFF1B1B1F),
onBackground: Color(0xFFE3E2E6),
surface: Color(0xFF1B1B1F),
onSurface: Color(0xFFE3E2E6),
);
theme.dart
import 'package:first_project/shared/color_schemes.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
final ThemeData lightThemeDataCustom = _buildLightTheme();
ThemeData _buildLightTheme() {
final ThemeData base = ThemeData.light();
return base.copyWith(
colorScheme: lightColorScheme,
primaryColor: lightColorScheme.primary,
scaffoldBackgroundColor: lightColorScheme.background,
textTheme: GoogleFonts.montserratTextTheme(ThemeData.light().textTheme),
);
}
final ThemeData darkThemeDataCustom = _buildDarkTheme();
ThemeData _buildDarkTheme() {
final ThemeData base = ThemeData.dark();
return base.copyWith(
colorScheme: darkColorScheme,
primaryColor: darkColorScheme.primary,
scaffoldBackgroundColor: darkColorScheme.background,
textTheme: GoogleFonts.montserratTextTheme(ThemeData.dark().textTheme),
);
}
main.dart
import 'package:first_project/shared/theme_two.dart';
import 'package:flutter/services.dart';
import '../screens/bottom_bar.dart';
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) {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.dark));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: lightThemeDataCustom,
darkTheme: darkThemeDataCustom,
home: const BottomBar(),
);
}
}
If you follow this approach, then you don't need to define colors in each and every widget. Just change the color scheme, flutter will automatically change colors according to Light theme and Dark theme.
I do not add textTheme customization code but you can do it in theme.dart file.
Hope this will help someone.

Related

How to change theme to dark on click on IconButton?

In my application, in the appBar, there is a button that should change the theme to dark. I need to create functionality Provider. How can this be implemented? I just need to change the Scaffold color to black and the text color to white.
My main.dart:
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 50.0, fontWeight: FontWeight.bold),
headline5: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
subtitle2: TextStyle(fontSize: 10.0, color: Colors.black),
bodyText1: TextStyle(fontSize: 14.0, color: Colors.black),
),
),
home: const HomeScreen(),
);
}
My switch button:
appBar: AppBar(
title: const Text('Flutter theme config'),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
},
icon: const Icon(Icons.dark_mode),
)
],
),
Theme provider:
class ThemeProvider extends ChangeNotifier {
}
You can try something like this :
First we provide our Provider globally for the whole application, and then in the attribute theme: we listen for the change.
** main **
void main() async {
runApp(
MultiProvider( // create the provider
providers: [
ChangeNotifierProvider(
create: (_) => ThemeProvider(),
)
],
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material App',
initialRoute: HomeScreen.routerName,
routes: {
},
theme: Provider.of<ThemeProvider>(context).currentTheme, // listen to the current theme
);
}
}
In the provider we will only have two functions, one to switch to LightMode and the other to DarkMode, then we add it to the currentTheme variable which is the one that listens in the main
** ThemeProvider **
class ThemeProvider extends ChangeNotifier {
ThemeData? currentTheme;
setLightMode() {
currentTheme = ThemeData(
brightness: Brightness.light, // LightMode
scaffoldBackgroundColor: Colors.red,
[...] // more attributes
);
notifyListeners();
}
setDarkmode() {
currentTheme = ThemeData(
brightness: Brightness.dark, // DarkMode
scaffoldBackgroundColor: Colors.green,
[...] // more attributes
);
notifyListeners();
}
}
Finally we create a StatefulWidget to change the isDarkMode variable to call the provider
** Button Home **
class _HomeScreenState extends State<SettingsScreen> {
bool isDarkmode = false; // new variable
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Settings"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: IconButton(
onPressed: () {
final themeProvider =
Provider.of<ThemeProvider>(context, listen: false); // get the provider, listen false is necessary cause is in a function
setState(() {
isDarkmode = !isDarkmode;
}); // change the variable
isDarkmode // call the functions
? themeProvider.setDarkmode()
: themeProvider.setLightMode();
},
icon: const Icon(Icons.dark_mode),
),
),
);
}
}

FLUTTER - How to properly upgrade this code to work with RiverPod 1.0+?

I added the libraries to pubspec.yaml. In the current project I am not using hooks, but used the package because I want to use this to build a skeleton starter project.
pubspec.yaml:
flutter_hooks: ^0.18.2+1
hooks_riverpod: ^1.0.3
Then I modified the code to the latest RiverPod syntax. This involved changing things to Consumer widgets and using the ref parameter. The app now runs but just is not switching from dark mode to light mode and vice versa!
main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
class AppThemeState extends StateNotifier<bool> {
AppThemeState(): super(false);
void setLightTheme() => state = false;
void setDarkTheme() => state = true;
}
// Theme
final appThemeStateNotifier = StateNotifierProvider((ref) => AppThemeState());
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends HookConsumerWidget {
#override
Widget build(BuildContext context, WidgetRef ref) {
final darkModeEnabled = ref.watch(appThemeStateNotifier);
return MaterialApp(
title: 'Flutter Demo',
theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
themeMode: darkModeEnabled != null ? ThemeMode.dark : ThemeMode.light,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 2.0,
title: Text("Flutter Theme Riverpod Demo"),
),
body: Column(
children: [
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Light Mode"),
DarkModeSwitch(),
Text("Dark Mode"),
],
),
),
],
),
);
}
}
class DarkModeSwitch extends ConsumerWidget {
#override
Widget build(BuildContext context, WidgetRef ref) {
final darkModeEnabled = ref.read(appThemeStateNotifier) == true;
return //Text('$darkModeEnabled');
Switch(
value: darkModeEnabled,
onChanged: (enabled) {
if (enabled) {
ref.read(appThemeStateNotifier.notifier).setDarkTheme();
} else {
ref.read(appThemeStateNotifier.notifier).setLightTheme();
}
},
);
}
}
class AppTheme {
// Private Constructor
AppTheme._();
static final lightTheme = ThemeData(
scaffoldBackgroundColor: Colors.white,
appBarTheme: AppBarTheme(
color: Colors.teal,
iconTheme: IconThemeData(
color: Colors.white,
),
),
textTheme: TextTheme(
bodyText2: TextStyle(
color: Colors.black,
),
),
);
static final darkTheme = ThemeData(
scaffoldBackgroundColor: Colors.black,
appBarTheme: AppBarTheme(
color: Colors.black,
iconTheme: IconThemeData(
color: Colors.white,
),
),
textTheme: TextTheme(
bodyText2: TextStyle(
color: Colors.white,
),
),
);
}
The problem is at this line:
themeMode: darkModeEnabled != null ? ThemeMode.dark : ThemeMode.light,
darkModeEnabled can't be null.
Use this instead:
themeMode: darkModeEnabled ? ThemeMode.dark : ThemeMode.light,

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

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

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

Why is my switch widget not sliding? I can't seem to change themes

I can't seem to figure out what is preventing the switch from changing states. I know if I set isDark to true, I get my dark theme, and if I set isDark to false I get my light theme. It might be possible that flutter is interpreting isDark as constantly being false. However, I am unsure as to how to fix that if that were the case. Regardless, something is stopping the switch and I can't figure it out. Any help is appreciated thank you.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: ChangeThemeButton(),
theme: _theme,
// theme: MyThemes.lightTheme,
// darkTheme: MyThemes.darkTheme,
// themeMode: ThemeMode.system,
routes: {
'homepage': (context) => HomePage(),
'sign-in': (context) => SignIn(),
'register': (context) => RegisterPage(),
},
);
}
}
class ChangeThemeButton extends StatefulWidget {
#override
_ChangeThemeButtonState createState() => _ChangeThemeButtonState();
}
class _ChangeThemeButtonState extends State<ChangeThemeButton> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Switch(
value: isDark,
onChanged: (val) => {
setState((){
val = isDark;
themeProvider(val);
})
},
),
),
);
}
}
var _theme = (isDark == false) ? MyThemes.lightTheme : MyThemes.darkTheme;
bool val;
bool isDark = false;
bool themeProvider (bool val) {
switch (val) {
case false:
return isDark == true;
break;
case true:
return isDark == false;
break;
}
return null;
}
class MyThemes {
static final lightTheme = ThemeData(
colorScheme: ColorScheme.light(),
iconTheme: IconThemeData(color: Colors.black),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
unselectedItemColor: Colors.white,
selectedItemColor: Color(0xff9D4C8F),
showUnselectedLabels: true,
selectedIconTheme: IconThemeData(
color: Color(0xff9D4C8F),
),
unselectedIconTheme: IconThemeData(
color: Colors.white,
),
),
primaryColor: Colors.deepPurpleAccent[100],
primaryColorDark: Colors.black54,
secondaryHeaderColor: Colors.deepPurpleAccent[50],
scaffoldBackgroundColor: Colors.white,
cardColor: Colors.deepPurpleAccent,
cardTheme: CardTheme(elevation: 5));
static final darkTheme = ThemeData(
colorScheme: ColorScheme.dark(),
iconTheme: IconThemeData(color: Colors.white),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
unselectedItemColor: Colors.white38,
selectedItemColor: Color(0xff9D4C8F),
showUnselectedLabels: true,
selectedIconTheme: IconThemeData(
color: Color(0xff9D4C8F),
),
unselectedIconTheme: IconThemeData(
color: Colors.white,
),
),
primaryColor: Colors.black,
primaryColorDark: Colors.grey,
secondaryHeaderColor: Colors.deepPurpleAccent[50],
scaffoldBackgroundColor: Colors.white38,
cardColor: Colors.deepPurpleAccent,
cardTheme: CardTheme(elevation: 5)
);
}
change val = isDark; to isDark = val, or you could remove that line and change it into the provider, because now you say that even if val changes, you set it right below to isDark, so nothing changes.