import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(title: Text('倒计时'),),
body: MyHomePage(),
)
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Duration time;
var seconds = 0;
Timer countdownTimer;
@override
Widget build(BuildContext context) {
return Container(
child:Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('定时'),
onPressed: () {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: CupertinoColors.white,
child: DefaultTextStyle(
style: const TextStyle(
color: CupertinoColors.black,
fontSize: 22.0,
),
child: CupertinoTimerPicker(
//initialTimerDuration: time,
//minuteInterval: 5,
mode: CupertinoTimerPickerMode.ms,
onTimerDurationChanged: (Duration newTimer) {
setState(() {
time = newTimer;
seconds = time.inSeconds;
// flag = true;
});
},
),
));
},
);
},
),
RaisedButton(
child: Text('开始倒计时'),
onPressed: () {
if (countdownTimer != null) {
return;
}
countdownTimer =
new Timer.periodic(new Duration(seconds: 1), (timer) {
setState(() {
if (seconds > 0) {
seconds--;
} else {
countdownTimer.cancel();
countdownTimer = null;
}
});
});
},
),
Text(
'倒计时: $seconds' + '秒',
style: TextStyle(fontSize: 30),
),
],
),
)
);
}
}
Flutter 倒计时功能
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 写一个倒计时定时器听起来真的好简单,然而在Flutter里面写这个东西还是挺坑的。 原本以为创建一个Timer就一...