Not*_*ros 3 timer dart flutter
我需要呼叫用户注意按钮。我想到的第一个想法是添加一个眨眼动画。我真的不知道该怎么做,但是我尝试通过以下代码使其工作:
Timer timer = new Timer(new Duration(seconds: 1), () {
//basic logic to change the color variable here
setState(() {});
});
Run Code Online (Sandbox Code Playgroud)
这很简单,每秒都会setState调用一次,然后再次创建小部件。
但这不起作用,计时器仅被调用一次。而且,除此之外,调用setState内Timer看来我错了。
有更好的方法吗?
nit*_*k72 10
你也可以用这种方法做到这一点。我的逻辑有点不同,我使用替代动画。一旦动画向前完成,我就会向后退。
哪个对视力好
IE:
向前 -> 向后
向后 -> 向前
等等
import 'package:flutter/material.dart';
import 'package:flutter/animation.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Wordpress App',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new BlinkAnimation(),
);
}
}
class BlinkAnimation extends StatefulWidget {
@override
_BlinkAnimationState createState() => _BlinkAnimationState();
}
class _BlinkAnimationState extends State<BlinkAnimation>
with SingleTickerProviderStateMixin {
Animation<Color> animation;
AnimationController controller;
initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 500), vsync: this);
final CurvedAnimation curve =
CurvedAnimation(parent: controller, curve: Curves.linear);
animation =
ColorTween(begin: Colors.white, end: Colors.blue).animate(curve);
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
controller.forward();
}
setState(() {});
});
controller.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text('Blink Animation'),
),
body: new Center(
child: AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget child) {
return new Container(
child: new RaisedButton(
color: animation.value,
onPressed: () {
controller.forward();
},
child: Text('Blink Animation'),
),
);
},
),
),
);
}
dispose() {
controller.dispose();
super.dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用AnimationController和Opacity小部件轻松实现此目的,这里有代码:
class MyBlinkingButton extends StatefulWidget {
@override
_MyBlinkingButtonState createState() => _MyBlinkingButtonState();
}
class _MyBlinkingButtonState extends State<MyBlinkingButton>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
@override
void initState() {
_animationController =
new AnimationController(vsync: this, duration: Duration(seconds: 1));
_animationController.repeat();
super.initState();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animationController,
child: MaterialButton(
onPressed: () => null,
child: Text("Text button"),
color: Colors.green,
),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: Material(child: Center(child: MyBlinkingButton())));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3071 次 |
| 最近记录: |