Theme (Light and Dark) in Flutter
import "package:flutter/material.dart";
import "package:sdreatech_learning_widget/Theme/themes.dart";
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
currentTheme.addListener(() {
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Theme",
debugShowCheckedModeBanner: false,
theme: CustomTheme.lightTheme,
darkTheme: CustomTheme.darkTheme,
themeMode: currentTheme.currentTheme,
home: const ThemePracticeTutorial(),
);
}
}
class ThemePracticeTutorial extends StatelessWidget {
const ThemePracticeTutorial({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: const Text(
"Theme Practice",
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {
currentTheme.toggleTheme();
},
icon: const Icon(Icons.brightness_4_rounded, color: Colors.white),
),
],
),
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Flutter Themes Demo",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),
),
],
),
),
);
}
}
import "package:flutter/material.dart";
CustomTheme currentTheme = CustomTheme();
class CustomTheme extends ChangeNotifier {
static bool _isDarkTheme = false;
ThemeMode get currentTheme {
return _isDarkTheme ? ThemeMode.dark : ThemeMode.light;
}
void toggleTheme() {
_isDarkTheme = !_isDarkTheme;
notifyListeners();
}
static ThemeData get lightTheme {
return ThemeData(
primaryColor: Colors.lightBlue,
scaffoldBackgroundColor: Colors.white,
textTheme: const TextTheme(
displayLarge: TextStyle(color: Colors.black),
displayMedium: TextStyle(color: Colors.black),
bodyLarge: TextStyle(color: Colors.black),
bodyMedium: TextStyle(color: Colors.black),
),
);
}
static ThemeData get darkTheme {
return ThemeData(
primaryColor: Colors.black,
scaffoldBackgroundColor: Colors.black,
textTheme: const TextTheme(
displayLarge: TextStyle(color: Colors.white),
displayMedium: TextStyle(color: Colors.white),
bodyLarge: TextStyle(color: Colors.white),
bodyMedium: TextStyle(color: Colors.white),
),
);
}
}


Comments
Post a Comment