Created
March 26, 2025 18:12
-
-
Save prof3ssorSt3v3/e11d21dd74437d06737b333b00c1a7e7 to your computer and use it in GitHub Desktop.
Flutter theme code from class 12.1
This file contains hidden or 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'; | |
import 'package:theming/components/mybutton.dart'; | |
import 'package:theming/theme/theme.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: lightTheme, | |
darkTheme: darkTheme, | |
home: const MyHomePage( | |
title: 'Flutter Demo Home Page', | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
TextTheme base = Theme.of(context).textTheme; | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: | |
Theme.of(context).colorScheme.error, | |
title: Text( | |
widget.title, | |
style: Theme.of(context).textTheme.bodySmall, | |
), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
"I am special", | |
style: Theme.of( | |
context, | |
).textTheme.displayLarge?.copyWith( | |
color: | |
Theme.of(context).colorScheme.error, | |
fontSize: 40, | |
), | |
), | |
MyButton(label: 'Press this!'), | |
Text( | |
'You have pushed the button this many times:', | |
style: | |
Theme.of( | |
context, | |
).textTheme.displayLarge, | |
), | |
Text( | |
'$_counter', | |
style: | |
Theme.of( | |
context, | |
).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} |
This file contains hidden or 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
// /lib/theme/theme.dart | |
import 'package:flutter/material.dart'; | |
import 'package:google_fonts/google_fonts.dart'; | |
ThemeData lightTheme = ThemeData( | |
brightness: Brightness.light, | |
colorScheme: ColorScheme( | |
brightness: Brightness.light, | |
primary: Colors.blue.shade300, | |
onPrimary: Colors.blue.shade800, | |
secondary: Colors.lime.shade300, | |
onSecondary: Colors.lime.shade800, | |
error: Colors.red.shade300, | |
onError: Colors.red.shade800, | |
surface: Colors.amber.shade300, | |
onSurface: Colors.amber.shade800, | |
), | |
textTheme: TextTheme( | |
displayLarge: GoogleFonts.notoSans( | |
fontSize: 70, | |
fontWeight: FontWeight.w700, | |
), | |
displayMedium: TextStyle( | |
fontSize: 50, | |
fontWeight: FontWeight.w500, | |
), | |
displaySmall: TextStyle( | |
fontSize: 40, | |
fontWeight: FontWeight.w500, | |
), | |
bodyLarge: TextStyle( | |
fontSize: 30, | |
fontWeight: FontWeight.w300, | |
), | |
bodyMedium: TextStyle( | |
fontSize: 24, | |
fontWeight: FontWeight.w300, | |
), | |
bodySmall: TextStyle( | |
fontSize: 16, | |
fontWeight: FontWeight.w300, | |
), | |
), | |
); | |
ThemeData darkTheme = ThemeData( | |
brightness: Brightness.dark, | |
colorScheme: ColorScheme( | |
brightness: Brightness.dark, | |
primary: Colors.blue.shade800, | |
onPrimary: Colors.blue.shade300, | |
secondary: Colors.lime.shade800, | |
onSecondary: Colors.lime.shade300, | |
error: Colors.red.shade800, | |
onError: Colors.red.shade300, | |
surface: Colors.amber.shade800, | |
onSurface: Colors.amber.shade300, | |
), | |
textTheme: TextTheme( | |
displayLarge: GoogleFonts.notoSans( | |
fontSize: 70, | |
fontWeight: FontWeight.w700, | |
), | |
displayMedium: TextStyle( | |
fontSize: 50, | |
fontWeight: FontWeight.w500, | |
), | |
displaySmall: TextStyle( | |
fontSize: 40, | |
fontWeight: FontWeight.w500, | |
), | |
bodyLarge: TextStyle( | |
fontSize: 30, | |
fontWeight: FontWeight.w300, | |
), | |
bodyMedium: TextStyle( | |
fontSize: 24, | |
fontWeight: FontWeight.w300, | |
), | |
bodySmall: TextStyle( | |
fontSize: 16, | |
fontWeight: FontWeight.w300, | |
), | |
), | |
); |
This file contains hidden or 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
// /lib/theme/theme.provider.dart | |
import 'package:provider/provider.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:theming/theme/theme.dart'; | |
class ThemeProvider with ChangeNotifier { | |
ThemeData _themeData = lightTheme; | |
ThemeData get themeData => _themeData; | |
set themeData(ThemeData themeData) { | |
_themeData = themeData; | |
//update the private internal _themeData | |
notifyListeners(); | |
//tell anyone who is using ThemeProvider themeData | |
} | |
void toggleTheme() { | |
if (_themeData == lightTheme) { | |
themeData = darkTheme; | |
} else { | |
themeData = lightTheme; | |
} | |
} | |
} | |
//mytheme = ThemeProvider.themeData calls get | |
// ThemeProvider.themeData = 876 calls set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment