0
点赞
收藏
分享

微信扫一扫

Flutter LateInitializationError Field ‘animationStatus‘ has not been initialized。

梅梅的时光 2022-02-26 阅读 173

Flutter 开发遇到错误:

LateInitializationError: Field ‘animationStatus’ has not been initialized。

原因是在初始化之前使用了它,我解决这个问题方法是为 AnimationStatus 添加了一个初始状态。

late AnimationStatus animationStatus = AnimationStatus.reverse;

有 bug 的代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyApp>
    with SingleTickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;
  late AnimationStatus animationStatus;
  late double animationValue = 0;

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: const Duration(seconds: 2), vsync: this);
    animation = Tween<double>(begin: 0, end: 300).animate(controller)
      ..addListener(() {
        setState(() {
          animationValue = animation.value;
        });
      })
      ..addStatusListener((AnimationStatus status) {
        setState(() {
          animationStatus = status;
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(top: 100),
        child: Column(
          children: [
            //点击监听
            GestureDetector(
              onTap: () {
                //重置动画
                controller.reset();
                //动画开始
                controller.forward();
              },
              child: Text(
                'Start',
                textDirection: TextDirection.ltr,
              ),
            ),
            Text('State:' + animationStatus.toString(),
                textDirection: TextDirection.ltr),
            Text('Value:' + animationValue.toString(),
                textDirection: TextDirection.ltr),
            //图片小到大的容器,动态设置大小
            Container(
              height: animation.value,
              width: animation.value,
              child: FlutterLogo(),
            )
          ],
        ));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}

修改后的代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyApp>
    with SingleTickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;
  //设置了一个初始状态。
  late AnimationStatus animationStatus = AnimationStatus.reverse;
  late double animationValue = 0;

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: const Duration(seconds: 2), vsync: this);
    animation = Tween<double>(begin: 0, end: 300).animate(controller)
      ..addListener(() {
        setState(() {
          animationValue = animation.value;
        });
      })
      ..addStatusListener((AnimationStatus status) {
        setState(() {
          animationStatus = status;
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.only(top: 100),
        child: Column(
          children: [
            //点击监听
            GestureDetector(
              onTap: () {
                //重置动画
                controller.reset();
                //动画开始
                controller.forward();
              },
              child: Text(
                'Start',
                textDirection: TextDirection.ltr,
              ),
            ),
            Text('State:' + animationStatus.toString(),
                textDirection: TextDirection.ltr),
            Text('Value:' + animationValue.toString(),
                textDirection: TextDirection.ltr),
            //图片小到大的容器,动态设置大小
            Container(
              height: animation.value,
              width: animation.value,
              child: FlutterLogo(),
            )
          ],
        ));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
举报

相关推荐

0 条评论