I want to have the option of changing between light(white), dark(black), custom(my original look - blue). Is this possible? If so, how?
Asked
Active
Viewed 95 times
0
-
Does this answer your question? [How to implement Dark mode and Light Mode in flutter?](https://stackoverflow.com/questions/60232070/how-to-implement-dark-mode-and-light-mode-in-flutter) – samuei Apr 05 '23 at 13:28
-
Those answers are for two color themes? Light and dark? – Leena Marie Apr 05 '23 at 13:33
1 Answers
1
You can provide the ThemeData
on MaterialApp(theme:
this will reflect the widget tree.
void main() {
runApp(name());
}
enum AppTheme {
LightTheme(),
DarkTheme,
CustomTheme;
}
extension AppThemeData on AppTheme {
get data {
switch (this) {
case AppTheme.DarkTheme:
return ThemeData.dark();
case AppTheme.CustomTheme:
return ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSwatch().copyWith(
secondary: Colors.deepPurple,
),
);
case AppTheme.LightTheme:
default:
return ThemeData.light();
}
}
}
class name extends StatefulWidget {
const name({super.key});
@override
State<name> createState() => _nameState();
}
// You may use some sort of state-management solution like riverpod/bloc on project level
class _nameState extends State<name> {
AppTheme theme = AppTheme.LightTheme;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
theme: theme.data,
home: Scaffold(
appBar: AppBar(
title: Text("test"),
),
body: Column(
children: [
Text("test"),
for (final value in AppTheme.values)
ElevatedButton(
onPressed: () {
setState(() {
theme = value;
});
},
child: Text(value.name),
)
],
),
),
);
}
}

Md. Yeasin Sheikh
- 54,221
- 7
- 29
- 56
-
Would you be able to show me how to add this functionality to a different file/page? I posted a new question here https://stackoverflow.com/questions/76111375/switch-between-3-color-themes-in-settings-page?noredirect=1#comment134491125_76111375 – Leena Marie May 18 '23 at 16:24