I have a project I'm working on and I want to switch it to the three modes, light, dark, and according to the system.
I want an add-on code to do this in addition to saving the mode to be selected, away from any state management, I have put a simple code with three buttons for each case.
Material app
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(scaffoldBackgroundColor: Colors.white),
darkTheme: ThemeData(scaffoldBackgroundColor: Colors.black),
themeMode: ThemeMode.system,
home: const HomePage(),
);
}
}
HomePage
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(onPressed: (){}, child: const Text('Dark mode')),
ElevatedButton(onPressed: (){}, child: const Text('Light mode')),
ElevatedButton(onPressed: (){}, child: const Text('System mode')),
],
)
);
}
}