Files
pipelines/app/lib/ui/theme/high_contrast_controls.dart
T
2026-12-03 23:56:39 +00:00

346 lines
13 KiB
Dart

import 'package:flutter/material.dart';
import 'high_contrast_palette.dart';
import 'high_contrast_theme.dart';
const Color _clear = Color(0x00000000);
/// The state resolvers High contrast's controls use, built once per
/// brightness: a resolver compares by identity, so fresh ones would make a
/// rebuilt theme unequal to the last (test/perf/design_bench_test.dart).
class _Resolvers {
_Resolvers(ColorScheme scheme)
: disabled = highContrastDisabled(scheme.brightness) {
final ink = scheme.onSurface;
BorderSide side(Color color) =>
BorderSide(color: color, width: highContrastStroke);
outline = WidgetStateProperty.resolveWith(
(states) => side(states.contains(WidgetState.disabled) ? disabled : ink),
);
filledSide = WidgetStateProperty.resolveWith(
(states) => side(
states.contains(WidgetState.disabled) ? disabled : scheme.primary,
),
);
filledBackground = WidgetStateProperty.resolveWith(
(states) =>
states.contains(WidgetState.disabled) ? _clear : scheme.primary,
);
filledForeground = _foreground(scheme.onPrimary);
primaryForeground = _foreground(scheme.primary);
inkForeground = _foreground(ink);
chipSide = WidgetStateBorderSide.resolveWith(
(states) => side(states.contains(WidgetState.disabled) ? disabled : ink),
);
segmentBackground = WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.selected)
? scheme.secondaryContainer
: _clear,
);
segmentForeground = WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.disabled)
? disabled
: states.contains(WidgetState.selected)
? scheme.onSecondaryContainer
: ink,
);
checkboxSide = WidgetStateBorderSide.resolveWith(
(states) => side(
states.contains(WidgetState.disabled)
? disabled
: states.contains(WidgetState.selected)
? scheme.primary
: ink,
),
);
trackOutline = WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.disabled)
? disabled
: states.contains(WidgetState.selected)
? scheme.primary
: ink,
);
selectedInk = WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.disabled)
? disabled
: states.contains(WidgetState.selected)
? scheme.primary
: ink,
);
// One border resolved by state (the field's own `InputBorder.none`
// replaces it whole): 3px of the accent while focused, the error red,
// the disabled ink, else 2px of the ink.
field = WidgetStateInputBorder.resolveWith((states) {
final color = states.contains(WidgetState.disabled)
? disabled
: states.contains(WidgetState.error)
? scheme.error
: states.contains(WidgetState.focused)
? scheme.primary
: ink;
return OutlineInputBorder(
borderRadius: HighContrastSurfaces.corners,
borderSide: BorderSide(
color: color,
width: states.contains(WidgetState.focused)
? highContrastFocusWidth
: highContrastStroke,
),
);
});
overlay = WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.pressed)) {
return ink.withValues(alpha: 0.2);
}
if (states.contains(WidgetState.focused)) {
return scheme.primary.withValues(alpha: 0.24);
}
if (states.contains(WidgetState.hovered)) {
return ink.withValues(alpha: 0.12);
}
return null;
});
}
final Color disabled;
late final WidgetStateProperty<BorderSide> outline;
late final WidgetStateProperty<BorderSide> filledSide;
late final WidgetStateProperty<Color> filledBackground;
late final WidgetStateProperty<Color> filledForeground;
late final WidgetStateProperty<Color> primaryForeground;
late final WidgetStateProperty<Color> inkForeground;
late final WidgetStateBorderSide chipSide;
late final WidgetStateProperty<Color> segmentBackground;
late final WidgetStateProperty<Color> segmentForeground;
late final WidgetStateBorderSide checkboxSide;
late final WidgetStateProperty<Color> trackOutline;
late final WidgetStateProperty<Color> selectedInk;
late final WidgetStateProperty<Color?> overlay;
late final InputBorder field;
/// [color], or the disabled ink (4.5:1 or more) on a disabled control.
WidgetStateProperty<Color> _foreground(Color color) =>
WidgetStateProperty.resolveWith(
(states) => states.contains(WidgetState.disabled) ? disabled : color,
);
}
final Map<Brightness, _Resolvers> _resolvers = {};
/// [base] with High contrast's controls: every button, field, chip, menu,
/// segmented button, checkbox, switch and slider 48px tall at least, in
/// [highContrastRadius] corners with 2px borders, no elevation, disabled
/// text that still reads at 4.5:1 and the selected state inverted.
/// Text and icon buttons stay unframed (an action in a sentence or a
/// toolbar), with 48px targets, strong hover and the focus ring.
///
/// Button text keeps the theme's own `labelLarge` (16px, weight 600): a
/// button style's text style is animated when the design switches, and a
/// style from the unlocalized text theme cannot blend with the default.
ThemeData highContrastControls(ThemeData base, HighContrastSurfaces surfaces) {
final scheme = base.colorScheme;
final r = _resolvers[scheme.brightness] ??= _Resolvers(scheme);
final text = base.textTheme;
const rounded = HighContrastSurfaces.rounded;
const minimum = WidgetStatePropertyAll(Size(48, 48));
ButtonStyle buttons(ButtonStyle? style) =>
(style ?? const ButtonStyle()).copyWith(
shape: const WidgetStatePropertyAll(rounded),
elevation: const WidgetStatePropertyAll(0),
shadowColor: const WidgetStatePropertyAll(_clear),
minimumSize: minimum,
overlayColor: r.overlay,
padding: const WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: 20, vertical: 10),
),
);
final menu = MenuStyle(
backgroundColor: WidgetStatePropertyAll(scheme.surface),
elevation: const WidgetStatePropertyAll(0),
shadowColor: const WidgetStatePropertyAll(_clear),
surfaceTintColor: const WidgetStatePropertyAll(_clear),
shape: WidgetStatePropertyAll(surfaces.framed),
padding: const WidgetStatePropertyAll(EdgeInsets.symmetric(vertical: 6)),
);
return base.copyWith(
menuTheme: MenuThemeData(style: menu),
dropdownMenuTheme: DropdownMenuThemeData(
menuStyle: menu,
textStyle: text.bodyLarge,
),
menuButtonTheme: MenuButtonThemeData(
style: MenuItemButton.styleFrom(
shape: rounded,
minimumSize: const Size(48, 48),
padding: const EdgeInsets.symmetric(horizontal: 16),
).copyWith(overlayColor: r.overlay),
),
filledButtonTheme: FilledButtonThemeData(
style: buttons(base.filledButtonTheme.style).copyWith(
backgroundColor: r.filledBackground,
foregroundColor: r.filledForeground,
side: r.filledSide,
),
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: buttons(base.elevatedButtonTheme.style).copyWith(
backgroundColor: WidgetStatePropertyAll(scheme.surface),
foregroundColor: r.primaryForeground,
side: r.outline,
),
),
outlinedButtonTheme: OutlinedButtonThemeData(
style: buttons(
base.outlinedButtonTheme.style,
).copyWith(side: r.outline, foregroundColor: r.primaryForeground),
),
textButtonTheme: TextButtonThemeData(
style: buttons(base.textButtonTheme.style).copyWith(
foregroundColor: r.primaryForeground,
padding: const WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: 12, vertical: 10),
),
),
),
iconButtonTheme: IconButtonThemeData(
style: (base.iconButtonTheme.style ?? const ButtonStyle()).copyWith(
shape: const WidgetStatePropertyAll(rounded),
minimumSize: minimum,
overlayColor: r.overlay,
foregroundColor: r.inkForeground,
),
),
segmentedButtonTheme: SegmentedButtonThemeData(
style: buttons(base.segmentedButtonTheme.style).copyWith(
side: r.outline,
backgroundColor: r.segmentBackground,
foregroundColor: r.segmentForeground,
iconColor: r.segmentForeground,
padding: const WidgetStatePropertyAll(
EdgeInsets.symmetric(horizontal: 12),
),
),
),
floatingActionButtonTheme: FloatingActionButtonThemeData(
elevation: 0,
focusElevation: 0,
hoverElevation: 0,
highlightElevation: 0,
backgroundColor: scheme.primary,
foregroundColor: scheme.onPrimary,
shape: RoundedRectangleBorder(
borderRadius: HighContrastSurfaces.corners,
side: BorderSide(color: scheme.onSurface, width: highContrastStroke),
),
),
// Chosen chips inverted; every chip framed in the ink.
chipTheme: ChipThemeData(
backgroundColor: _clear,
selectedColor: scheme.secondaryContainer,
disabledColor: _clear,
checkmarkColor: scheme.onSecondaryContainer,
shape: rounded,
side: r.chipSide,
elevation: 0,
pressElevation: 0,
labelStyle: text.labelLarge?.copyWith(color: scheme.onSurface),
secondaryLabelStyle: text.labelLarge?.copyWith(
color: scheme.onSecondaryContainer,
),
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 6),
),
// Only the base border: a field that sets `InputBorder.none` (the
// composer, whose frame is its pill) keeps none in every state.
inputDecorationTheme: InputDecorationTheme(
border: r.field,
contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 14),
labelStyle: text.bodyLarge?.copyWith(color: scheme.onSurfaceVariant),
hintStyle: text.bodyLarge?.copyWith(color: scheme.onSurfaceVariant),
errorStyle: text.bodySmall?.copyWith(color: scheme.error),
),
searchBarTheme: SearchBarThemeData(
elevation: const WidgetStatePropertyAll(0),
shape: const WidgetStatePropertyAll(rounded),
side: WidgetStatePropertyAll(surfaces.border),
backgroundColor: WidgetStatePropertyAll(scheme.surface),
surfaceTintColor: const WidgetStatePropertyAll(_clear),
overlayColor: r.overlay,
constraints: const BoxConstraints(minHeight: 48),
textStyle: WidgetStatePropertyAll(text.bodyLarge),
hintStyle: WidgetStatePropertyAll(
text.bodyLarge?.copyWith(color: scheme.onSurfaceVariant),
),
),
listTileTheme: ListTileThemeData(
shape: rounded,
minTileHeight: 48,
selectedColor: scheme.onSecondaryContainer,
selectedTileColor: scheme.secondaryContainer,
),
expansionTileTheme: ExpansionTileThemeData(
shape: const Border(),
collapsedShape: const Border(),
iconColor: scheme.onSurface,
collapsedIconColor: scheme.onSurface,
),
tabBarTheme: TabBarThemeData(
dividerColor: scheme.outlineVariant,
dividerHeight: highContrastStroke,
labelColor: scheme.onSurface,
unselectedLabelColor: scheme.onSurfaceVariant,
labelStyle: text.titleSmall?.copyWith(fontWeight: FontWeight.w800),
unselectedLabelStyle: text.titleSmall,
indicatorSize: TabBarIndicatorSize.tab,
indicator: _tabIndicators[scheme.primary] ??= UnderlineTabIndicator(
borderSide: BorderSide(color: scheme.primary, width: 4),
),
overlayColor: r.overlay,
),
switchTheme: SwitchThemeData(
trackOutlineColor: r.trackOutline,
trackOutlineWidth: const WidgetStatePropertyAll(highContrastStroke),
),
checkboxTheme: CheckboxThemeData(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(3)),
),
side: r.checkboxSide,
checkColor: WidgetStatePropertyAll(scheme.onPrimary),
),
radioTheme: RadioThemeData(fillColor: r.selectedInk),
sliderTheme: SliderThemeData(
trackHeight: 6,
activeTrackColor: scheme.primary,
inactiveTrackColor: scheme.outlineVariant,
thumbColor: scheme.primary,
overlayColor: scheme.primary.withValues(alpha: 0.24),
valueIndicatorColor: scheme.inverseSurface,
valueIndicatorTextStyle: text.labelLarge?.copyWith(
color: scheme.onInverseSurface,
),
),
// Counts in the accent, larger, in heavy figures.
badgeTheme: BadgeThemeData(
backgroundColor: scheme.primary,
textColor: scheme.onPrimary,
smallSize: 10,
largeSize: 20,
textStyle: text.labelSmall?.copyWith(fontWeight: FontWeight.w800),
padding: const EdgeInsets.symmetric(horizontal: 6),
),
progressIndicatorTheme: ProgressIndicatorThemeData(
color: scheme.primary,
linearTrackColor: scheme.outlineVariant,
circularTrackColor: _clear,
linearMinHeight: 6,
),
scrollbarTheme: ScrollbarThemeData(
radius: const Radius.circular(highContrastRadius),
thickness: const WidgetStatePropertyAll(8),
thumbColor: WidgetStatePropertyAll(scheme.onSurfaceVariant),
),
);
}
final Map<Color, Decoration> _tabIndicators = {};