mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

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
107 lines
3.5 KiB
Dart
107 lines
3.5 KiB
Dart
// 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'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|