首页 > 解决方案 > 如何使用 OnPressed FAB 按钮在 Flutter 中设置计时器倒计时 30 秒?

问题描述

目前,我已经编写了用两个按钮组成计数器应用程序的代码。1 个用于重置的凸起按钮和一个用于增量计数器的 fab 按钮。

是否可以添加倒数计时器以在 FAB 按钮上实现?当 FAB 按钮单击 20 秒倒计时计时器启动。

另外,我在下面的线程中找到了相同类型的函数实现。但我没有在我的应用程序中放置代码以在 FAB 按钮上实现倒计时工作。

如何在flutter中进行倒计时?

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Counter App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Counter App'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  int _counter = 0;
  AnimationController controller;
  bool _isButtonDisabled;

  Duration get duration => controller.duration * controller.value;

  bool get expired => duration.inSeconds == 0;

  @override
  void initState() {
    controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 20),
    );

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'number $_counter added',
            ),
            AnimatedBuilder(
                animation: controller,
                builder: (BuildContext context, Widget child) {
                  return new Text(
                    '${duration.inSeconds}',
                    style: new TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 50.0,
                    ),
                  );
                }),
            new Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                new RaisedButton(
                  padding: const EdgeInsets.all(15.0),
                  textColor: Colors.white,
                  color: Colors.redAccent,
                  onPressed: () {
                    setState(() {
                      controller.reset();
                      _counter = 0;
                    });
                  },
                  child: new Text("Reset"),
                ),
                new RaisedButton(
                  onPressed: () => setState(() {
                    controller.reverse(from: 1);
                  }),
                  textColor: Colors.white,
                  color: Colors.purple,
                  padding: const EdgeInsets.all(15.0),
                  child: new Text(
                    "Start",
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Container(
          height: 50.0,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() {
          _counter++;
        }),
        tooltip: 'Increment Counter',
        child: Icon(Icons.add),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

标签: androiddartflutterflutter-layoutflutter-test

解决方案


您可以在任何地方使用此代码。

Timer(Duration(seconds: 30), () {
      //checkFirstSeen(); your logic
    });

推荐阅读