如何解决如果有动画小部件,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 举报,一经查实,本站将立刻删除。