@@ -853,6 +853,88 @@ void main() {
853
853
tester.binding.platformDispatcher.clearPlatformBrightnessTestValue ();
854
854
});
855
855
856
+ testWidgets ('MaterialApp animates theme changes' , (WidgetTester tester) async {
857
+ final ThemeData lightTheme = ThemeData .light ();
858
+ final ThemeData darkTheme = ThemeData .dark ();
859
+ await tester.pumpWidget (
860
+ MaterialApp (
861
+ theme: lightTheme,
862
+ darkTheme: darkTheme,
863
+ themeMode: ThemeMode .light,
864
+ home: Builder (
865
+ builder: (BuildContext context) {
866
+ return const Scaffold ();
867
+ },
868
+ ),
869
+ ),
870
+ );
871
+ expect (tester.widget <Material >(find.byType (Material )).color, lightTheme.scaffoldBackgroundColor);
872
+
873
+ // Change to dark theme
874
+ await tester.pumpWidget (
875
+ MaterialApp (
876
+ theme: ThemeData .light (),
877
+ darkTheme: ThemeData .dark (),
878
+ themeMode: ThemeMode .dark,
879
+ home: Builder (
880
+ builder: (BuildContext context) {
881
+ return const Scaffold ();
882
+ },
883
+ ),
884
+ ),
885
+ );
886
+
887
+ // Wait half kThemeAnimationDuration = 200ms.
888
+ await tester.pump (const Duration (milliseconds: 100 ));
889
+
890
+ // Default curve is linear so background should be half way between
891
+ // the two colors.
892
+ final Color halfBGColor = Color .lerp (lightTheme.scaffoldBackgroundColor, darkTheme.scaffoldBackgroundColor, 0.5 )! ;
893
+ expect (tester.widget <Material >(find.byType (Material )).color, halfBGColor);
894
+ });
895
+
896
+ testWidgets ('MaterialApp theme animation can be turned off' , (WidgetTester tester) async {
897
+ final ThemeData lightTheme = ThemeData .light ();
898
+ final ThemeData darkTheme = ThemeData .dark ();
899
+ int scaffoldRebuilds = 0 ;
900
+
901
+ final Widget scaffold = Builder (
902
+ builder: (BuildContext context) {
903
+ scaffoldRebuilds++ ;
904
+ // Use Theme.of() to ensure we are building when the theme changes.
905
+ return Scaffold (backgroundColor: Theme .of (context).scaffoldBackgroundColor);
906
+ },
907
+ );
908
+
909
+ await tester.pumpWidget (
910
+ MaterialApp (
911
+ theme: lightTheme,
912
+ darkTheme: darkTheme,
913
+ themeMode: ThemeMode .light,
914
+ themeAnimationDuration: Duration .zero,
915
+ home: scaffold,
916
+ ),
917
+ );
918
+ expect (tester.widget <Material >(find.byType (Material )).color, lightTheme.scaffoldBackgroundColor);
919
+ expect (scaffoldRebuilds, 1 );
920
+
921
+ // Change to dark theme
922
+ await tester.pumpWidget (
923
+ MaterialApp (
924
+ theme: ThemeData .light (),
925
+ darkTheme: ThemeData .dark (),
926
+ themeMode: ThemeMode .dark,
927
+ themeAnimationDuration: Duration .zero,
928
+ home: scaffold,
929
+ ),
930
+ );
931
+
932
+ // Wait for any animation to finish.
933
+ await tester.pumpAndSettle ();
934
+ expect (tester.widget <Material >(find.byType (Material )).color, darkTheme.scaffoldBackgroundColor);
935
+ expect (scaffoldRebuilds, 2 );
936
+ });
937
+
856
938
testWidgets ('MaterialApp switches themes when the Window platformBrightness changes.' , (WidgetTester tester) async {
857
939
// Mock the Window to explicitly report a light platformBrightness.
858
940
final TestWidgetsFlutterBinding binding = tester.binding;
0 commit comments