diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 1d29800357..c4fd1b108a 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -1328,6 +1328,7 @@ class TabBarView extends StatefulWidget { this.controller, this.physics, this.dragStartBehavior = DragStartBehavior.start, + this.viewportFraction = 1.0, }) : assert(children != null), assert(dragStartBehavior != null), super(key: key); @@ -1358,6 +1359,9 @@ class TabBarView extends StatefulWidget { /// {@macro flutter.widgets.scrollable.dragStartBehavior} final DragStartBehavior dragStartBehavior; + /// {@macro flutter.widgets.pageview.viewportFraction} + final double viewportFraction; + @override State createState() => _TabBarViewState(); } @@ -1411,7 +1415,10 @@ class _TabBarViewState extends State { super.didChangeDependencies(); _updateTabController(); _currentIndex = _controller!.index; - _pageController = PageController(initialPage: _currentIndex!); + _pageController = PageController( + initialPage: _currentIndex!, + viewportFraction: widget.viewportFraction, + ); } @override diff --git a/packages/flutter/lib/src/widgets/page_view.dart b/packages/flutter/lib/src/widgets/page_view.dart index c804607902..358453fb42 100644 --- a/packages/flutter/lib/src/widgets/page_view.dart +++ b/packages/flutter/lib/src/widgets/page_view.dart @@ -144,10 +144,12 @@ class PageController extends ScrollController { /// locations used to save scroll offsets. final bool keepPage; + /// {@template flutter.widgets.pageview.viewportFraction} /// The fraction of the viewport that each page should occupy. /// /// Defaults to 1.0, which means each page fills the viewport in the scrolling /// direction. + /// {@endtemplate} final double viewportFraction; /// The current page displayed in the controlled [PageView]. diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index f8ff769949..a047b9e6ed 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -1017,6 +1017,93 @@ void main() { expect(position.pixels, 800); }); + testWidgets('TabBarView viewportFraction sets PageView viewport fraction', (WidgetTester tester) async { + const Duration animationDuration = Duration(milliseconds: 100); + final List tabs = ['A', 'B', 'C']; + + final TabController tabController = TabController( + vsync: const TestVSync(), + initialIndex: 1, + length: tabs.length, + animationDuration: animationDuration, + ); + await tester.pumpWidget(boilerplate( + child: Column( + children: [ + TabBar( + tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + controller: tabController, + ), + SizedBox( + width: 400.0, + height: 400.0, + child: TabBarView( + viewportFraction: 0.8, + controller: tabController, + children: const [ + Center(child: Text('0')), + Center(child: Text('1')), + Center(child: Text('2')), + ], + ), + ), + ], + ), + )); + + expect(tabController.index, 1); + + final PageView pageView = tester.widget(find.byType(PageView)); + final PageController pageController = pageView.controller; + + // The TabView was initialized with viewportFraction as 0.8 + // So it's expected the PageView inside would obtain the same viewportFraction + expect(pageController.viewportFraction, 0.8); + }); + + testWidgets('TabBarView viewportFraction is 1 by default', (WidgetTester tester) async { + const Duration animationDuration = Duration(milliseconds: 100); + final List tabs = ['A', 'B', 'C']; + + final TabController tabController = TabController( + vsync: const TestVSync(), + initialIndex: 1, + length: tabs.length, + animationDuration: animationDuration, + ); + await tester.pumpWidget(boilerplate( + child: Column( + children: [ + TabBar( + tabs: tabs.map((String tab) => Tab(text: tab)).toList(), + controller: tabController, + ), + SizedBox( + width: 400.0, + height: 400.0, + child: TabBarView( + controller: tabController, + children: const [ + Center(child: Text('0')), + Center(child: Text('1')), + Center(child: Text('2')), + ], + ), + ), + ], + ), + )); + + expect(tabController.index, 1); + + final PageView pageView = tester.widget(find.byType(PageView)); + final PageController pageController = pageView.controller; + + // The TabView was initialized with default viewportFraction + // So it's expected the PageView inside would obtain the value 1 + expect(pageController.viewportFraction, 1); + }); + testWidgets('TabBar tap skips indicator animation when disabled in controller', (WidgetTester tester) async { final List tabs = ['A', 'B'];