要自动显示一个dialog,但是,有动画在运行,不断地调用setstate,然后调用build,最终导致直接背景成为黑色的,dialog也在不断重构
解决方法是显示dialog的时候让动画暂停
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_localizations/flutter_localizations.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State
with SingleTickerProviderStateMixin {
int _counter = 0;
AnimationController animationController;
Animation animation;
bool showing=false;
bool first=true;
@override
void initState() {
// TODO: implement initState
super.initState();
animationController = new AnimationController(
vsync: this, duration: Duration(milliseconds: 3000));
animation = Tween(begin: 1.0, end: 2.0).animate(CurvedAnimation(
parent: animationController, curve: Curves.fastOutSlowIn))
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationController.reverse();
}
if (status == AnimationStatus.dismissed) {
animationController.forward();
}
})
..addListener(() {
if (!showing){
setState(() {});
}
// setState(() {});
});
animationController.repeat();
print("rrrrrrrrrrrrrrrrr");
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
void setState(fn) {
// TODO: implement setState
super.setState(fn);
print("ppppppppppppppp");
}
@override
void didUpdateWidget(MyHomePage oldWidget) {
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
print("11111111111111");
}
@override
Widget build(BuildContext context) {
print("22222222222222");
if(first==true){
Future.delayed(Duration.zero, () => showMyDialog(context));
first=false;
}
return new MaterialApp(
// localizationsDelegates: [
// // ... app-specific localization delegate[s] here
// GlobalMaterialLocalizations.delegate,
// GlobalWidgetsLocalizations.delegate,
// ],
// supportedLocales: [
// const Locale('zh', 'CH'),
// ],
home: new Scaffold(
body: new Center(
child: new Stack(
children: [
Transform.translate(
offset: Offset(animation.value, animation.value),
child: Image.asset(
"assets/cute1.jpg",
width: 21.0,
height: 21.0,
fit: BoxFit.fill,
),
),
Transform.translate(
offset: Offset(animation.value * 2, animation.value * 2),
child: Image.asset(
"assets/add.png",
width: 21.0,
height: 21.0,
fit: BoxFit.fill,
),
),
// new RaisedButton(
// child: new Text('提交'),
// onPressed: () {
// showMyDialog(context);
// }
// )
],
)),
floatingActionButton: new FloatingActionButton(
onPressed: (){showMyDialog(context);},
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
));
// return
}
void showMyDialog(BuildContext context) {
showing=true;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: const Text(
'Message',
),
actions: [
FlatButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop(true);
showing=false;
},
),
],
);
// return new SimpleDialog(
// title: new Text('Test'),
// children: [
// new RadioListTile(
// title: new Text('Testing'), value: null, groupValue: null, onChanged: (value) {},
// )
// ],
// );
// return
},
);
}
}