Unverified Commit 872e6048 authored by Hari07's avatar Hari07 Committed by GitHub

Added viewport fraction parameter to tabView (#98512)

parent 078deb92
...@@ -1328,6 +1328,7 @@ class TabBarView extends StatefulWidget { ...@@ -1328,6 +1328,7 @@ class TabBarView extends StatefulWidget {
this.controller, this.controller,
this.physics, this.physics,
this.dragStartBehavior = DragStartBehavior.start, this.dragStartBehavior = DragStartBehavior.start,
this.viewportFraction = 1.0,
}) : assert(children != null), }) : assert(children != null),
assert(dragStartBehavior != null), assert(dragStartBehavior != null),
super(key: key); super(key: key);
...@@ -1358,6 +1359,9 @@ class TabBarView extends StatefulWidget { ...@@ -1358,6 +1359,9 @@ class TabBarView extends StatefulWidget {
/// {@macro flutter.widgets.scrollable.dragStartBehavior} /// {@macro flutter.widgets.scrollable.dragStartBehavior}
final DragStartBehavior dragStartBehavior; final DragStartBehavior dragStartBehavior;
/// {@macro flutter.widgets.pageview.viewportFraction}
final double viewportFraction;
@override @override
State<TabBarView> createState() => _TabBarViewState(); State<TabBarView> createState() => _TabBarViewState();
} }
...@@ -1411,7 +1415,10 @@ class _TabBarViewState extends State<TabBarView> { ...@@ -1411,7 +1415,10 @@ class _TabBarViewState extends State<TabBarView> {
super.didChangeDependencies(); super.didChangeDependencies();
_updateTabController(); _updateTabController();
_currentIndex = _controller!.index; _currentIndex = _controller!.index;
_pageController = PageController(initialPage: _currentIndex!); _pageController = PageController(
initialPage: _currentIndex!,
viewportFraction: widget.viewportFraction,
);
} }
@override @override
......
...@@ -144,10 +144,12 @@ class PageController extends ScrollController { ...@@ -144,10 +144,12 @@ class PageController extends ScrollController {
/// locations used to save scroll offsets. /// locations used to save scroll offsets.
final bool keepPage; final bool keepPage;
/// {@template flutter.widgets.pageview.viewportFraction}
/// The fraction of the viewport that each page should occupy. /// The fraction of the viewport that each page should occupy.
/// ///
/// Defaults to 1.0, which means each page fills the viewport in the scrolling /// Defaults to 1.0, which means each page fills the viewport in the scrolling
/// direction. /// direction.
/// {@endtemplate}
final double viewportFraction; final double viewportFraction;
/// The current page displayed in the controlled [PageView]. /// The current page displayed in the controlled [PageView].
......
...@@ -1017,6 +1017,93 @@ void main() { ...@@ -1017,6 +1017,93 @@ void main() {
expect(position.pixels, 800); expect(position.pixels, 800);
}); });
testWidgets('TabBarView viewportFraction sets PageView viewport fraction', (WidgetTester tester) async {
const Duration animationDuration = Duration(milliseconds: 100);
final List<String> tabs = <String>['A', 'B', 'C'];
final TabController tabController = TabController(
vsync: const TestVSync(),
initialIndex: 1,
length: tabs.length,
animationDuration: animationDuration,
);
await tester.pumpWidget(boilerplate(
child: Column(
children: <Widget>[
TabBar(
tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
controller: tabController,
),
SizedBox(
width: 400.0,
height: 400.0,
child: TabBarView(
viewportFraction: 0.8,
controller: tabController,
children: const <Widget>[
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<String> tabs = <String>['A', 'B', 'C'];
final TabController tabController = TabController(
vsync: const TestVSync(),
initialIndex: 1,
length: tabs.length,
animationDuration: animationDuration,
);
await tester.pumpWidget(boilerplate(
child: Column(
children: <Widget>[
TabBar(
tabs: tabs.map<Widget>((String tab) => Tab(text: tab)).toList(),
controller: tabController,
),
SizedBox(
width: 400.0,
height: 400.0,
child: TabBarView(
controller: tabController,
children: const <Widget>[
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 { testWidgets('TabBar tap skips indicator animation when disabled in controller', (WidgetTester tester) async {
final List<String> tabs = <String>['A', 'B']; final List<String> tabs = <String>['A', 'B'];
......
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