Customizing Widget Styles
Customize individual widget styles using deltas and the CLI.
There are 2 ways to customize individual widget styles: deltas for small tweaks and the CLI for major overhauls.
Deltas
All widget styles have corresponding deltas that represent a partial modification. For example,
FAccordionStyle has a
corresponding FAccordionStyleDelta.
Widgets accept deltas via their style parameter. To change an accordion's focused outline color while preserving the
rest of the style:
1FAccordion(2 style: .delta(focusedOutlineStyle: .delta(color: Colors.blue)),3 children: [],4);5Since widget styles extend their corresponding deltas, you can also pass a full style to replace the existing one entirely:
1FAccordion(2 // Replace the entire style with a new one based on the green theme.3 style: FAccordionStyle.inherit(4 colors: FColors.zinc.light.withPrimary(FPrimaryColors.green.light),5 typography: FThemeData(6 touch: true,7 colors: FColors.zinc.light.withPrimary(FPrimaryColors.green.light),8 ).typography,9 style: FThemeData(10 touch: true,11 colors: FColors.zinc.light.withPrimary(FPrimaryColors.green.light),12 ).style,13 touch: true,14 ),15 children: const [],16);17The following built-in Flutter types also have corresponding deltas:
Variants
Widget styles contain FVariants fields. They can be modified using a delta(...)
function that accepts a sequence of operations. Depending on the variant's type, operations are either delta-based
modifications or full replacements.
There are 3 ways to modify FVariants fields:
Composite types such as BoxDecoration, IconThemeData, TextStyle, and nested styles support delta-based modifications.
To modify an accordion's title text style:
1FAccordion(2 style: .delta(3 titleTextStyle: .delta([4 // Change ONLY the base title color.5 .base(const .delta(color: Colors.red)),6 // Make hovered AND focused titles underlined, adds a new constraint7 // if it doesn't already exist.8 .exact({9 .hovered.and(.focused),10 }, .delta(decoration: () => .underline)),11 // Make all existing constraints containing hovered bold, does12 // nothing if none exist.13 .match({.hovered}, const .delta(fontWeight: .bold)),14 ]),15 ),16 children: const [],17);18Atomic types such as double and Color only support full replacements. To modify a radio's border color:
1FRadio(2 style: .delta(3 borderColor: .delta([4 // Change ONLY the base border color.5 .base(Colors.blue),6 // Set the border color when selected, adds a new constraint7 // if it doesn't already exist.8 .exact({.selected}, Colors.green),9 // Set the border color for all existing constraints containing10 // disabled, does nothing if none exist.11 .match({.disabled}, Colors.grey),12 ]),13 ),14);15Like widget styles, FVariants can also be replaced entirely by passing an FVariants directly. To replace an
accordion's title text style:
1FAccordion(2 style: .delta(3 titleTextStyle: FVariants.from(4 const TextStyle(fontSize: 18, fontWeight: .bold),5 variants: {6 [.hovered, .pressed]: .delta(7 decoration: () => TextDecoration.underline,8 ),9 [.disabled]: const .delta(color: Colors.grey),10 },11 ),12 ),13 children: const [],14);15CLI
The following section demonstrates how to override an accordion's style. This is recommended for major style overhauls.
Run to generate a widget style:
dart run forui style create accordionTip: Run dart run forui style ls to see all available styles.
To add an underline to the accordion title when focused, modify the generated style:
1FAccordionStyle accordionStyle({2 required FColors colors,3 required FTypography typography,4 required FStyle style,5}) => FAccordionStyle(6 titleTextStyle: FVariants.from(7 // This text style is applied when the accordion is NOT hovered OR pressed.8 typography.md.copyWith(fontWeight: .w500, color: colors.foreground),9 variants: {10 // This text style is applied when the accordion is hovered OR pressed OR focused (new).11 [.hovered, .pressed, .focused]: .delta(12 decoration: () => TextDecoration.underline,13 ),14 },15 ),16);17Lastly, pass the modified style to an FAccordion:
12@override3Widget build(BuildContext context) => FAccordion(4 // Pass the modified style to the widget.5 style: accordionStyle(6 colors: context.theme.colors,7 typography: context.theme.typography,8 style: context.theme.style,9 ),10 children: const [11 FAccordionItem(12 title: Text('Is it accessible?'),13 child: Text('Yes. It adheres to the WAI-ARIA design pattern.'),14 ),15 ],16);17