我有一个带有 3 个标签的标签栏。当从第一个选项卡更改为第三个选项卡时,还会调用 tab2 的 initState 方法。我不想要那个。
import 'package:flutter/material.dart';
void main() {
runApp(TabBarDemo());
}
class TabBarDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
),
);
}
}
在 tab1 上并单击 tab3 时,我希望立即显示 tab3。没有动画。
最佳答案
我还没有测试过这个,但你可以尝试设置一个自定义 TabController
到 TabBar
和 TabBarView
,它覆盖了 animateTo
跳过动画的方法:
class CustomTabController extends TabController {
@override
void animateTo(int value, {Duration duration = kTabScrollDuration, Curve curve = Curves.ease}) {
super.animateTo(value, duration: null, curve: null);
}
}
关于flutter - 在 Flutter 中更改选项卡时禁用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60545618/