android 倒计时30秒,android - 如何使用OnPressed FAB按钮在颤振中设置定时器倒计时30秒? - 堆栈内存溢出...

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

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

另外,我发现下面的线程用于相同类型的功能工具。 但是我没有在我的应用程序中放置代码的地方来实现FAB按钮的倒计时工作。

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 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: [

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: [

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,

);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值