diff --git a/packages/flutter/lib/src/cupertino/tab_scaffold.dart b/packages/flutter/lib/src/cupertino/tab_scaffold.dart index 043fda3b09..b478eb81e2 100644 --- a/packages/flutter/lib/src/cupertino/tab_scaffold.dart +++ b/packages/flutter/lib/src/cupertino/tab_scaffold.dart @@ -194,14 +194,12 @@ class _CupertinoTabScaffoldState extends State { tabNumber: widget.tabBar.items.length, tabBuilder: widget.tabBuilder, ); + EdgeInsets contentPadding = EdgeInsets.zero; if (widget.resizeToAvoidBottomInset) { // Remove the view inset and add it back as a padding in the inner content. newMediaQuery = newMediaQuery.removeViewInsets(removeBottom: true); - content = Padding( - padding: EdgeInsets.only(bottom: existingMediaQuery.viewInsets.bottom), - child: content, - ); + contentPadding = EdgeInsets.only(bottom: existingMediaQuery.viewInsets.bottom); } if (widget.tabBar != null && @@ -219,10 +217,7 @@ class _CupertinoTabScaffoldState extends State { // translucent, let main content draw behind the tab bar but hint the // obstructed area. if (widget.tabBar.opaque(context)) { - content = Padding( - padding: EdgeInsets.only(bottom: bottomPadding), - child: content, - ); + contentPadding = EdgeInsets.only(bottom: bottomPadding); } else { newMediaQuery = newMediaQuery.copyWith( padding: newMediaQuery.padding.copyWith( @@ -234,7 +229,10 @@ class _CupertinoTabScaffoldState extends State { content = MediaQuery( data: newMediaQuery, - child: content, + child: Padding( + padding: contentPadding, + child: content, + ), ); // The main content being at the bottom is added to the stack first. diff --git a/packages/flutter/test/cupertino/README.md b/packages/flutter/test/cupertino/README.md index 9fa54b1a40..dea158134e 100644 --- a/packages/flutter/test/cupertino/README.md +++ b/packages/flutter/test/cupertino/README.md @@ -3,5 +3,8 @@ Avoid importing the Material 'package:flutter/material.dart' in these tests as we're trying to test the Cupertino package in standalone scenarios. -Some tests may be replicated in the Material tests when Material reuses +The 'material' subdirectory contains tests for cross-interactions of Material +Cupertino widgets in hybridized apps. + +Some tests may also be replicated in the Material tests when Material reuses Cupertino components on iOS such as page transitions and text editing. diff --git a/packages/flutter/test/cupertino/material/README.md b/packages/flutter/test/cupertino/material/README.md new file mode 100644 index 0000000000..d927fbcc32 --- /dev/null +++ b/packages/flutter/test/cupertino/material/README.md @@ -0,0 +1,4 @@ +# Tests for the Cupertino+Material mixed usages + +In this package, we test for interactions between Material and Cupertino +widgets. diff --git a/packages/flutter/test/cupertino/material/tab_scaffold_test.dart b/packages/flutter/test/cupertino/material/tab_scaffold_test.dart new file mode 100644 index 0000000000..facd118008 --- /dev/null +++ b/packages/flutter/test/cupertino/material/tab_scaffold_test.dart @@ -0,0 +1,241 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../painting/mocks_for_image_cache.dart'; + +List selectedTabs; + +void main() { + setUp(() { + selectedTabs = []; + }); + + testWidgets('Last tab gets focus', (WidgetTester tester) async { + // 2 nodes for 2 tabs + final List focusNodes = [FocusNode(), FocusNode()]; + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return TextField( + focusNode: focusNodes[index], + autofocus: true, + ); + }, + ), + ), + ), + ); + + expect(focusNodes[0].hasFocus, isTrue); + + await tester.tap(find.text('Tab 2')); + await tester.pump(); + + expect(focusNodes[0].hasFocus, isFalse); + expect(focusNodes[1].hasFocus, isTrue); + + await tester.tap(find.text('Tab 1')); + await tester.pump(); + + expect(focusNodes[0].hasFocus, isTrue); + expect(focusNodes[1].hasFocus, isFalse); + }); + + testWidgets('Do not affect focus order in the route', (WidgetTester tester) async { + final List focusNodes = [ + FocusNode(), FocusNode(), FocusNode(), FocusNode(), + ]; + + await tester.pumpWidget( + MaterialApp( + home: Material( + child: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return Column( + children: [ + TextField( + focusNode: focusNodes[index * 2], + decoration: const InputDecoration( + hintText: 'TextField 1', + ), + ), + TextField( + focusNode: focusNodes[index * 2 + 1], + decoration: const InputDecoration( + hintText: 'TextField 2', + ), + ), + ], + ); + }, + ), + ), + ), + ); + + expect( + focusNodes.any((FocusNode node) => node.hasFocus), + isFalse, + ); + + await tester.tap(find.widgetWithText(TextField, 'TextField 2')); + + expect( + focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)), + 1, + ); + + await tester.tap(find.text('Tab 2')); + await tester.pump(); + + await tester.tap(find.widgetWithText(TextField, 'TextField 1')); + + expect( + focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)), + 2, + ); + + await tester.tap(find.text('Tab 1')); + await tester.pump(); + + // Upon going back to tab 1, the item it tab 1 that previously had the focus + // (TextField 2) gets it back. + expect( + focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)), + 1, + ); + }); + + testWidgets('Tab bar respects themes', (WidgetTester tester) async { + await tester.pumpWidget( + CupertinoApp( + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const Placeholder(); + }, + ), + ), + ); + + BoxDecoration tabDecoration = tester.widget(find.descendant( + of: find.byType(CupertinoTabBar), + matching: find.byType(DecoratedBox), + )).decoration; + + expect(tabDecoration.color, const Color(0xCCF8F8F8)); + + await tester.tap(find.text('Tab 2')); + await tester.pump(); + + // Pump again but with dark theme. + await tester.pumpWidget( + CupertinoApp( + theme: const CupertinoThemeData( + brightness: Brightness.dark, + primaryColor: CupertinoColors.destructiveRed, + ), + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const Placeholder(); + }, + ), + ), + ); + + tabDecoration = tester.widget(find.descendant( + of: find.byType(CupertinoTabBar), + matching: find.byType(DecoratedBox), + )).decoration; + + expect(tabDecoration.color, const Color(0xB7212121)); + + final RichText tab1 = tester.widget(find.descendant( + of: find.text('Tab 1'), + matching: find.byType(RichText), + )); + // Tab 2 should still be selected after changing theme. + expect(tab1.text.style.color, CupertinoColors.inactiveGray); + final RichText tab2 = tester.widget(find.descendant( + of: find.text('Tab 2'), + matching: find.byType(RichText), + )); + expect(tab2.text.style.color, CupertinoColors.destructiveRed); + }); + + testWidgets('Does not lose state when focusing on text input', (WidgetTester tester) async { + // Regression testing for https://github.com/flutter/flutter/issues/28457. + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewInsets: EdgeInsets.only(bottom: 0), + ), + child: MaterialApp( + home: Material( + child: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const TextField(); + }, + ), + ), + ), + ), + ); + + final EditableTextState editableState = tester.state(find.byType(EditableText)); + + await tester.enterText(find.byType(TextField), "don't lose me"); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewInsets: EdgeInsets.only(bottom: 100), + ), + child: MaterialApp( + home: Material( + child: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const TextField(); + }, + ), + ), + ), + ), + ); + + // The exact same state instance is still there. + expect(tester.state(find.byType(EditableText)), editableState); + expect(find.text("don't lose me"), findsOneWidget); + }); +} + +CupertinoTabBar _buildTabBar({ int selectedTab = 0 }) { + return CupertinoTabBar( + items: const [ + BottomNavigationBarItem( + icon: ImageIcon(TestImageProvider(24, 24)), + title: Text('Tab 1'), + ), + BottomNavigationBarItem( + icon: ImageIcon(TestImageProvider(24, 24)), + title: Text('Tab 2'), + ), + ], + currentIndex: selectedTab, + onTap: (int newTab) => selectedTabs.add(newTab), + ); +} diff --git a/packages/flutter/test/cupertino/tab_scaffold_test.dart b/packages/flutter/test/cupertino/tab_scaffold_test.dart index 71291e1864..7b898c8d38 100644 --- a/packages/flutter/test/cupertino/tab_scaffold_test.dart +++ b/packages/flutter/test/cupertino/tab_scaffold_test.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../painting/mocks_for_image_cache.dart'; @@ -111,16 +110,14 @@ void main() { await tester.pumpWidget( CupertinoApp( - home: Material( - child: CupertinoTabScaffold( - tabBar: _buildTabBar(), - tabBuilder: (BuildContext context, int index) { - return TextField( - focusNode: focusNodes[index], - autofocus: true, - ); - }, - ), + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return CupertinoTextField( + focusNode: focusNodes[index], + autofocus: true, + ); + }, ), ), ); @@ -147,28 +144,22 @@ void main() { await tester.pumpWidget( CupertinoApp( - home: Material( - child: CupertinoTabScaffold( - tabBar: _buildTabBar(), - tabBuilder: (BuildContext context, int index) { - return Column( - children: [ - TextField( - focusNode: focusNodes[index * 2], - decoration: const InputDecoration( - hintText: 'TextField 1', - ), - ), - TextField( - focusNode: focusNodes[index * 2 + 1], - decoration: const InputDecoration( - hintText: 'TextField 2', - ), - ), - ], - ); - }, - ), + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return Column( + children: [ + CupertinoTextField( + focusNode: focusNodes[index * 2], + placeholder: 'TextField 1', + ), + CupertinoTextField( + focusNode: focusNodes[index * 2 + 1], + placeholder: 'TextField 2', + ), + ], + ); + }, ), ), ); @@ -178,7 +169,7 @@ void main() { isFalse, ); - await tester.tap(find.widgetWithText(TextField, 'TextField 2')); + await tester.tap(find.widgetWithText(CupertinoTextField, 'TextField 2')); expect( focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)), @@ -188,7 +179,7 @@ void main() { await tester.tap(find.text('Tab 2')); await tester.pump(); - await tester.tap(find.widgetWithText(TextField, 'TextField 1')); + await tester.tap(find.widgetWithText(CupertinoTextField, 'TextField 1')); expect( focusNodes.indexOf(focusNodes.singleWhere((FocusNode node) => node.hasFocus)), @@ -471,6 +462,50 @@ void main() { expect(find.text('Page 2', skipOffstage: false), findsNothing); expect(find.text('Page 4', skipOffstage: false), findsNothing); }); + + testWidgets('Does not lose state when focusing on text input', (WidgetTester tester) async { + // Regression testing for https://github.com/flutter/flutter/issues/28457. + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewInsets: EdgeInsets.only(bottom: 0), + ), + child: CupertinoApp( + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const CupertinoTextField(); + }, + ), + ), + ), + ); + + final EditableTextState editableState = tester.state(find.byType(EditableText)); + + await tester.enterText(find.byType(CupertinoTextField), "don't lose me"); + + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData( + viewInsets: EdgeInsets.only(bottom: 100), + ), + child: CupertinoApp( + home: CupertinoTabScaffold( + tabBar: _buildTabBar(), + tabBuilder: (BuildContext context, int index) { + return const CupertinoTextField(); + }, + ), + ), + ), + ); + + // The exact same state instance is still there. + expect(tester.state(find.byType(EditableText)), editableState); + expect(find.text("don't lose me"), findsOneWidget); + }); } CupertinoTabBar _buildTabBar({ int selectedTab = 0 }) {