performance - Flutter 低帧率性能

标签 performance flutter

当我启动 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/

相关文章:

jQuery 选择器效率

android - Phonegap VS 类固醇。我选对了吗?

java - 代码效率、调试、对象方法、数组

c - 全局变量性能效果(c、c++)

flutter - 根据屏幕尺寸控制元素尺寸

string - 如何在dart中使用关系运算符比较日期?

Flutter 应用程序错误 - 类型 'Timestamp' 不是类型 'DateTime' 的子类型

firebase - 无法安装 FlutterFire CLI

python - 从python中的特定reddit线程获取所有评论

flutter - 使用Futurebuilder有条件地渲染小部件