Created
August 29, 2022 19:42
-
-
Save felipecastrosales/5a1fe8ddf45c93d3866043bf647bbf58 to your computer and use it in GitHub Desktop.
Theme Extension Updated
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:flutter/scheduler.dart'; | |
void main() { | |
timeDilation = 5.0; | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
State<MyApp> createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
bool isLightTheme = true; | |
void toggleTheme() { | |
setState(() => isLightTheme = !isLightTheme); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.light().copyWith( | |
extensions: <ThemeExtension<dynamic>>[ | |
const MyColors( | |
brandColor: Color(0xFF1E88E5), | |
danger: Color(0xFFE53935), | |
), | |
], | |
), | |
darkTheme: ThemeData.dark().copyWith( | |
extensions: <ThemeExtension<dynamic>>[ | |
const MyColors( | |
brandColor: Color(0xFF90CAF9), | |
danger: Color(0xFFEF9A9A), | |
), | |
], | |
), | |
themeMode: isLightTheme ? ThemeMode.light : ThemeMode.dark, | |
home: Home( | |
isLightTheme: isLightTheme, | |
toggleTheme: toggleTheme, | |
), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
const Home({ | |
Key? key, | |
required this.isLightTheme, | |
required this.toggleTheme, | |
}) : super(key: key); | |
final bool isLightTheme; | |
final void Function() toggleTheme; | |
@override | |
Widget build(BuildContext context) { | |
final myColors1 = Theme.of(context).extension<MyColors>()!; | |
final myColors2 = Theme.of(context).extension<MyColors?>(); | |
final myColors3 = Theme.of(context).extension<MyColors>(); | |
return Material( | |
child: Center( | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
// 1. | |
Container(width: 50, height: 50, color: myColors1.brandColor), | |
const SizedBox(width: 10), | |
Container(width: 50, height: 50, color: myColors1.danger), | |
const SizedBox(width: 50), | |
IconButton( | |
icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny), | |
onPressed: toggleTheme, | |
), | |
// 2. | |
Container(width: 50, height: 50, color: myColors2?.brandColor), // ! | |
const SizedBox(width: 10), | |
Container(width: 50, height: 50, color: myColors2?.danger), // ! | |
const SizedBox(width: 50), | |
IconButton( | |
icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny), | |
onPressed: toggleTheme, | |
), | |
// 3. | |
Container(width: 50, height: 50, color: myColors3?.brandColor), | |
const SizedBox(width: 10), | |
Container(width: 50, height: 50, color: myColors3?.danger), | |
const SizedBox(width: 50), | |
IconButton( | |
icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny), | |
onPressed: toggleTheme, | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
@immutable | |
class MyColors extends ThemeExtension<MyColors> { | |
const MyColors({ | |
required this.brandColor, | |
required this.danger, | |
}); | |
final Color? brandColor; | |
final Color? danger; | |
@override | |
MyColors copyWith({Color? brandColor, Color? danger}) { | |
return MyColors( | |
brandColor: brandColor ?? this.brandColor, | |
danger: danger ?? this.danger, | |
); | |
} | |
@override | |
MyColors lerp(ThemeExtension<MyColors>? other, double t) { | |
if (other is! MyColors) { | |
return this; | |
} | |
return MyColors( | |
brandColor: Color.lerp(brandColor, other.brandColor, t), | |
danger: Color.lerp(danger, other.danger, t), | |
); | |
} | |
@override | |
String toString() => 'MyColors(brandColor: $brandColor, danger: $danger)'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment