Introduce TabBar.indicatorAnimation to customize tab indicator animation (#151746)

fixes [Add ability to customize `TabBar` indicator animation](https://github.com/flutter/flutter/issues/150508)

Similar option exist on Android
https://developer.android.com/reference/com/google/android/material/tabs/TabLayout#setTabIndicatorAnimationMode(int)

### Dartpad Example Preview
<img width="874" alt="Screenshot 2024-07-12 at 17 36 08" src="https://github.com/user-attachments/assets/e349c5aa-ee5d-46ce-9e44-4f02346603bd">

### Linear vs Elastic tab indicator animation

https://github.com/user-attachments/assets/d7ae3ae4-ae52-4ccd-89b1-75908bf8a34d
This commit is contained in:
Taha Tesser
2024-07-26 14:54:28 +03:00
committed by GitHub
parent 98c5e683fd
commit bba6ea9a2d
6 changed files with 474 additions and 9 deletions

View File

@@ -0,0 +1,106 @@
// Copyright 2014 The Flutter 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/material.dart';
/// Flutter code sample for [TabBar.indicatorAnimation].
void main() => runApp(const IndicatorAnimationExampleApp());
class IndicatorAnimationExampleApp extends StatelessWidget {
const IndicatorAnimationExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: IndicatorAnimationExample(),
);
}
}
const List<(TabIndicatorAnimation, String)> indicatorAnimationSegments = <(TabIndicatorAnimation, String)>[
(TabIndicatorAnimation.linear, 'Linear'),
(TabIndicatorAnimation.elastic, 'Elastic'),
];
class IndicatorAnimationExample extends StatefulWidget {
const IndicatorAnimationExample({super.key});
@override
State<IndicatorAnimationExample> createState() => _IndicatorAnimationExampleState();
}
class _IndicatorAnimationExampleState extends State<IndicatorAnimationExample> {
Set<TabIndicatorAnimation> _animationStyleSelection = <TabIndicatorAnimation>{TabIndicatorAnimation.linear};
TabIndicatorAnimation _tabIndicatorAnimation = TabIndicatorAnimation.linear;
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 6,
child: Scaffold(
appBar: AppBar(
title: const Text('Indicator Animation Example'),
bottom: TabBar(
indicatorAnimation: _tabIndicatorAnimation,
isScrollable: true,
tabAlignment: TabAlignment.start,
tabs: const <Widget>[
Tab(text: 'Short Tab'),
Tab(text: 'Very Very Very Long Tab'),
Tab(text: 'Short Tab'),
Tab(text: 'Very Very Very Long Tab'),
Tab(text: 'Short Tab'),
Tab(text: 'Very Very Very Long Tab'),
],
),
),
body: Column(
children: <Widget>[
const SizedBox(height: 16),
SegmentedButton<TabIndicatorAnimation>(
selected: _animationStyleSelection,
onSelectionChanged: (Set<TabIndicatorAnimation> styles) {
setState(() {
_animationStyleSelection = styles;
_tabIndicatorAnimation = styles.first;
});
},
segments: indicatorAnimationSegments
.map<ButtonSegment<TabIndicatorAnimation>>(((TabIndicatorAnimation, String) shirt) {
return ButtonSegment<TabIndicatorAnimation>(value: shirt.$1, label: Text(shirt.$2));
})
.toList(),
),
const SizedBox(height: 16),
const Expanded(
child: TabBarView(
children: <Widget>[
Center(
child: Text('Short Tab Page'),
),
Center(
child: Text('Very Very Very Long Tab Page'),
),
Center(
child: Text('Short Tab Page'),
),
Center(
child: Text('Very Very Very Long Tab Page'),
),
Center(
child: Text('Short Tab Page'),
),
Center(
child: Text('Very Very Very Long Tab Page'),
),
],
),
),
],
),
),
);
}
}

View File

@@ -0,0 +1,85 @@
// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter_api_samples/material/tabs/tab_bar.indicator_animation.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('TabBar.indicatorAnimation can customize tab indicator animation', (WidgetTester tester) async {
await tester.pumpWidget(
const example.IndicatorAnimationExampleApp(),
);
final RenderBox tabBarBox = tester.firstRenderObject<RenderBox>(find.byType(TabBar));
late RRect indicatorRRect;
expect(tabBarBox, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawRRect) {
return false;
}
indicatorRRect = arguments[0] as RRect;
return true;
}));
expect(indicatorRRect.left, equals(16.0));
expect(indicatorRRect.top, equals(45.0));
expect(indicatorRRect.right, closeTo(142.9, 0.1));
expect(indicatorRRect.bottom, equals(48.0));
// Tap the long tab.
await tester.tap(find.text('Very Very Very Long Tab').first);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(tabBarBox, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawRRect) {
return false;
}
indicatorRRect = arguments[0] as RRect;
return true;
}));
expect(indicatorRRect.left, closeTo(107.5, 0.1));
expect(indicatorRRect.top, equals(45.0));
expect(indicatorRRect.right, closeTo(348.2, 0.1));
expect(indicatorRRect.bottom, equals(48.0));
// Tap to go to the first tab.
await tester.tap(find.text('Short Tab').first);
await tester.pumpAndSettle();
expect(tabBarBox, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawRRect) {
return false;
}
indicatorRRect = arguments[0] as RRect;
return true;
}));
expect(indicatorRRect.left, equals(16.0));
expect(indicatorRRect.top, equals(45.0));
expect(indicatorRRect.right, closeTo(142.9, 0.1));
expect(indicatorRRect.bottom, equals(48.0));
// Select the elastic animation.
await tester.tap(find.text('Elastic'));
await tester.pumpAndSettle();
// Tap the long tab.
await tester.tap(find.text('Very Very Very Long Tab').first);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(tabBarBox, paints..something((Symbol method, List<dynamic> arguments) {
if (method != #drawRRect) {
return false;
}
indicatorRRect = arguments[0] as RRect;
return true;
}));
expect(indicatorRRect.left, closeTo(51.0, 0.1));
expect(indicatorRRect.top, equals(45.0));
expect(indicatorRRect.right, closeTo(221.4, 0.1));
expect(indicatorRRect.bottom, equals(48.0));
});
}