Last active
February 16, 2025 11:54
-
-
Save rydmike/0d688f47e9fa89aa9f5bc08f0da3da22 to your computer and use it in GitHub Desktop.
Theme context issue: 1 use builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
ThemeData getLightTheme() => ThemeData( | |
brightness: Brightness.light, | |
colorScheme: ColorScheme.fromSeed( | |
seedColor: Colors.blue, | |
brightness: Brightness.light, | |
), | |
); | |
ThemeData getDarkTheme() => ThemeData( | |
brightness: Brightness.dark, | |
colorScheme: ColorScheme.fromSeed( | |
seedColor: Colors.blue, | |
brightness: Brightness.dark, | |
), | |
); | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
var _themeMode = ThemeMode.light; | |
void _toggleThemeMode() { | |
setState(() { | |
_themeMode = | |
_themeMode == ThemeMode.light ? ThemeMode.dark : ThemeMode.light; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
theme: getLightTheme(), | |
darkTheme: getDarkTheme(), | |
themeMode: _themeMode, | |
debugShowCheckedModeBanner: false, | |
home: Builder(builder: (context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Theme Mode Example'), | |
actions: [ | |
IconButton( | |
icon: const Icon(Icons.brightness_6), | |
onPressed: _toggleThemeMode, | |
), | |
], | |
), | |
body: Center( | |
child: Text( | |
'TEXT', | |
style: Theme.of(context).textTheme.bodyLarge, | |
), | |
), | |
); | |
}), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment