'If you want to use dark/light mode in your app according to your System mode assuming the end devices support that - the MaterialApp
has a theme
attribute which represents the the 'System light theme' where as the darkTheme
Attribute represents the 'System dark mode'. I created a class called AppTheme
where i initialise 2 static final ThemeData
objects:
class AppTheme {
static final ThemeData lightTeme = ThemeData(
//Theme light
brightness: Brightness.light,
primaryColor: Colors.white,
accentColor: Colors.black,
inputDecorationTheme: InputDecorationTheme(
labelStyle: TextStyle(color: Colors.black),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.black)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.black))),
buttonTheme: ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.black)),
),
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity);
static final ThemeData darkTheme = ThemeData(
brightness: Brightness.dark,
primaryColor: Colors.black,
accentColor: Colors.white,
inputDecorationTheme: InputDecorationTheme(
labelStyle: TextStyle(color: Colors.white),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.white)),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: BorderSide(color: Colors.white))),
buttonTheme: ButtonThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.white)),
),
//Button Theme Light
visualDensity: VisualDensity.adaptivePlatformDensity);
}
you can access them in your MaterialApp
widget:
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: S.delegate.supportedLocales,
theme: AppTheme.lightTeme,
darkTheme: AppTheme.darkTheme,
home: HomeworkDiaryApp(),
);
}
You dont have to worry about your app handling changes. It will do it automatically once you change the system dark mode (Testing on my real device samsung galaxy s8 works like a charm)
I think its paradoxical to implement an 'only in app dark and light mode' when all the phones people mostly use today have a system dark or light mode!