Created
March 24, 2021 04:36
-
-
Save vladnicula/8d7f378317bd9318ad130dd747b8b305 to your computer and use it in GitHub Desktop.
Flutter Theme Switcher
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'; | |
/// Dart data model | |
class AppThemeValues { | |
final Color primaryColor; | |
final Color primaryBackgroundColor; | |
AppThemeValues( | |
{required this.primaryColor, required this.primaryBackgroundColor}); | |
} | |
AppThemeValues ligthTheme = AppThemeValues( | |
primaryColor: Color.fromARGB(255, 0, 0, 0), | |
primaryBackgroundColor: Color.fromARGB(255, 255, 255, 255), | |
); | |
AppThemeValues darkTheme = AppThemeValues( | |
primaryColor: Color.fromARGB(255, 255, 255, 255), | |
primaryBackgroundColor: Color.fromARGB(255, 0, 0, 0), | |
); | |
class AppTheme extends InheritedWidget { | |
final AppThemeValues value; | |
AppTheme({required Widget child, required this.value}) : super(child: child); | |
@override | |
bool updateShouldNotify(covariant AppTheme oldWidget) { | |
return true; | |
} | |
} | |
/// Flutter Context Value Reading | |
class PrimaryHeading extends StatelessWidget { | |
final String title; | |
const PrimaryHeading({Key? key, required this.title}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
var appTheme = context.dependOnInheritedWidgetOfExactType<AppTheme>()!; | |
return Text( | |
title, | |
style: TextStyle(color: appTheme.value.primaryColor), | |
); | |
} | |
} | |
/// Main App | |
class AppThemeSwitcher extends StatefulWidget { | |
const AppThemeSwitcher({Key? key}) : super(key: key); | |
@override | |
_AppThemeSwitcherState createState() => _AppThemeSwitcherState(); | |
} | |
/// Main App State Widget | |
class _AppThemeSwitcherState extends State<AppThemeSwitcher> { | |
AppThemeValues currentTheme = ligthTheme; | |
@override | |
Widget build(BuildContext context) { | |
return AppTheme( | |
value: currentTheme, | |
child: Container( | |
color: currentTheme.primaryBackgroundColor, | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
PrimaryHeading( | |
title: "Theme Switcher", | |
), | |
TextButton( | |
onPressed: () { | |
setState(() { | |
currentTheme = | |
currentTheme == ligthTheme ? darkTheme : ligthTheme; | |
}); | |
}, | |
child: Container( | |
color: currentTheme.primaryColor, | |
child: Text( | |
"Change Theme", | |
style: | |
TextStyle(color: currentTheme.primaryBackgroundColor), | |
), | |
)) | |
], | |
), | |
), | |
); | |
} | |
} | |
/// Flutter boilerplate, not relevant for Context | |
/// ============================================== | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [Expanded(child:AppThemeSwitcher())]), | |
), | |
); | |
} | |
} | |
/// End Flutter boilerplate | |
/// ============================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment