Skip to content

Instantly share code, notes, and snippets.

@rydmike

rydmike/main.dart

Created Jan 16, 2021
Embed
What would you like to do?
Example for FlexColorScheme issue #2: https://github.com/rydmike/flex_color_scheme/issues/2
import 'package:flutter/material.dart';
import 'package:flex_color_scheme/flex_color_scheme.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Example 1)
// The default Material Design Guide Theme from its color scheme.
ThemeData fromMaterialScheme =
ThemeData.from(colorScheme: ColorScheme.light());
// Example 2)
// A FlexColorScheme based theme from one of its built in color schemes.
ThemeData fromFlexScheme = FlexColorScheme.light(
colors: FlexColor.schemes[FlexScheme.mandyRed].light,
).toTheme;
// Example 3)
// Modified the above scheme and make some customization, keeping
// all other properties as their where. In this case we change
// the theme of the FAB to be primary color based.
ThemeData myModifiedFlexScheme = fromFlexScheme.copyWith(
floatingActionButtonTheme: fromFlexScheme.floatingActionButtonTheme
.copyWith(
backgroundColor: fromFlexScheme.colorScheme.primary,
foregroundColor: fromFlexScheme.colorScheme.onPrimary));
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
// Uncomment the theme you want to try.
theme: fromMaterialScheme, // Example 1
// theme: fromFlexScheme, // Example 2
// theme: myModifiedFlexScheme, // Example 3
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
children: <Widget>[
Center(
child: Text(
'Active Theme Colors',
style: Theme.of(context).textTheme.headline4,
),
),
Center(child: ShowThemeColors()),
SizedBox(height: 10),
Center(
child: Text(
'You have pushed the button this many times:',
),
),
Center(
child: Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
),
SizedBox(height: 10),
Center(
child: Text(
'Theme.colorScheme.primary: ${Theme.of(context).colorScheme.primary}',
),
),
Center(
child: Text(
'Theme.primaryColor: ${Theme.of(context).primaryColor}',
),
),
SizedBox(height: 10),
Center(
child: Text(
'Theme.colorScheme.secondary: ${Theme.of(context).colorScheme.secondary}',
),
),
Center(
child: Text(
'Theme.accentColor: ${Theme.of(context).accentColor}',
),
),
SizedBox(height: 10),
Center(
child: Text(
'Theme.FABTheme.backgroundColor: ${Theme.of(context).floatingActionButtonTheme.backgroundColor}',
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
// Draw a number of boxes showing the colors of key theme color properties
// in the ColorScheme of the inherited ThemeData and some of its key color
// properties.
// This widget is just used so we can visually see the active theme colors
// in the examples and their used FlexColorScheme based themes.
class ShowThemeColors extends StatelessWidget {
const ShowThemeColors({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
final ColorScheme colorScheme = theme.colorScheme;
final Color appBarColor = theme.appBarTheme.color ?? theme.primaryColor;
// A Wrap widget is just the right handy widget for this type of
// widget to make it responsive.
return Wrap(
spacing: 4,
runSpacing: 4,
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
ThemeCard(
label: 'Primary',
color: colorScheme.primary,
textColor: colorScheme.onPrimary,
),
ThemeCard(
label: 'Primary\nColor',
color: theme.primaryColor,
textColor: theme.primaryTextTheme.subtitle1.color,
),
ThemeCard(
label: 'Primary\nColorDark',
color: theme.primaryColorDark,
textColor:
ThemeData.estimateBrightnessForColor(theme.primaryColorDark) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Primary\nColorLight',
color: theme.primaryColorLight,
textColor:
ThemeData.estimateBrightnessForColor(theme.primaryColorLight) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Secondary\nHeader',
color: theme.secondaryHeaderColor,
textColor: ThemeData.estimateBrightnessForColor(
theme.secondaryHeaderColor) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Primary\nVariant',
color: colorScheme.primaryVariant,
textColor: ThemeData.estimateBrightnessForColor(
colorScheme.primaryVariant) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Secondary',
color: colorScheme.secondary,
textColor: colorScheme.onSecondary,
),
ThemeCard(
label: 'Accent\nColor',
color: theme.accentColor,
textColor: colorScheme.onPrimary,
),
ThemeCard(
label: 'Toggleable\nActive',
color: theme.toggleableActiveColor,
textColor: ThemeData.estimateBrightnessForColor(
theme.toggleableActiveColor) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Secondary\nVariant',
color: colorScheme.secondaryVariant,
textColor: ThemeData.estimateBrightnessForColor(
colorScheme.secondaryVariant) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'AppBar',
color: appBarColor,
textColor: ThemeData.estimateBrightnessForColor(appBarColor) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Bottom\nAppBar',
color: theme.bottomAppBarColor,
textColor:
ThemeData.estimateBrightnessForColor(theme.bottomAppBarColor) ==
Brightness.dark
? Colors.white
: Colors.black,
),
ThemeCard(
label: 'Divider',
color: theme.dividerColor,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Background',
color: colorScheme.background,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Canvas',
color: theme.canvasColor,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Surface',
color: colorScheme.surface,
textColor: colorScheme.onSurface,
),
ThemeCard(
label: 'Card',
color: theme.cardColor,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Dialog',
color: theme.dialogBackgroundColor,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Scaffold\nbackground',
color: theme.scaffoldBackgroundColor,
textColor: colorScheme.onBackground,
),
ThemeCard(
label: 'Error',
color: colorScheme.error,
textColor: colorScheme.onError,
),
],
);
}
}
// This is just simple SizedBox with a Card with a passed in label, background
// and text label color. Used to show the colors of a theme color property.
class ThemeCard extends StatelessWidget {
const ThemeCard({
Key key,
this.label,
this.color,
this.textColor,
}) : super(key: key);
final String label;
final Color color;
final Color textColor;
@override
Widget build(BuildContext context) {
return SizedBox(
height: 50,
width: 85,
child: Card(
margin: const EdgeInsets.all(0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),
side: BorderSide(
color: Theme.of(context).dividerColor,
),
),
elevation: 0,
color: color,
child: Center(
child: Text(
label,
style: TextStyle(color: textColor, fontSize: 12),
textAlign: TextAlign.center,
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment