Added viewport fraction parameter to tabView (#98512)
This commit is contained in:
@@ -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<TabBarView> createState() => _TabBarViewState();
|
||||
}
|
||||
@@ -1411,7 +1415,10 @@ class _TabBarViewState extends State<TabBarView> {
|
||||
super.didChangeDependencies();
|
||||
_updateTabController();
|
||||
_currentIndex = _controller!.index;
|
||||
_pageController = PageController(initialPage: _currentIndex!);
|
||||
_pageController = PageController(
|
||||
initialPage: _currentIndex!,
|
||||
viewportFraction: widget.viewportFraction,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -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].
|
||||
|
||||
@@ -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<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 {
|
||||
final List<String> tabs = <String>['A', 'B'];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user