From bf8e1270a7688a3520626d8a8c9bc8ed96673897 Mon Sep 17 00:00:00 2001 From: jacobsimionato Date: Wed, 8 Sep 2021 13:45:04 -0400 Subject: [PATCH] Implement operator = and hashcode for CupertinoThemeData (#89604) This fixes #71222 * Implement operator = and hashcode for CupertinoThemeData and CupertinoTextThemeData which CupertinoThemeData depends on. * Add basic tests for CupertinoThemeData equality and hashcode. I also manually tested the case mentioned in #71222, e.g. equality of ThemeData(cupertinoOverrideTheme: someTheme) Co-authored-by: Jacob Simionato --- .../flutter/lib/src/cupertino/text_theme.dart | 38 +++++++++++ packages/flutter/lib/src/cupertino/theme.dart | 65 +++++++++++++++++++ .../flutter/test/cupertino/theme_test.dart | 35 ++++++++++ .../flutter/test/material/theme_test.dart | 3 +- 4 files changed, 140 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/cupertino/text_theme.dart b/packages/flutter/lib/src/cupertino/text_theme.dart index d632f49c95..d48658d244 100644 --- a/packages/flutter/lib/src/cupertino/text_theme.dart +++ b/packages/flutter/lib/src/cupertino/text_theme.dart @@ -247,6 +247,44 @@ class CupertinoTextThemeData with Diagnosticable { ); } + @override + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) { + return false; + } + + // Warning: make sure these properties are in the exact same order as in + // hashValues() and in the raw constructor and in the order of fields in + // the class and in the lerp() method. + return other is CupertinoTextThemeData && + other.textStyle == textStyle && + other.actionTextStyle == actionTextStyle && + other.tabLabelTextStyle == tabLabelTextStyle && + other.navTitleTextStyle == navTitleTextStyle && + other.navLargeTitleTextStyle == navLargeTitleTextStyle && + other.navActionTextStyle == navActionTextStyle && + other.pickerTextStyle == pickerTextStyle && + other.dateTimePickerTextStyle == dateTimePickerTextStyle; + } + + @override + int get hashCode { + // Warning: For the sanity of the reader, please make sure these properties + // are in the exact same order as in operator == and in the raw constructor + // and in the order of fields in the class and in the lerp() method. + final List values = [ + textStyle, + actionTextStyle, + tabLabelTextStyle, + navTitleTextStyle, + navLargeTitleTextStyle, + navActionTextStyle, + pickerTextStyle, + dateTimePickerTextStyle, + ]; + return hashList(values); + } + @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); diff --git a/packages/flutter/lib/src/cupertino/theme.dart b/packages/flutter/lib/src/cupertino/theme.dart index 69f555e451..197f5ce675 100644 --- a/packages/flutter/lib/src/cupertino/theme.dart +++ b/packages/flutter/lib/src/cupertino/theme.dart @@ -290,6 +290,38 @@ class CupertinoThemeData extends NoDefaultCupertinoThemeData with Diagnosticable ); } + @override + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) + return false; + // Warning: make sure these properties are in the exact same order as in + // hashValues() and in the raw constructor and in the order of fields in + // the class and in the lerp() method. + return other is CupertinoThemeData && + other.brightness == brightness && + other.primaryColor == primaryColor && + other.primaryContrastingColor == primaryContrastingColor && + other.textTheme == textTheme && + other.barBackgroundColor == barBackgroundColor && + other.scaffoldBackgroundColor == scaffoldBackgroundColor; + } + + @override + int get hashCode { + // Warning: For the sanity of the reader, please make sure these properties + // are in the exact same order as in operator == and in the raw constructor + // and in the order of fields in the class and in the lerp() method. + final List values = [ + brightness, + primaryColor, + primaryContrastingColor, + textTheme, + barBackgroundColor, + scaffoldBackgroundColor, + ]; + return hashList(values); + } + @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { super.debugFillProperties(properties); @@ -315,6 +347,7 @@ class CupertinoThemeData extends NoDefaultCupertinoThemeData with Diagnosticable /// /// * [CupertinoThemeData], which uses reasonable default values for /// unspecified theme properties. +@immutable class NoDefaultCupertinoThemeData { /// Creates a [NoDefaultCupertinoThemeData] styling specification. /// @@ -442,6 +475,38 @@ class NoDefaultCupertinoThemeData { scaffoldBackgroundColor: scaffoldBackgroundColor ?? this.scaffoldBackgroundColor, ); } + + @override + bool operator ==(Object other) { + if (other.runtimeType != runtimeType) + return false; + // Warning: make sure these properties are in the exact same order as in + // hashValues() and in the raw constructor and in the order of fields in + // the class and in the lerp() method. + return other is NoDefaultCupertinoThemeData && + other.brightness == brightness && + other.primaryColor == primaryColor && + other.primaryContrastingColor == primaryContrastingColor && + other.textTheme == textTheme && + other.barBackgroundColor == barBackgroundColor && + other.scaffoldBackgroundColor == scaffoldBackgroundColor; + } + + @override + int get hashCode { + // Warning: For the sanity of the reader, please make sure these properties + // are in the exact same order as in operator == and in the raw constructor + // and in the order of fields in the class and in the lerp() method. + final List values = [ + brightness, + primaryColor, + primaryContrastingColor, + textTheme, + barBackgroundColor, + scaffoldBackgroundColor, + ]; + return hashList(values); + } } @immutable diff --git a/packages/flutter/test/cupertino/theme_test.dart b/packages/flutter/test/cupertino/theme_test.dart index 87b8665abc..6075e2a5e3 100644 --- a/packages/flutter/test/cupertino/theme_test.dart +++ b/packages/flutter/test/cupertino/theme_test.dart @@ -206,6 +206,41 @@ void main() { ); }); + test('Theme operator == returns correct value for simple cases', () { + expect(const CupertinoThemeData(), equals(const CupertinoThemeData())); + + expect(const CupertinoThemeData(brightness: Brightness.light), + isNot(equals(const CupertinoThemeData(brightness: Brightness.dark)))); + + expect( + const CupertinoThemeData( + textTheme: CupertinoTextThemeData( + primaryColor: CupertinoColors.activeGreen)), + isNot(equals(const CupertinoThemeData( + textTheme: CupertinoTextThemeData( + primaryColor: CupertinoColors.activeOrange))))); + }); + + test('hashCode behaves correctly for simple cases', () { + expect(const CupertinoThemeData().hashCode, + equals(const CupertinoThemeData().hashCode)); + + expect( + const CupertinoThemeData(brightness: Brightness.light).hashCode, + isNot(equals(const CupertinoThemeData(brightness: Brightness.dark)) + .hashCode)); + + expect( + const CupertinoThemeData( + textTheme: CupertinoTextThemeData( + primaryColor: CupertinoColors.activeGreen)) + .hashCode, + isNot(equals(const CupertinoThemeData( + textTheme: CupertinoTextThemeData( + primaryColor: CupertinoColors.activeOrange)) + .hashCode))); + }); + late Brightness currentBrightness; void colorMatches(Color? componentColor, CupertinoDynamicColor expectedDynamicColor) { switch (currentBrightness) { diff --git a/packages/flutter/test/material/theme_test.dart b/packages/flutter/test/material/theme_test.dart index d1c227b6a1..7682b5f6d5 100644 --- a/packages/flutter/test/material/theme_test.dart +++ b/packages/flutter/test/material/theme_test.dart @@ -591,7 +591,8 @@ void main() { ), )); - expect(buildCount, 2); + // Widget does not rebuild as theme color remains overriden. + expect(buildCount, 1); expect(theme.primaryColor, CupertinoColors.activeOrange); }, );