gpt4 book ai didi

performance - Flutter 低帧率性能

转载 作者:IT王子 更新时间:2023-10-29 07:11:08 25 4
gpt4 key购买 nike

当我启动 speed_dial 动画插件时,我看到帧率 UI 线性下降。当我在这里添加 sharedpref 函数时出现问题:

 @override
Widget build(BuildContext context) {
sharedpref_function();
return Scaffold(

听一个保存的值,即使 sharedpref 是空的,我也会有这种退化。

10 分钟后,之前什么都不做,我在调用 _renderSpeedDial 时测得 1120 毫秒/帧

完整代码如下:

  bool _dialVisible = true;
Color _speedDial = Colors.pink;

sharedpref_function() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
}
);
}

_renderSpeedDial() {
return SpeedDial(
animatedIcon: AnimatedIcons.add_event,
animatedIconTheme: IconThemeData(size: 22.0),
backgroundColor: _speedDial,
// child: Icon(Icons.add),
/* onOpen: () => print('OPENING DIAL'),
onClose: () => print('DIAL CLOSED'),*/
visible: _dialVisible,
curve: Curves.bounceIn,
children: [

SpeedDialChild(
child: Icon(Icons.fullscreen_exit, color: Colors.white),
backgroundColor: Color(0xffa088df),
onTap: () {
setState(() {

});
},
label: '1',
labelStyle: TextStyle(fontWeight: FontWeight.w500,color: Colors.white),
labelBackgroundColor:Color(0xffa088df),
),

],
);
}

@override
Widget build(BuildContext context) {
sharedpref_function(); // here the sharedpref I use to listen saved value
return Scaffold(
body: Stack(
children: <Widget>[
Padding
(
padding: const EdgeInsets.only(right:10.0, bottom:10.0),
child:
_renderSpeedDial(),
),
],
)
);
}
}

最佳答案

您的sharedpref_function() 方法在您的build 方法中被调用。根本不推荐这样做,因为它会在 UI 需要重建的每一帧上调用,并且您的代码(那里有动画)将以 60fps(在每一帧上)被调用。

将您的方法移到 initStatedidChangeDependencies 中(还有更多方法只调用一次或几次,例如 didChangeDependencies)。

当您需要更新值时,您可以在 onTap 手势内完成,仅此而已。

此外,在 --release( Release模式)中测试您的应用,以真正测试您的应用的速度。

关于performance - Flutter 低帧率性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56776196/

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