gpt4 book ai didi

dart - 滞后动画

转载 作者:IT王子 更新时间:2023-10-29 06:46:09 24 4
gpt4 key购买 nike

有没有人在使用 TabBar 时经历过屏幕之间的过渡滞后,或者我用错了?基本上我通过在其他小部件中构建页面来使用它

home: DefaultTabController(
initialIndex: 0,
length: 5,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
isScrollable: true,
labelStyle: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),
tabs: [
Tab(text: 'HOME'),
Tab(text: 'SCHEDULE'),
Tab(text: 'ANALYTICS'),
Tab(text: 'TEMP'),
Tab(text: 'SETTINGS'),
],
),
title: Text('${widget.title}'),
),
body: TabBarView(
children: [
TheGridView().build(),
SchedulePage(),
ChartPage(),
TempPage(),
SettingsPage(),
],
),
),
),

最佳答案

TabBarView 不保存小部件的状态。每次重新出现在屏幕上时,它们都会被重建。您可以通过在 initState() 中为其中一个小部件添加 print() 语句来检查这一点。要解决此问题,您可以使用 AutomaticKeepAliveClientMixin

class SettingsPage extends StatefulWidget {
SettingsPage({Key key}) : super(key: key);

@override
_SettingsPageState createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> with AutomaticKeepAliveClientMixin { //<-- AutomaticKeepAliveClientMixin

@override
bool get wantKeepAlive => true; //Set to true

@override
Widget build(BuildContext context) {
super.build(context); //You must add this
return Container(); //A screen here
}
}

有了这个小改动,小部件将不会在每次返回屏幕时都重建

关于dart - 滞后动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51543431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com