From 23cc1401f09f25fe84c4c23d77601c55e0204d79 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Mon, 2 Nov 2020 15:45:38 -0800 Subject: [PATCH] Updated the button.icon factory constructors for NNBD (#69596) --- .../lib/src/material/elevated_button.dart | 14 ++--- .../lib/src/material/outlined_button.dart | 14 ++--- .../flutter/lib/src/material/text_button.dart | 14 ++--- .../test/material/elevated_button_test.dart | 46 ++++++++++++-- .../test/material/outlined_button_test.dart | 60 ++++++++++++++++--- .../test/material/text_button_test.dart | 38 +++++++++++- 6 files changed, 150 insertions(+), 36 deletions(-) diff --git a/packages/flutter/lib/src/material/elevated_button.dart b/packages/flutter/lib/src/material/elevated_button.dart index 07fedef344..472f9b50af 100644 --- a/packages/flutter/lib/src/material/elevated_button.dart +++ b/packages/flutter/lib/src/material/elevated_button.dart @@ -82,13 +82,13 @@ class ElevatedButton extends ButtonStyleButton { /// /// The [icon] and [label] arguments must not be null. factory ElevatedButton.icon({ - Key key, - required VoidCallback onPressed, - VoidCallback onLongPress, - ButtonStyle style, - FocusNode focusNode, - bool autofocus, - Clip clipBehavior, + Key? key, + required VoidCallback? onPressed, + VoidCallback? onLongPress, + ButtonStyle? style, + FocusNode? focusNode, + bool? autofocus, + Clip? clipBehavior, required Widget icon, required Widget label, }) = _ElevatedButtonWithIcon; diff --git a/packages/flutter/lib/src/material/outlined_button.dart b/packages/flutter/lib/src/material/outlined_button.dart index 552e9569d5..98212289bc 100644 --- a/packages/flutter/lib/src/material/outlined_button.dart +++ b/packages/flutter/lib/src/material/outlined_button.dart @@ -79,13 +79,13 @@ class OutlinedButton extends ButtonStyleButton { /// /// The [icon] and [label] arguments must not be null. factory OutlinedButton.icon({ - Key key, - required VoidCallback onPressed, - VoidCallback onLongPress, - ButtonStyle style, - FocusNode focusNode, - bool autofocus, - Clip clipBehavior, + Key? key, + required VoidCallback? onPressed, + VoidCallback? onLongPress, + ButtonStyle? style, + FocusNode? focusNode, + bool? autofocus, + Clip? clipBehavior, required Widget icon, required Widget label, }) = _OutlinedButtonWithIcon; diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index 7f2911a6e4..a0faa22d3d 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -86,13 +86,13 @@ class TextButton extends ButtonStyleButton { /// /// The [icon] and [label] arguments must not be null. factory TextButton.icon({ - Key key, - required VoidCallback onPressed, - VoidCallback onLongPress, - ButtonStyle style, - FocusNode focusNode, - bool autofocus, - Clip clipBehavior, + Key? key, + required VoidCallback? onPressed, + VoidCallback? onLongPress, + ButtonStyle? style, + FocusNode? focusNode, + bool? autofocus, + Clip? clipBehavior, required Widget icon, required Widget label, }) = _TextButtonWithIcon; diff --git a/packages/flutter/test/material/elevated_button_test.dart b/packages/flutter/test/material/elevated_button_test.dart index fc7206740d..c4436a90e7 100644 --- a/packages/flutter/test/material/elevated_button_test.dart +++ b/packages/flutter/test/material/elevated_button_test.dart @@ -11,7 +11,7 @@ import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; void main() { - testWidgets('ElevatedButton defaults', (WidgetTester tester) async { + testWidgets('ElevatedButton, ElevatedButton.icon defaults', (WidgetTester tester) async { const ColorScheme colorScheme = ColorScheme.light(); // Enabled ElevatedButton @@ -27,13 +27,13 @@ void main() { ), ); - final Finder rawButtonMaterial = find.descendant( + final Finder buttonMaterial = find.descendant( of: find.byType(ElevatedButton), matching: find.byType(Material), ); - Material material = tester.widget(rawButtonMaterial); + Material material = tester.widget(buttonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); @@ -56,7 +56,7 @@ void main() { expect(inkFeatures, paints..circle(color: colorScheme.onPrimary.withAlpha(0x3d))); // splash color is onPrimary(0.24) // Only elevation changes when enabled and pressed. - material = tester.widget(rawButtonMaterial); + material = tester.widget(buttonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); @@ -74,6 +74,42 @@ void main() { await gesture.up(); await tester.pumpAndSettle(); + // Enabled ElevatedButton.icon + final Key iconButtonKey = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData.from(colorScheme: colorScheme), + home: Center( + child: ElevatedButton.icon( + key: iconButtonKey, + onPressed: () { }, + icon: const Icon(Icons.add), + label: const Text('label'), + ), + ), + ), + ); + + final Finder iconButtonMaterial = find.descendant( + of: find.byKey(iconButtonKey), + matching: find.byType(Material), + ); + + material = tester.widget(iconButtonMaterial); + expect(material.animationDuration, const Duration(milliseconds: 200)); + expect(material.borderOnForeground, true); + expect(material.borderRadius, null); + expect(material.clipBehavior, Clip.none); + expect(material.color, colorScheme.primary); + expect(material.elevation, 2); + expect(material.shadowColor, const Color(0xff000000)); + expect(material.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(4))); + expect(material.textStyle!.color, colorScheme.onPrimary); + expect(material.textStyle!.fontFamily, 'Roboto'); + expect(material.textStyle!.fontSize, 14); + expect(material.textStyle!.fontWeight, FontWeight.w500); + expect(material.type, MaterialType.button); + // Disabled ElevatedButton await tester.pumpWidget( MaterialApp( @@ -90,7 +126,7 @@ void main() { // Finish the elevation animation, final background color change. await tester.pumpAndSettle(); - material = tester.widget(rawButtonMaterial); + material = tester.widget(buttonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); diff --git a/packages/flutter/test/material/outlined_button_test.dart b/packages/flutter/test/material/outlined_button_test.dart index a8007d3c02..f27e3180b6 100644 --- a/packages/flutter/test/material/outlined_button_test.dart +++ b/packages/flutter/test/material/outlined_button_test.dart @@ -11,12 +11,7 @@ import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; void main() { - testWidgets('OutlinedButton defaults', (WidgetTester tester) async { - final Finder rawButtonMaterial = find.descendant( - of: find.byType(OutlinedButton), - matching: find.byType(Material), - ); - + testWidgets('OutlinedButton, OutlinedButton.icon defaults', (WidgetTester tester) async { const ColorScheme colorScheme = ColorScheme.light(); // Enabled OutlinedButton @@ -32,7 +27,12 @@ void main() { ), ); - Material material = tester.widget(rawButtonMaterial); + final Finder buttonMaterial = find.descendant( + of: find.byType(OutlinedButton), + matching: find.byType(Material), + ); + + Material material = tester.widget(buttonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); @@ -63,7 +63,49 @@ void main() { await gesture.up(); await tester.pumpAndSettle(); // No change vs enabled and not pressed. - material = tester.widget(rawButtonMaterial); + material = tester.widget(buttonMaterial); + expect(material.animationDuration, const Duration(milliseconds: 200)); + expect(material.borderOnForeground, true); + expect(material.borderRadius, null); + expect(material.clipBehavior, Clip.none); + expect(material.color, Colors.transparent); + expect(material.elevation, 0.0); + expect(material.shadowColor, const Color(0xff000000)); + expect(material.shape, RoundedRectangleBorder( + side: BorderSide( + width: 1, + color: colorScheme.onSurface.withOpacity(0.12), + ), + borderRadius: BorderRadius.circular(4.0), + )); + expect(material.textStyle!.color, colorScheme.primary); + expect(material.textStyle!.fontFamily, 'Roboto'); + expect(material.textStyle!.fontSize, 14); + expect(material.textStyle!.fontWeight, FontWeight.w500); + expect(material.type, MaterialType.button); + + // Enabled OutlinedButton.icon + final Key iconButtonKey = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData.from(colorScheme: colorScheme), + home: Center( + child: OutlinedButton.icon( + key: iconButtonKey, + onPressed: () { }, + icon: const Icon(Icons.add), + label: const Text('label'), + ), + ), + ), + ); + + final Finder iconButtonMaterial = find.descendant( + of: find.byKey(iconButtonKey), + matching: find.byType(Material), + ); + + material = tester.widget(iconButtonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); @@ -97,7 +139,7 @@ void main() { ), ); - material = tester.widget(rawButtonMaterial); + material = tester.widget(buttonMaterial); expect(material.animationDuration, const Duration(milliseconds: 200)); expect(material.borderOnForeground, true); expect(material.borderRadius, null); diff --git a/packages/flutter/test/material/text_button_test.dart b/packages/flutter/test/material/text_button_test.dart index 0d697a9b4a..4f283162eb 100644 --- a/packages/flutter/test/material/text_button_test.dart +++ b/packages/flutter/test/material/text_button_test.dart @@ -11,7 +11,7 @@ import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; void main() { - testWidgets('TextButton defaults', (WidgetTester tester) async { + testWidgets('TextButton, TextButton.icon defaults', (WidgetTester tester) async { const ColorScheme colorScheme = ColorScheme.light(); // Enabled TextButton @@ -72,6 +72,42 @@ void main() { expect(material.textStyle!.fontWeight, FontWeight.w500); expect(material.type, MaterialType.button); + // Enabled TextButton.icon + final Key iconButtonKey = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData.from(colorScheme: colorScheme), + home: Center( + child: TextButton.icon( + key: iconButtonKey, + onPressed: () { }, + icon: const Icon(Icons.add), + label: const Text('label'), + ), + ), + ), + ); + + final Finder iconButtonMaterial = find.descendant( + of: find.byKey(iconButtonKey), + matching: find.byType(Material), + ); + + material = tester.widget(iconButtonMaterial); + expect(material.animationDuration, const Duration(milliseconds: 200)); + expect(material.borderOnForeground, true); + expect(material.borderRadius, null); + expect(material.clipBehavior, Clip.none); + expect(material.color, Colors.transparent); + expect(material.elevation, 0.0); + expect(material.shadowColor, const Color(0xff000000)); + expect(material.shape, RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0))); + expect(material.textStyle!.color, colorScheme.primary); + expect(material.textStyle!.fontFamily, 'Roboto'); + expect(material.textStyle!.fontSize, 14); + expect(material.textStyle!.fontWeight, FontWeight.w500); + expect(material.type, MaterialType.button); + // Disabled TextButton await tester.pumpWidget( MaterialApp(