Enhance ColorScheme.fromSeed with a new variant parameter (#144805)

Fixes #144649

This PR is to add an enhancement for `ColorScheme.fromSeed()`.  When we input a brighter color with a high chroma and we want the resulting `ColorScheme` respects the source color, we can achieve this by setting

```dart
ColorScheme.fromSeed(
  seedColor: sourceColor,
  variant: Variant.fidelity,
  ...
)
```

Here is a demo for `ColorScheme` constructed by all 9 variants:
![Screenshot 2024-03-13 at 4 33 02 PM](https://github.com/flutter/flutter/assets/36861262/2cdbe559-67ff-4f8b-bd48-ccff5131b949)
![Screenshot 2024-03-13 at 4 33 07 PM](https://github.com/flutter/flutter/assets/36861262/e6af2362-c501-499b-8880-06a321de91e6)
This commit is contained in:
Ian Hickson
2024-04-03 08:54:34 -07:00
committed by GitHub
parent a46ed4ee07
commit 950ec1c405
4 changed files with 348 additions and 100 deletions

View File

@@ -19,95 +19,93 @@ class ColorSchemeExample extends StatefulWidget {
class _ColorSchemeExampleState extends State<ColorSchemeExample> {
Color selectedColor = ColorSeed.baseColor.color;
Brightness selectedBrightness = Brightness.light;
static const List<DynamicSchemeVariant> schemeVariants = DynamicSchemeVariant.values;
@override
Widget build(BuildContext context) {
final Color? colorSeed = selectedColor == ColorSeed.baseColor.color ? null : selectedColor;
final ThemeData lightTheme = ThemeData(
colorSchemeSeed: colorSeed,
brightness: Brightness.light,
);
final ThemeData darkTheme = ThemeData(
colorSchemeSeed: colorSeed,
brightness: Brightness.dark,
);
Widget schemeLabel(String brightness) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 15),
child: Text(
brightness,
style: const TextStyle(fontWeight: FontWeight.bold),
),
);
}
Widget schemeView(ThemeData theme) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: ColorSchemeView(colorScheme: theme.colorScheme),
);
}
return MaterialApp(
theme: ThemeData(colorSchemeSeed: selectedColor),
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: selectedColor,
brightness: selectedBrightness,
)
),
home: Builder(
builder: (BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('ColorScheme'),
leading: MenuAnchor(
builder: (BuildContext context, MenuController controller, Widget? widget) {
return IconButton(
icon: Icon(Icons.circle, color: selectedColor),
onPressed: () {
setState(() {
if (!controller.isOpen) {
controller.open();
}
});
},
);
},
menuChildren: List<Widget>.generate(ColorSeed.values.length, (int index) {
final Color itemColor = ColorSeed.values[index].color;
return MenuItemButton(
leadingIcon: selectedColor == ColorSeed.values[index].color
? Icon(Icons.circle, color: itemColor)
: Icon(Icons.circle_outlined, color: itemColor),
onPressed: () {
setState(() {
selectedColor = itemColor;
});
},
child: Text(ColorSeed.values[index].label),
);
}),
),
actions: <Widget>[
Row(
children: <Widget>[
const Text('Color Seed'),
MenuAnchor(
builder: (BuildContext context, MenuController controller, Widget? widget) {
return IconButton(
icon: Icon(Icons.circle, color: selectedColor),
onPressed: () {
setState(() {
if (!controller.isOpen) {
controller.open();
}
});
},
);
},
menuChildren: List<Widget>.generate(ColorSeed.values.length, (int index) {
final Color itemColor = ColorSeed.values[index].color;
return MenuItemButton(
leadingIcon: selectedColor == ColorSeed.values[index].color
? Icon(Icons.circle, color: itemColor)
: Icon(Icons.circle_outlined, color: itemColor),
onPressed: () {
setState(() {
selectedColor = itemColor;
});
},
child: Text(ColorSeed.values[index].label),
);
}),
),
],
),
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Column(
children: <Widget>[
schemeLabel('Light ColorScheme'),
schemeView(lightTheme),
],
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Row(
children: <Widget>[
const Text('Brightness'),
const SizedBox(width: 10),
Switch(
value: selectedBrightness == Brightness.light,
onChanged: (bool value) {
setState(() {
selectedBrightness = value ? Brightness.light : Brightness.dark;
});
},
),
),
Expanded(
child: Column(
children: <Widget>[
schemeLabel('Dark ColorScheme'),
schemeView(darkTheme),
],
),
),
],
],
),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List<Widget>.generate(schemeVariants.length, (int index) {
return ColorSchemeVariantColumn(
selectedColor: selectedColor,
brightness: selectedBrightness,
schemeVariant: schemeVariants[index],
);
}).toList(),
),
),
],
),
@@ -119,6 +117,47 @@ class _ColorSchemeExampleState extends State<ColorSchemeExample> {
}
}
class ColorSchemeVariantColumn extends StatelessWidget {
const ColorSchemeVariantColumn({
super.key,
this.schemeVariant = DynamicSchemeVariant.tonalSpot,
this.brightness = Brightness.light,
required this.selectedColor,
});
final DynamicSchemeVariant schemeVariant;
final Brightness brightness;
final Color selectedColor;
@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: const BoxConstraints.tightFor(width: 250),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 15),
child: Text(
schemeVariant.name == 'tonalSpot' ? '${schemeVariant.name} (Default)' : schemeVariant.name,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: ColorSchemeView(
colorScheme: ColorScheme.fromSeed(
seedColor: selectedColor,
brightness: brightness,
dynamicSchemeVariant: schemeVariant,
),
),
),
],
)
);
}
}
class ColorSchemeView extends StatelessWidget {
const ColorSchemeView({super.key, required this.colorScheme});
@@ -301,7 +340,10 @@ enum ColorSeed {
yellow('Yellow', Colors.yellow),
orange('Orange', Colors.orange),
deepOrange('Deep Orange', Colors.deepOrange),
pink('Pink', Colors.pink);
pink('Pink', Colors.pink),
brightBlue('Bright Blue', Color(0xFF0000FF)),
brightGreen('Bright Green', Color(0xFF00FF00)),
brightRed('Bright Red', Color(0xFFFF0000));
const ColorSeed(this.label, this.color);
final String label;

View File

@@ -11,10 +11,9 @@ void main() {
await tester.pumpWidget(
const example.ColorSchemeExample(),
);
expect(find.text('Light ColorScheme'), findsOneWidget);
expect(find.text('Dark ColorScheme'), findsOneWidget);
expect(find.text('tonalSpot (Default)'), findsOneWidget);
expect(find.byType(example.ColorChip), findsNWidgets(86));
expect(find.byType(example.ColorChip), findsNWidgets(43 * 9));
});
testWidgets('Change color seed', (WidgetTester tester) async {
@@ -30,7 +29,7 @@ void main() {
)
);
}
expect(coloredBox().color, const Color(0xFF6750A4));
expect(coloredBox().color, const Color(0xff65558f));
await tester.tap(find.byType(MenuAnchor));
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(MenuItemButton, 'Yellow'));