Unverified Commit 4464d09d authored by Hans Muller's avatar Hans Muller Committed by GitHub

Updated TabBar and ToggleButtons examples (#128088)

Updated the ToggleButtons example and test to M3.

Updated the nested tabs test to avoid warnings like:
```
Warning: A call to tap() with finder "exactly one widget with type "Tab" which is an ancestor of text "Explore": Tab(text: "Explore", icon: Icon)" derived an Offset (Offset(666.7, 92.0)) that would not hit test on the specified widget.
```
parent ab70aea3
...@@ -23,8 +23,9 @@ class ToggleButtonsExampleApp extends StatelessWidget { ...@@ -23,8 +23,9 @@ class ToggleButtonsExampleApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return const MaterialApp( return MaterialApp(
home: ToggleButtonsSample(title: 'ToggleButtons Sample'), theme: ThemeData(useMaterial3: true),
home: const ToggleButtonsSample(title: 'ToggleButtons Sample'),
); );
} }
} }
......
...@@ -24,45 +24,40 @@ void main() { ...@@ -24,45 +24,40 @@ void main() {
final TabBar secondaryTabBar = tester.widget<TabBar>(find.byType(TabBar).first); final TabBar secondaryTabBar = tester.widget<TabBar>(find.byType(TabBar).first);
expect(secondaryTabBar.tabs.length, 2); expect(secondaryTabBar.tabs.length, 2);
final Finder primaryTab1 = find.widgetWithText(Tab, primaryTabLabel1);
final Finder primaryTab2 = find.widgetWithText(Tab, primaryTabLabel2);
final Finder primaryTab3 = find.widgetWithText(Tab, primaryTabLabel3);
final Finder secondaryTab2 = find.widgetWithText(Tab, secondaryTabLabel2);
String tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel1 tab'; String tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel1 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(primaryTab1); await tester.tap(find.text(primaryTabLabel1));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel1: $secondaryTabLabel1 tab'; tabBarViewText = '$primaryTabLabel1: $secondaryTabLabel1 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(secondaryTab2); await tester.tap(find.text(secondaryTabLabel2));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel1: $secondaryTabLabel2 tab'; tabBarViewText = '$primaryTabLabel1: $secondaryTabLabel2 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(primaryTab2); await tester.tap(find.text(primaryTabLabel2));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel1 tab'; tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel1 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(secondaryTab2); await tester.tap(find.text(secondaryTabLabel2));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel2 tab'; tabBarViewText = '$primaryTabLabel2: $secondaryTabLabel2 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(primaryTab3); await tester.tap(find.text(primaryTabLabel3));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel3: $secondaryTabLabel1 tab'; tabBarViewText = '$primaryTabLabel3: $secondaryTabLabel1 tab';
expect(find.text(tabBarViewText), findsOneWidget); expect(find.text(tabBarViewText), findsOneWidget);
await tester.tap(secondaryTab2); await tester.tap(find.text(secondaryTabLabel2));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
tabBarViewText = '$primaryTabLabel3: $secondaryTabLabel2 tab'; tabBarViewText = '$primaryTabLabel3: $secondaryTabLabel2 tab';
......
...@@ -24,10 +24,13 @@ void main() { ...@@ -24,10 +24,13 @@ void main() {
TextButton secondButton = findButton('Banana'); TextButton secondButton = findButton('Banana');
TextButton thirdButton = findButton('Orange'); TextButton thirdButton = findButton('Orange');
const Color selectedColor = Color(0xffef9a9a);
const Color unselectedColor = Color(0x00fffbfe);
/// First button is selected. /// First button is selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xffef9a9a)); expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
/// Tap on second button. /// Tap on second button.
await tester.tap(find.widgetWithText(TextButton, 'Banana')); await tester.tap(find.widgetWithText(TextButton, 'Banana'));
...@@ -38,9 +41,9 @@ void main() { ...@@ -38,9 +41,9 @@ void main() {
thirdButton = findButton('Orange'); thirdButton = findButton('Orange');
/// Only second button is selected. /// Only second button is selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffef9a9a)); expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
}); });
testWidgets('Multi-select ToggleButtons', (WidgetTester tester) async { testWidgets('Multi-select ToggleButtons', (WidgetTester tester) async {
...@@ -59,10 +62,13 @@ void main() { ...@@ -59,10 +62,13 @@ void main() {
TextButton secondButton = findButton('Potatoes'); TextButton secondButton = findButton('Potatoes');
TextButton thirdButton = findButton('Carrots'); TextButton thirdButton = findButton('Carrots');
const Color selectedColor = Color(0xffa5d6a7);
const Color unselectedColor = Color(0x00fffbfe);
/// Second button is selected. /// Second button is selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7)); expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
/// Tap on other two buttons. /// Tap on other two buttons.
await tester.tap(find.widgetWithText(TextButton, 'Tomatoes')); await tester.tap(find.widgetWithText(TextButton, 'Tomatoes'));
...@@ -74,9 +80,9 @@ void main() { ...@@ -74,9 +80,9 @@ void main() {
thirdButton = findButton('Carrots'); thirdButton = findButton('Carrots');
/// All buttons are selected. /// All buttons are selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7)); expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7)); expect(secondButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0xffa5d6a7)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), selectedColor);
}); });
testWidgets('Icon-only ToggleButtons', (WidgetTester tester) async { testWidgets('Icon-only ToggleButtons', (WidgetTester tester) async {
...@@ -95,10 +101,14 @@ void main() { ...@@ -95,10 +101,14 @@ void main() {
TextButton secondButton = findButton(Icons.cloud); TextButton secondButton = findButton(Icons.cloud);
TextButton thirdButton = findButton(Icons.ac_unit); TextButton thirdButton = findButton(Icons.ac_unit);
const Color selectedColor = Color(0xff90caf9);
const Color unselectedColor = Color(0x00fffbfe);
/// Third button is selected. /// Third button is selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(firstButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0xff90caf9)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), selectedColor);
/// Tap on the first button. /// Tap on the first button.
await tester.tap(find.widgetWithIcon(TextButton, Icons.sunny)); await tester.tap(find.widgetWithIcon(TextButton, Icons.sunny));
...@@ -109,9 +119,9 @@ void main() { ...@@ -109,9 +119,9 @@ void main() {
thirdButton = findButton(Icons.ac_unit); thirdButton = findButton(Icons.ac_unit);
/// First button os selected. /// First button os selected.
expect(firstButton.style!.backgroundColor!.resolve(enabled), const Color(0xff90caf9)); expect(firstButton.style!.backgroundColor!.resolve(enabled), selectedColor);
expect(secondButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(secondButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
expect(thirdButton.style!.backgroundColor!.resolve(enabled), const Color(0x00ffffff)); expect(thirdButton.style!.backgroundColor!.resolve(enabled), unselectedColor);
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment