如果有动画小部件,Flutter不会重建

问题描述

我在Flutter中遇到了一个奇怪的问题。在我的应用中,在初始化模型和其他内容之前,我会显示一个初始屏幕,然后在应用期间显示一个不同的Widget。

一切都会按预期进行-除非第一个窗口小部件包含动画对象(例如CircularProgressIndicator),否则!在这种情况下,即使我从build起几秒钟后调用setState,也不会再调用setState时再次调用Timer方法。

如果我在IDE中单击“热重启”或“热加载”,则会按预期重建应用。如果第一个窗口小部件不包含CircularProgressIndicator或任何其他动画窗口小部件,它也将起作用。

最小示例:

import 'dart:async';
import 'package:flutter/material.dart';

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

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

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


class _MyAppState extends State<MyApp> {
  bool splash = true;

  @override
  void initState() {
    super.initState();

    Timer(Duration(milliseconds: 3000),(){
      if( mounted ) setState((){
        print('Enough splashing!');
        splash = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    print('build   splash: $splash');

    return MaterialApp(
      title: 'FlutterTest',theme: ThemeData(
        primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,),home: Scaffold(
        appBar: AppBar(
          title: Text('FlutterTest'),body: Center(
          child: splash
            ? Column(
              mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
                CircularProgressIndicator(strokeWidth: 1,valueColor: AlwaysStoppedAnimation<Color>(Colors.red)),Text('Splash...',style: TextStyle(color: Colors.red))
              ],)
            : Column(
              mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
                Text('FlutterTest',style: TextStyle(color: Colors.blue)),],)
    );
  }
}

存在CircularProgressIndicator的结果:

I/flutter (19306): build   splash: true
I/flutter (19306): Enough splashing!

没有CircularProgressIndicator或在“热刷新” /“热刷新”之后的结果:

I/flutter (19436): build   splash: true
I/flutter (19436): Enough splashing!
I/flutter (19436): build   splash: false    

这是怎么回事?

我在调试模式下的Android仿真器上看到了这一点。

flutter doctor -v

[✓] Flutter (Channel stable,1.20.3,on Mac OS X 10.15.1 19B88,locale en-SE)
    • Flutter version 1.20.3 at /Users/username/dev/flutter
    • Framework revision 216dee60c0 (5 weeks ago),2020-09-01 12:24:47 -0700
    • Engine revision d1bc06f032
    • Dart version 2.9.2

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/username/Library/Android/sdk
    • Platform android-30,build-tools 30.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.1,Build version 11A1027
    • CocoaPods version 1.8.4

[!] Android Studio (version 3.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1136-b06)

[✓] IntelliJ IDEA Community Edition (version 2018.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 32.0.2
    • Dart plugin version 183.4733

[✓] Connected device (1 available)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)

更新

在另一台计算机上对其进行了尝试,并且可以正常工作。那台计算机的Flutter版本为1.12.13 + hotfix.8,因此我尝试在无法正常工作的计算机上将Flutter更新到1.22。结果还是一样。我检查了模拟器-它们在两台计算机上都运行完全相同的API,系统映像,模拟器版本和相同的HAXM版本。

在出现故障的计算机上,我尝试使用较低的Android API-它可以正常工作。因此,我尝试为API 30更新系统映像修订版-对于新的AVD仍然是相同的问题。作为最后的选择,我将模拟器和HAXM更新到最新版本。现在它可以在API 30上运行。因此,即使完全相同的版本在另一台计算机上也可以使用,但问题似乎出在模拟器/ HAXM中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)