From 87804363e4b5c9150eb19869a14319a622b79bc8 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Thu, 2 Jun 2022 23:46:39 +0300 Subject: [PATCH] Refactor `MaterialStateProperty` lerp functions (#104507) --- .../lib/src/material/button_style.dart | 25 +---------- .../lib/src/material/checkbox_theme.dart | 35 ++-------------- .../lib/src/material/data_table_theme.dart | 28 +------------ .../lib/src/material/material_state.dart | 30 ++++++++++++++ .../src/material/navigation_bar_theme.dart | 33 +-------------- .../flutter/lib/src/material/radio_theme.dart | 33 +-------------- .../lib/src/material/scrollbar_theme.dart | 41 +++---------------- .../lib/src/material/switch_theme.dart | 35 ++-------------- .../flutter/lib/src/painting/borders.dart | 39 ++++++++++++++++++ .../material_state_property_test.dart | 38 +++++++++++++++++ 10 files changed, 126 insertions(+), 211 deletions(-) diff --git a/packages/flutter/lib/src/material/button_style.dart b/packages/flutter/lib/src/material/button_style.dart index 739d72b107..216c5c788d 100644 --- a/packages/flutter/lib/src/material/button_style.dart +++ b/packages/flutter/lib/src/material/button_style.dart @@ -465,7 +465,7 @@ class ButtonStyle with Diagnosticable { fixedSize: _lerpProperties(a?.fixedSize, b?.fixedSize, t, Size.lerp), maximumSize: _lerpProperties(a?.maximumSize, b?.maximumSize, t, Size.lerp), side: _lerpSides(a?.side, b?.side, t), - shape: _lerpShapes(a?.shape, b?.shape, t), + shape: MaterialStateProperty.lerp(a?.shape, b?.shape, t, OutlinedBorder.lerp), mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor, visualDensity: t < 0.5 ? a?.visualDensity : b?.visualDensity, tapTargetSize: t < 0.5 ? a?.tapTargetSize : b?.tapTargetSize, @@ -491,14 +491,6 @@ class ButtonStyle with Diagnosticable { } return _LerpSides(a, b, t); } - - // TODO(hansmuller): OutlinedBorder needs a lerp method - https://github.com/flutter/flutter/issues/60555. - static MaterialStateProperty? _lerpShapes(MaterialStateProperty? a, MaterialStateProperty? b, double t) { - if (a == null && b == null) { - return null; - } - return _LerpShapes(a, b, t); - } } class _LerpProperties implements MaterialStateProperty { @@ -540,18 +532,3 @@ class _LerpSides implements MaterialStateProperty { return BorderSide.lerp(resolvedA, resolvedB, t); } } - -class _LerpShapes implements MaterialStateProperty { - const _LerpShapes(this.a, this.b, this.t); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - - @override - OutlinedBorder? resolve(Set states) { - final OutlinedBorder? resolvedA = a?.resolve(states); - final OutlinedBorder? resolvedB = b?.resolve(states); - return ShapeBorder.lerp(resolvedA, resolvedB, t) as OutlinedBorder?; - } -} diff --git a/packages/flutter/lib/src/material/checkbox_theme.dart b/packages/flutter/lib/src/material/checkbox_theme.dart index 360b2cf663..30d1448607 100644 --- a/packages/flutter/lib/src/material/checkbox_theme.dart +++ b/packages/flutter/lib/src/material/checkbox_theme.dart @@ -129,9 +129,9 @@ class CheckboxThemeData with Diagnosticable { static CheckboxThemeData lerp(CheckboxThemeData? a, CheckboxThemeData? b, double t) { return CheckboxThemeData( mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor, - fillColor: _lerpProperties(a?.fillColor, b?.fillColor, t, Color.lerp), - checkColor: _lerpProperties(a?.checkColor, b?.checkColor, t, Color.lerp), - overlayColor: _lerpProperties(a?.overlayColor, b?.overlayColor, t, Color.lerp), + fillColor: MaterialStateProperty.lerp(a?.fillColor, b?.fillColor, t, Color.lerp), + checkColor: MaterialStateProperty.lerp(a?.checkColor, b?.checkColor, t, Color.lerp), + overlayColor: MaterialStateProperty.lerp(a?.overlayColor, b?.overlayColor, t, Color.lerp), splashRadius: lerpDouble(a?.splashRadius, b?.splashRadius, t), materialTapTargetSize: t < 0.5 ? a?.materialTapTargetSize : b?.materialTapTargetSize, visualDensity: t < 0.5 ? a?.visualDensity : b?.visualDensity, @@ -187,19 +187,6 @@ class CheckboxThemeData with Diagnosticable { properties.add(DiagnosticsProperty('side', side, defaultValue: null)); } - static MaterialStateProperty? _lerpProperties( - MaterialStateProperty? a, - MaterialStateProperty? b, - double t, - T Function(T?, T?, double) lerpFunction, - ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } - // Special case because BorderSide.lerp() doesn't support null arguments static BorderSide? _lerpSides(BorderSide? a, BorderSide? b, double t) { if (a == null && b == null) { @@ -209,22 +196,6 @@ class CheckboxThemeData with Diagnosticable { } } -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } -} - /// Applies a checkbox theme to descendant [Checkbox] widgets. /// /// Descendant widgets obtain the current theme's [CheckboxTheme] object using diff --git a/packages/flutter/lib/src/material/data_table_theme.dart b/packages/flutter/lib/src/material/data_table_theme.dart index 69f92227aa..6a79264d5a 100644 --- a/packages/flutter/lib/src/material/data_table_theme.dart +++ b/packages/flutter/lib/src/material/data_table_theme.dart @@ -122,10 +122,10 @@ class DataTableThemeData with Diagnosticable { assert(t != null); return DataTableThemeData( decoration: Decoration.lerp(a.decoration, b.decoration, t), - dataRowColor: _lerpProperties(a.dataRowColor, b.dataRowColor, t, Color.lerp), + dataRowColor: MaterialStateProperty.lerp(a.dataRowColor, b.dataRowColor, t, Color.lerp), dataRowHeight: lerpDouble(a.dataRowHeight, b.dataRowHeight, t), dataTextStyle: TextStyle.lerp(a.dataTextStyle, b.dataTextStyle, t), - headingRowColor: _lerpProperties(a.headingRowColor, b.headingRowColor, t, Color.lerp), + headingRowColor: MaterialStateProperty.lerp(a.headingRowColor, b.headingRowColor, t, Color.lerp), headingRowHeight: lerpDouble(a.headingRowHeight, b.headingRowHeight, t), headingTextStyle: TextStyle.lerp(a.headingTextStyle, b.headingTextStyle, t), horizontalMargin: lerpDouble(a.horizontalMargin, b.horizontalMargin, t), @@ -187,30 +187,6 @@ class DataTableThemeData with Diagnosticable { properties.add(DoubleProperty('dividerThickness', dividerThickness, defaultValue: null)); properties.add(DoubleProperty('checkboxHorizontalMargin', checkboxHorizontalMargin, defaultValue: null)); } - - static MaterialStateProperty? _lerpProperties(MaterialStateProperty? a, MaterialStateProperty? b, double t, T Function(T?, T?, double) lerpFunction ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } -} - -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } } /// Applies a data table theme to descendant [DataTable] widgets. diff --git a/packages/flutter/lib/src/material/material_state.dart b/packages/flutter/lib/src/material/material_state.dart index 282dc561ce..2566c50b33 100644 --- a/packages/flutter/lib/src/material/material_state.dart +++ b/packages/flutter/lib/src/material/material_state.dart @@ -658,6 +658,36 @@ abstract class MaterialStateProperty { // a dart fix that will replace this with MaterialStatePropertyAll: // https://github.com/dart-lang/sdk/issues/49056. static MaterialStateProperty all(T value) => MaterialStatePropertyAll(value); + + /// Linearly interpolate between two [MaterialStateProperty]s. + static MaterialStateProperty? lerp( + MaterialStateProperty? a, + MaterialStateProperty? b, + double t, + T? Function(T?, T?, double) lerpFunction, + ) { + // Avoid creating a _LerpProperties object for a common case. + if (a == null && b == null) { + return null; + } + return _LerpProperties(a, b, t, lerpFunction); + } +} + +class _LerpProperties implements MaterialStateProperty { + const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); + + final MaterialStateProperty? a; + final MaterialStateProperty? b; + final double t; + final T? Function(T?, T?, double) lerpFunction; + + @override + T? resolve(Set states) { + final T? resolvedA = a?.resolve(states); + final T? resolvedB = b?.resolve(states); + return lerpFunction(resolvedA, resolvedB, t); + } } class _MaterialStatePropertyWith implements MaterialStateProperty { diff --git a/packages/flutter/lib/src/material/navigation_bar_theme.dart b/packages/flutter/lib/src/material/navigation_bar_theme.dart index 25eb453720..94e4c561ec 100644 --- a/packages/flutter/lib/src/material/navigation_bar_theme.dart +++ b/packages/flutter/lib/src/material/navigation_bar_theme.dart @@ -127,8 +127,8 @@ class NavigationBarThemeData with Diagnosticable { elevation: lerpDouble(a?.elevation, b?.elevation, t), indicatorColor: Color.lerp(a?.indicatorColor, b?.indicatorColor, t), indicatorShape: ShapeBorder.lerp(a?.indicatorShape, b?.indicatorShape, t), - labelTextStyle: _lerpProperties(a?.labelTextStyle, b?.labelTextStyle, t, TextStyle.lerp), - iconTheme: _lerpProperties(a?.iconTheme, b?.iconTheme, t, IconThemeData.lerp), + labelTextStyle: MaterialStateProperty.lerp(a?.labelTextStyle, b?.labelTextStyle, t, TextStyle.lerp), + iconTheme: MaterialStateProperty.lerp(a?.iconTheme, b?.iconTheme, t, IconThemeData.lerp), labelBehavior: t < 0.5 ? a?.labelBehavior : b?.labelBehavior, ); } @@ -179,35 +179,6 @@ class NavigationBarThemeData with Diagnosticable { properties.add(DiagnosticsProperty>('iconTheme', iconTheme, defaultValue: null)); properties.add(DiagnosticsProperty('labelBehavior', labelBehavior, defaultValue: null)); } - - static MaterialStateProperty? _lerpProperties( - MaterialStateProperty? a, - MaterialStateProperty? b, - double t, - T Function(T?, T?, double) lerpFunction, - ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } -} - -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } } /// An inherited widget that defines visual properties for [NavigationBar]s and diff --git a/packages/flutter/lib/src/material/radio_theme.dart b/packages/flutter/lib/src/material/radio_theme.dart index 485fb2ef3d..8631525743 100644 --- a/packages/flutter/lib/src/material/radio_theme.dart +++ b/packages/flutter/lib/src/material/radio_theme.dart @@ -111,9 +111,9 @@ class RadioThemeData with Diagnosticable { static RadioThemeData lerp(RadioThemeData? a, RadioThemeData? b, double t) { return RadioThemeData( mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor, - fillColor: _lerpProperties(a?.fillColor, b?.fillColor, t, Color.lerp), + fillColor: MaterialStateProperty.lerp(a?.fillColor, b?.fillColor, t, Color.lerp), materialTapTargetSize: t < 0.5 ? a?.materialTapTargetSize : b?.materialTapTargetSize, - overlayColor: _lerpProperties(a?.overlayColor, b?.overlayColor, t, Color.lerp), + overlayColor: MaterialStateProperty.lerp(a?.overlayColor, b?.overlayColor, t, Color.lerp), splashRadius: lerpDouble(a?.splashRadius, b?.splashRadius, t), visualDensity: t < 0.5 ? a?.visualDensity : b?.visualDensity, ); @@ -156,35 +156,6 @@ class RadioThemeData with Diagnosticable { properties.add(DiagnosticsProperty('materialTapTargetSize', materialTapTargetSize, defaultValue: null)); properties.add(DiagnosticsProperty('visualDensity', visualDensity, defaultValue: null)); } - - static MaterialStateProperty? _lerpProperties( - MaterialStateProperty? a, - MaterialStateProperty? b, - double t, - T Function(T?, T?, double) lerpFunction, - ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } -} - -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } } /// Applies a radio theme to descendant [Radio] widgets. diff --git a/packages/flutter/lib/src/material/scrollbar_theme.dart b/packages/flutter/lib/src/material/scrollbar_theme.dart index da29beeb80..d1bece8e4b 100644 --- a/packages/flutter/lib/src/material/scrollbar_theme.dart +++ b/packages/flutter/lib/src/material/scrollbar_theme.dart @@ -198,16 +198,16 @@ class ScrollbarThemeData with Diagnosticable { static ScrollbarThemeData lerp(ScrollbarThemeData? a, ScrollbarThemeData? b, double t) { assert(t != null); return ScrollbarThemeData( - thumbVisibility: _lerpProperties(a?.thumbVisibility, b?.thumbVisibility, t, _lerpBool), - thickness: _lerpProperties(a?.thickness, b?.thickness, t, lerpDouble), - trackVisibility: _lerpProperties(a?.trackVisibility, b?.trackVisibility, t, _lerpBool), + thumbVisibility: MaterialStateProperty.lerp(a?.thumbVisibility, b?.thumbVisibility, t, _lerpBool), + thickness: MaterialStateProperty.lerp(a?.thickness, b?.thickness, t, lerpDouble), + trackVisibility: MaterialStateProperty.lerp(a?.trackVisibility, b?.trackVisibility, t, _lerpBool), showTrackOnHover: _lerpBool(a?.showTrackOnHover, b?.showTrackOnHover, t), isAlwaysShown: _lerpBool(a?.isAlwaysShown, b?.isAlwaysShown, t), interactive: _lerpBool(a?.interactive, b?.interactive, t), radius: Radius.lerp(a?.radius, b?.radius, t), - thumbColor: _lerpProperties(a?.thumbColor, b?.thumbColor, t, Color.lerp), - trackColor: _lerpProperties(a?.trackColor, b?.trackColor, t, Color.lerp), - trackBorderColor: _lerpProperties(a?.trackBorderColor, b?.trackBorderColor, t, Color.lerp), + thumbColor: MaterialStateProperty.lerp(a?.thumbColor, b?.thumbColor, t, Color.lerp), + trackColor: MaterialStateProperty.lerp(a?.trackColor, b?.trackColor, t, Color.lerp), + trackBorderColor: MaterialStateProperty.lerp(a?.trackBorderColor, b?.trackBorderColor, t, Color.lerp), crossAxisMargin: lerpDouble(a?.crossAxisMargin, b?.crossAxisMargin, t), mainAxisMargin: lerpDouble(a?.mainAxisMargin, b?.mainAxisMargin, t), minThumbLength: lerpDouble(a?.minThumbLength, b?.minThumbLength, t), @@ -272,35 +272,6 @@ class ScrollbarThemeData with Diagnosticable { properties.add(DiagnosticsProperty('mainAxisMargin', mainAxisMargin, defaultValue: null)); properties.add(DiagnosticsProperty('minThumbLength', minThumbLength, defaultValue: null)); } - - static MaterialStateProperty? _lerpProperties( - MaterialStateProperty? a, - MaterialStateProperty? b, - double t, - T Function(T?, T?, double) lerpFunction, - ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } -} - -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } } bool? _lerpBool(bool? a, bool? b, double t) => t < 0.5 ? a : b; diff --git a/packages/flutter/lib/src/material/switch_theme.dart b/packages/flutter/lib/src/material/switch_theme.dart index 8d079eedaa..3ac3b45018 100644 --- a/packages/flutter/lib/src/material/switch_theme.dart +++ b/packages/flutter/lib/src/material/switch_theme.dart @@ -98,11 +98,11 @@ class SwitchThemeData with Diagnosticable { /// {@macro dart.ui.shadow.lerp} static SwitchThemeData lerp(SwitchThemeData? a, SwitchThemeData? b, double t) { return SwitchThemeData( - thumbColor: _lerpProperties(a?.thumbColor, b?.thumbColor, t, Color.lerp), - trackColor: _lerpProperties(a?.trackColor, b?.trackColor, t, Color.lerp), + thumbColor: MaterialStateProperty.lerp(a?.thumbColor, b?.thumbColor, t, Color.lerp), + trackColor: MaterialStateProperty.lerp(a?.trackColor, b?.trackColor, t, Color.lerp), materialTapTargetSize: t < 0.5 ? a?.materialTapTargetSize : b?.materialTapTargetSize, mouseCursor: t < 0.5 ? a?.mouseCursor : b?.mouseCursor, - overlayColor: _lerpProperties(a?.overlayColor, b?.overlayColor, t, Color.lerp), + overlayColor: MaterialStateProperty.lerp(a?.overlayColor, b?.overlayColor, t, Color.lerp), splashRadius: lerpDouble(a?.splashRadius, b?.splashRadius, t), ); } @@ -144,35 +144,6 @@ class SwitchThemeData with Diagnosticable { properties.add(DiagnosticsProperty>('overlayColor', overlayColor, defaultValue: null)); properties.add(DoubleProperty('splashRadius', splashRadius, defaultValue: null)); } - - static MaterialStateProperty? _lerpProperties( - MaterialStateProperty? a, - MaterialStateProperty? b, - double t, - T Function(T?, T?, double) lerpFunction, - ) { - // Avoid creating a _LerpProperties object for a common case. - if (a == null && b == null) { - return null; - } - return _LerpProperties(a, b, t, lerpFunction); - } -} - -class _LerpProperties implements MaterialStateProperty { - const _LerpProperties(this.a, this.b, this.t, this.lerpFunction); - - final MaterialStateProperty? a; - final MaterialStateProperty? b; - final double t; - final T Function(T?, T?, double) lerpFunction; - - @override - T resolve(Set states) { - final T? resolvedA = a?.resolve(states); - final T? resolvedB = b?.resolve(states); - return lerpFunction(resolvedA, resolvedB, t); - } } /// Applies a switch theme to descendant [Switch] widgets. diff --git a/packages/flutter/lib/src/painting/borders.dart b/packages/flutter/lib/src/painting/borders.dart index 52a463137e..9d8666d403 100644 --- a/packages/flutter/lib/src/painting/borders.dart +++ b/packages/flutter/lib/src/painting/borders.dart @@ -566,6 +566,45 @@ abstract class OutlinedBorder extends ShapeBorder { /// Returns a copy of this OutlinedBorder that draws its outline with the /// specified [side], if [side] is non-null. OutlinedBorder copyWith({ BorderSide? side }); + + @override + ShapeBorder scale(double t); + + @override + ShapeBorder? lerpFrom(ShapeBorder? a, double t) { + if (a == null) { + return scale(t); + } + return null; + } + + @override + ShapeBorder? lerpTo(ShapeBorder? b, double t) { + if (b == null) { + return scale(1.0 - t); + } + return null; + } + + /// Linearly interpolates between two [OutlinedBorder]s. + /// + /// This defers to `b`'s [lerpTo] function if `b` is not null. If `b` is + /// null or if its [lerpTo] returns null, it uses `a`'s [lerpFrom] + /// function instead. If both return null, it returns `a` before `t=0.5` + /// and `b` after `t=0.5`. + /// + /// {@macro dart.ui.shadow.lerp} + static OutlinedBorder? lerp(OutlinedBorder? a, OutlinedBorder? b, double t) { + assert(t != null); + ShapeBorder? result; + if (b != null) { + result = b.lerpFrom(a, t); + } + if (result == null && a != null) { + result = a.lerpTo(b, t); + } + return result as OutlinedBorder? ?? (t < 0.5 ? a : b); + } } /// Represents the addition of two otherwise-incompatible borders. diff --git a/packages/flutter/test/material/material_state_property_test.dart b/packages/flutter/test/material/material_state_property_test.dart index d6679e72e8..2f93047140 100644 --- a/packages/flutter/test/material/material_state_property_test.dart +++ b/packages/flutter/test/material/material_state_property_test.dart @@ -42,4 +42,42 @@ void main() { expect(value.resolve({MaterialState.disabled}), 123); expect(value.resolve({MaterialState.error}), 123); }); + + test("Can interpolate between two MaterialStateProperty's", () { + const MaterialStateProperty textStyle1 = MaterialStatePropertyAll( + TextStyle(fontSize: 14.0), + ); + const MaterialStateProperty textStyle2 = MaterialStatePropertyAll( + TextStyle(fontSize: 20.0), + ); + + // Using `0.0` interpolation value. + TextStyle textStyle = MaterialStateProperty.lerp( + textStyle1, + textStyle2, + 0.0, + TextStyle.lerp, + )!.resolve(enabled)!; + expect(textStyle.fontSize, 14.0); + + // Using `0.5` interpolation value. + textStyle = MaterialStateProperty.lerp( + textStyle1, + textStyle2, + 0.5, + TextStyle.lerp, + )!.resolve(enabled)!; + expect(textStyle.fontSize, 17.0); + + // Using `1.0` interpolation value. + textStyle = MaterialStateProperty.lerp( + textStyle1, + textStyle2, + 1.0, + TextStyle.lerp, + )!.resolve(enabled)!; + expect(textStyle.fontSize, 20.0); + }); } + +Set enabled = {};