| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- // Copyright 2021 The Flutter team. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- import 'package:flutter/gestures.dart';
- import 'package:flutter/material.dart';
- import 'package:url_launcher/url_launcher.dart';
- const Widget divider = SizedBox(height: 10);
- // If screen content width is greater or equal to this value, the light and dark
- // color schemes will be displayed in a column. Otherwise, they will
- // be displayed in a row.
- const double narrowScreenWidthThreshold = 400;
- class ColorPalettesScreen extends StatelessWidget {
- const ColorPalettesScreen({super.key});
- @override
- Widget build(BuildContext context) {
- Color selectedColor = Theme.of(context).primaryColor;
- ThemeData lightTheme = ThemeData(
- colorSchemeSeed: selectedColor,
- brightness: Brightness.light,
- );
- ThemeData darkTheme = ThemeData(
- colorSchemeSeed: selectedColor,
- 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,
- ),
- );
- }
- Widget dynamicColorNotice() => RichText(
- textAlign: TextAlign.center,
- text: TextSpan(
- style: Theme.of(context).textTheme.bodySmall,
- children: [
- const TextSpan(
- text: 'To create color schemes based on a '
- 'platform\'s implementation of dynamic color, '
- 'use the '),
- TextSpan(
- text: 'dynamic_color',
- style: const TextStyle(decoration: TextDecoration.underline),
- recognizer: TapGestureRecognizer()
- ..onTap = () async {
- final url = Uri.parse(
- 'https://pub.dev/packages/dynamic_color',
- );
- if (!await launchUrl(url)) {
- throw Exception('Could not launch $url');
- }
- },
- ),
- const TextSpan(text: ' package.'),
- ],
- ),
- );
- return Expanded(
- child: LayoutBuilder(builder: (context, constraints) {
- if (constraints.maxWidth < narrowScreenWidthThreshold) {
- return SingleChildScrollView(
- child: Column(
- children: [
- dynamicColorNotice(),
- divider,
- schemeLabel('Light ColorScheme'),
- schemeView(lightTheme),
- divider,
- divider,
- schemeLabel('Dark ColorScheme'),
- schemeView(darkTheme),
- ],
- ),
- );
- } else {
- return SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.only(top: 5),
- child: Column(
- children: [
- dynamicColorNotice(),
- Row(
- children: [
- Expanded(
- child: Column(
- children: [
- schemeLabel('Light ColorScheme'),
- schemeView(lightTheme),
- ],
- ),
- ),
- Expanded(
- child: Column(
- children: [
- schemeLabel('Dark ColorScheme'),
- schemeView(darkTheme),
- ],
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- );
- }
- }),
- );
- }
- }
- class ColorSchemeView extends StatelessWidget {
- const ColorSchemeView({super.key, required this.colorScheme});
- final ColorScheme colorScheme;
- @override
- Widget build(BuildContext context) {
- return Column(
- children: [
- ColorGroup(children: [
- ColorChip(
- label: 'primary',
- color: colorScheme.primary,
- onColor: colorScheme.onPrimary,
- ),
- ColorChip(
- label: 'onPrimary',
- color: colorScheme.onPrimary,
- onColor: colorScheme.primary),
- ColorChip(
- label: 'primaryContainer',
- color: colorScheme.primaryContainer,
- onColor: colorScheme.onPrimaryContainer,
- ),
- ColorChip(
- label: 'onPrimaryContainer',
- color: colorScheme.onPrimaryContainer,
- onColor: colorScheme.primaryContainer,
- ),
- ]),
- divider,
- ColorGroup(children: [
- ColorChip(
- label: 'secondary',
- color: colorScheme.secondary,
- onColor: colorScheme.onSecondary,
- ),
- ColorChip(
- label: 'onSecondary',
- color: colorScheme.onSecondary,
- onColor: colorScheme.secondary,
- ),
- ColorChip(
- label: 'secondaryContainer',
- color: colorScheme.secondaryContainer,
- onColor: colorScheme.onSecondaryContainer,
- ),
- ColorChip(
- label: 'onSecondaryContainer',
- color: colorScheme.onSecondaryContainer,
- onColor: colorScheme.secondaryContainer),
- ]),
- divider,
- ColorGroup(
- children: [
- ColorChip(
- label: 'tertiary',
- color: colorScheme.tertiary,
- onColor: colorScheme.onTertiary),
- ColorChip(
- label: 'onTertiary',
- color: colorScheme.onTertiary,
- onColor: colorScheme.tertiary),
- ColorChip(
- label: 'tertiaryContainer',
- color: colorScheme.tertiaryContainer,
- onColor: colorScheme.onTertiaryContainer),
- ColorChip(
- label: 'onTertiaryContainer',
- color: colorScheme.onTertiaryContainer,
- onColor: colorScheme.tertiaryContainer),
- ],
- ),
- divider,
- ColorGroup(
- children: [
- ColorChip(
- label: 'error',
- color: colorScheme.error,
- onColor: colorScheme.onError),
- ColorChip(
- label: 'onError',
- color: colorScheme.onError,
- onColor: colorScheme.error),
- ColorChip(
- label: 'errorContainer',
- color: colorScheme.errorContainer,
- onColor: colorScheme.onErrorContainer),
- ColorChip(
- label: 'onErrorContainer',
- color: colorScheme.onErrorContainer,
- onColor: colorScheme.errorContainer),
- ],
- ),
- divider,
- ColorGroup(
- children: [
- ColorChip(
- label: 'background',
- color: colorScheme.background,
- onColor: colorScheme.onBackground),
- ColorChip(
- label: 'onBackground',
- color: colorScheme.onBackground,
- onColor: colorScheme.background),
- ],
- ),
- divider,
- ColorGroup(
- children: [
- ColorChip(
- label: 'surface',
- color: colorScheme.surface,
- onColor: colorScheme.onSurface),
- ColorChip(
- label: 'onSurface',
- color: colorScheme.onSurface,
- onColor: colorScheme.surface),
- ColorChip(
- label: 'surfaceVariant',
- color: colorScheme.surfaceVariant,
- onColor: colorScheme.onSurfaceVariant),
- ColorChip(
- label: 'onSurfaceVariant',
- color: colorScheme.onSurfaceVariant,
- onColor: colorScheme.surfaceVariant),
- ],
- ),
- divider,
- ColorGroup(
- children: [
- ColorChip(label: 'outline', color: colorScheme.outline),
- ColorChip(label: 'shadow', color: colorScheme.shadow),
- ColorChip(
- label: 'inverseSurface',
- color: colorScheme.inverseSurface,
- onColor: colorScheme.onInverseSurface),
- ColorChip(
- label: 'onInverseSurface',
- color: colorScheme.onInverseSurface,
- onColor: colorScheme.inverseSurface),
- ColorChip(
- label: 'inversePrimary',
- color: colorScheme.inversePrimary,
- onColor: colorScheme.primary),
- ],
- ),
- ],
- );
- }
- }
- class ColorGroup extends StatelessWidget {
- const ColorGroup({super.key, required this.children});
- final List<Widget> children;
- @override
- Widget build(BuildContext context) {
- return RepaintBoundary(
- child: Card(
- clipBehavior: Clip.antiAlias,
- child: Column(
- children: children,
- ),
- ),
- );
- }
- }
- class ColorChip extends StatelessWidget {
- const ColorChip({
- super.key,
- required this.color,
- required this.label,
- this.onColor,
- });
- final Color color;
- final Color? onColor;
- final String label;
- static Color contrastColor(Color color) {
- final brightness = ThemeData.estimateBrightnessForColor(color);
- switch (brightness) {
- case Brightness.dark:
- return Colors.white;
- case Brightness.light:
- return Colors.black;
- }
- }
- @override
- Widget build(BuildContext context) {
- final Color labelColor = onColor ?? contrastColor(color);
- return Container(
- color: color,
- child: Padding(
- padding: const EdgeInsets.all(16),
- child: Row(
- children: [
- Expanded(child: Text(label, style: TextStyle(color: labelColor))),
- ],
- ),
- ),
- );
- }
- }
|