Unverified Commit 2d89866c authored by Pierre-Louis's avatar Pierre-Louis Committed by GitHub

Minor improvements to `ThemeExtension` example (#100693)

* Update theme_extension.1.dart

* empty commit

* update color names to be semantic
parent 66ed64be
...@@ -10,18 +10,18 @@ import 'package:flutter/scheduler.dart'; ...@@ -10,18 +10,18 @@ import 'package:flutter/scheduler.dart';
@immutable @immutable
class MyColors extends ThemeExtension<MyColors> { class MyColors extends ThemeExtension<MyColors> {
const MyColors({ const MyColors({
required this.blue, required this.brandColor,
required this.red, required this.danger,
}); });
final Color? blue; final Color? brandColor;
final Color? red; final Color? danger;
@override @override
MyColors copyWith({Color? red, Color? blue}) { MyColors copyWith({Color? brandColor, Color? danger}) {
return MyColors( return MyColors(
blue: blue ?? this.blue, brandColor: brandColor ?? this.brandColor,
red: red ?? this.red, danger: danger ?? this.danger,
); );
} }
...@@ -31,14 +31,14 @@ class MyColors extends ThemeExtension<MyColors> { ...@@ -31,14 +31,14 @@ class MyColors extends ThemeExtension<MyColors> {
return this; return this;
} }
return MyColors( return MyColors(
blue: Color.lerp(blue, other.blue, t), brandColor: Color.lerp(brandColor, other.brandColor, t),
red: Color.lerp(red, other.red, t), danger: Color.lerp(danger, other.danger, t),
); );
} }
// Optional // Optional
@override @override
String toString() => 'MyColors(blue: $blue, red: $red)'; String toString() => 'MyColors(brandColor: $brandColor, danger: $danger)';
} }
void main() { void main() {
...@@ -68,20 +68,20 @@ class _MyAppState extends State<MyApp> { ...@@ -68,20 +68,20 @@ class _MyAppState extends State<MyApp> {
return MaterialApp( return MaterialApp(
title: MyApp._title, title: MyApp._title,
theme: ThemeData.light().copyWith( theme: ThemeData.light().copyWith(
extensions: <ThemeExtension<dynamic>>{ extensions: <ThemeExtension<dynamic>>[
const MyColors( const MyColors(
blue: Color(0xFF1E88E5), brandColor: Color(0xFF1E88E5),
red: Color(0xFFE53935), danger: Color(0xFFE53935),
), ),
}, ],
), ),
darkTheme: ThemeData.dark().copyWith( darkTheme: ThemeData.dark().copyWith(
extensions: <ThemeExtension<dynamic>>{ extensions: <ThemeExtension<dynamic>>[
const MyColors( const MyColors(
blue: Color(0xFF90CAF9), brandColor: Color(0xFF90CAF9),
red: Color(0xFFEF9A9A), danger: Color(0xFFEF9A9A),
), ),
}, ],
), ),
themeMode: isLightTheme ? ThemeMode.light : ThemeMode.dark, themeMode: isLightTheme ? ThemeMode.light : ThemeMode.dark,
home: Home( home: Home(
...@@ -110,9 +110,9 @@ class Home extends StatelessWidget { ...@@ -110,9 +110,9 @@ class Home extends StatelessWidget {
child: Row( child: Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Container(width: 100, height: 100, color: myColors.blue), Container(width: 100, height: 100, color: myColors.brandColor),
const SizedBox(width: 10), const SizedBox(width: 10),
Container(width: 100, height: 100, color: myColors.red), Container(width: 100, height: 100, color: myColors.danger),
const SizedBox(width: 50), const SizedBox(width: 50),
IconButton( IconButton(
icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny), icon: Icon(isLightTheme ? Icons.nightlight : Icons.wb_sunny),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment