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,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));
});
}