颤振-闪烁按钮

时间:2018-08-07 18:13:01

标签: timer dart flutter

我需要呼叫用户注意按钮。我想到的第一个想法是添加一个眨眼动画。我真的不知道该怎么做,但是我尝试通过以下代码使其起作用:

Timer timer = new Timer(new Duration(seconds: 1), () {
  //basic logic to change the color variable here
  setState(() {});
});

这很简单,每秒调用setState,然后再次创建小部件。

但是它不起作用,计时器仅被调用一次。而且,除此之外,在setState中调用Timer对我来说似乎是错误的。

有更好的方法吗?

2 个答案:

答案 0 :(得分:4)

您可以使用time.sleep()UPDATE it_time_track SET Monday = (SELECT SUM(Monday) FROM it_time_track ) WHERE Id = 7 小部件以简单的方式实现此目标,这里有代码:

AnimationController

用法:

Opacity

Working sample gif

答案 1 :(得分:4)

您也可以使用这种方法来做到这一点。 我的逻辑略有不同,我使用的是替代动画。一旦动画完成向前,我就会向后移动。

哪个对视力有益

即:

前进->后退

后退->前进

以此类推

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();
  }
}