Flutter 如何在一行中添加一个倒计时计时器,在颤振中达到零时使用事件触发器?
所以,当我开始在颤振中工作时,我尝试添加一个倒计时计时器,从20:00分钟开始,逐渐倒计时到零 -正在一行中添加计时器 -归零后显示一个弹出窗口 下面是我的代码Flutter 如何在一行中添加一个倒计时计时器,在颤振中达到零时使用事件触发器?,flutter,flutter-layout,Flutter,Flutter Layout,所以,当我开始在颤振中工作时,我尝试添加一个倒计时计时器,从20:00分钟开始,逐渐倒计时到零 -正在一行中添加计时器 -归零后显示一个弹出窗口 下面是我的代码 class TimerPage extends StatefulWidget { TimerPage({Key key}) : super(key: key); TimerPageState createState() => new TimerPageState(); } class TimerPageState ex
class TimerPage extends StatefulWidget {
TimerPage({Key key}) : super(key: key);
TimerPageState createState() => new TimerPageState();
}
class TimerPageState extends State<TimerPage> {
final items = <Widget>[
ListTile(
leading: Icon(Icons.photo_camera),
title: Text('Camera'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.photo_library),
title: Text('Select'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.delete),
title: Text('Delete'),
onTap: () {},
),
Divider(),
if (true)
ListTile(
title: Text('Cancel'),
onTap: () {},
),
];
_showSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext _) {
return Container(
child: Wrap(
children: items,
),
);
},
isScrollControlled: true,
);
}
@override
Widget build(BuildContext context) {
return Column(children: [
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.pause_circle_outline,
color: Colors.blue,
),
onPressed: _showSheet),
Text('20:00') // this is where dynamic text timer will be added
Spacer(),
FlatButton(
onPressed: _showSheet,
child: Text(
"Submit",
style: TextStyle(
fontSize: 18,
fontFamily: 'lato',
color: Colors.blue,
),
)),
],
),
]);
}
}
class TimerPage扩展StatefulWidget{
TimerPage({Key}):超级(Key:Key);
TimerPageState createState()=>新的TimerPageState();
}
类TimerPageState扩展了状态{
最后项目=[
列表砖(
前导:图标(图标、照相/摄像机),
标题:文本(“摄影机”),
onTap:(){},
),
列表砖(
前导:图标(图标。照片库),
标题:文本(“选择”),
onTap:(){},
),
列表砖(
前导:图标(Icons.delete),
标题:文本(“删除”),
onTap:(){},
),
分隔符(),
如果(真)
列表砖(
标题:文本(“取消”),
onTap:(){},
),
];
_展示页(){
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext\ux){
返回容器(
孩子:包裹(
儿童:项目,
),
);
},
是的,
);
}
@凌驾
小部件构建(构建上下文){
返回列(子项:[
划船(
儿童:[
图标按钮(
图标:图标(
图标。暂停\圆圈\轮廓,
颜色:颜色,蓝色,
),
按下按钮:_展示页),
Text('20:00')//这是添加动态文本计时器的地方
垫片(),
扁平按钮(
已按下:_展示页,
子:文本(
“提交”,
样式:TextStyle(
尺码:18,
fontFamily:“lato”,
颜色:颜色,蓝色,
),
)),
],
),
]);
}
}
上面的代码是我正在尝试做的一个正在进行的项目。该项目基本上是停止进行基于时间的考试
另外,如果你能给我推荐一个好的整体教程或文档,我就可以理解颤振将非常棒。试试这个:
class TimerPage extends StatefulWidget {
TimerPage({Key key}) : super(key: key);
TimerPageState createState() => new TimerPageState();
}
class TimerPageState extends State<TimerPage> {
final items = <Widget>[
ListTile(
leading: Icon(Icons.photo_camera),
title: Text('Camera'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.photo_library),
title: Text('Select'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.delete),
title: Text('Delete'),
onTap: () {},
),
Divider(),
if (true)
ListTile(
title: Text('Cancel'),
onTap: () {},
),
];
_showSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext _) {
return Container(
child: Wrap(
children: items,
),
);
},
isScrollControlled: true,
);
}
int _count = 20 * 60;
@override
void initState() {
super.initState();
Timer.periodic(Duration(seconds: 1), (timer) {
--_count;
if (_count < 0)
timer.cancel();
else
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Column(children: [
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.pause_circle_outline,
color: Colors.blue,
),
onPressed: _showSheet),
Text('${_formatSeconds(_count)}'), // this is where dynamic text timer will be added
Spacer(),
FlatButton(
onPressed: _showSheet,
child: Text(
"Submit",
style: TextStyle(
fontSize: 18,
fontFamily: 'lato',
color: Colors.blue,
),
),
),
],
),
]);
}
String _formatSeconds(int count) {
int hours = count ~/ 3600;
int secondsLeft = count - hours * 3600;
int minutes = secondsLeft ~/ 60;
int seconds = secondsLeft - minutes * 60;
String formattedTime = "";
if (minutes < 10) formattedTime += "0";
formattedTime = "$minutes:";
if (seconds < 10) formattedTime += "0";
formattedTime += "$seconds";
return formattedTime;
}
}
class TimerPage扩展StatefulWidget{
TimerPage({Key}):超级(Key:Key);
TimerPageState createState()=>新的TimerPageState();
}
类TimerPageState扩展了状态{
最后项目=[
列表砖(
前导:图标(图标、照相/摄像机),
标题:文本(“摄影机”),
onTap:(){},
),
列表砖(
前导:图标(图标。照片库),
标题:文本(“选择”),
onTap:(){},
),
列表砖(
前导:图标(Icons.delete),
标题:文本(“删除”),
onTap:(){},
),
分隔符(),
如果(真)
列表砖(
标题:文本(“取消”),
onTap:(){},
),
];
_展示页(){
showModalBottomSheet(
上下文:上下文,
生成器:(BuildContext\ux){
返回容器(
孩子:包裹(
儿童:项目,
),
);
},
是的,
);
}
整数计数=20*60;
@凌驾
void initState(){
super.initState();
定时器。周期(持续时间(秒:1),(定时器){
--_计数;
如果(_计数<0)
timer.cancel();
其他的
setState((){});
});
}
@凌驾
小部件构建(构建上下文){
返回列(子项:[
划船(
儿童:[
图标按钮(
图标:图标(
图标。暂停\圆圈\轮廓,
颜色:颜色,蓝色,
),
按下按钮:_展示页),
Text('${u formatSeconds(_count)}'),//这是添加动态文本计时器的地方
垫片(),
扁平按钮(
已按下:_展示页,
子:文本(
“提交”,
样式:TextStyle(
尺码:18,
fontFamily:“lato”,
颜色:颜色,蓝色,
),
),
),
],
),
]);
}
字符串格式秒(整数计数){
整小时=计数~/3600;
int secondsLeft=计数-小时*3600;
int分钟=secondsLeft~/60;
整数秒=秒英尺-分钟*60;
字符串formattedTime=“”;
如果(分钟数<10)formattedTime+=“0”;
formattedTime=“$minutes:”;
如果(秒<10)formattedTime+=“0”;
formattedTime+=“$seconds”;
返回格式化时间;
}
}
导入“包装:颤振/材料.飞镖”;
导入“dart:async”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
背景颜色:颜色。灰色,
正文:TimerPage(),
),
);
}
}
类TimerPage扩展StatefulWidget{
TimerPage({Key}):超级(Key:Key);
TimerPageState createState()=>新的TimerPageState();
}
类TimerPageState扩展了状态{
int _start=20;
定时器(u定时器),;
void startTimer(){
const oneSec=const持续时间(秒:1);
_定时器=新定时器。周期性(
一秒,
(计时器)=>设置状态(
() {
如果(_start<1){
showDialog(上下文:上下文,生成器:(上下文){
返回警报对话框(
标题:文本(“您的对话框…”),
);
});
timer.cancel();
}否则{
_开始=_开始-1;
}
},
),
);
}
@凌驾
void initState(){
startTimer();
//TODO:实现initState
super.initState();
}
最后项目=[
列表砖(
前导:图标(图标、照相/摄像机),
标题:文本(“摄影机”),
onTap:(){},
),
列表砖(
前导:图标(图标。照片库),
标题:文本(“选择”),
onTap:(){},
),
列表砖(
前导:图标(Icons.delete),
标题:文本(“删除”),
onTap:(){},
),
分隔符(),
如果(真)
列表砖(
标题:文本(“取消”),
onTap:(){},
),
import 'package:flutter/material.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey,
body: TimerPage(),
),
);
}
}
class TimerPage extends StatefulWidget {
TimerPage({Key key}) : super(key: key);
TimerPageState createState() => new TimerPageState();
}
class TimerPageState extends State<TimerPage> {
int _start = 20;
Timer _timer ;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(
() {
if (_start < 1) {
showDialog(context: context,builder: (context) {
return AlertDialog(
title: Text(" your Dialog .... "),
);
});
timer.cancel();
} else {
_start = _start - 1;
}
},
),
);
}
@override
void initState() {
startTimer();
// TODO: implement initState
super.initState();
}
final items = <Widget>[
ListTile(
leading: Icon(Icons.photo_camera),
title: Text('Camera'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.photo_library),
title: Text('Select'),
onTap: () {},
),
ListTile(
leading: Icon(Icons.delete),
title: Text('Delete'),
onTap: () {},
),
Divider(),
if (true)
ListTile(
title: Text('Cancel'),
onTap: () {},
),
];
_showSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext _) {
return Container(
child: Wrap(
children: items,
),
);
},
isScrollControlled: true,
);
}
@override
void dispose() {
_timer.cancel();
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(children: [
Row(
children: <Widget>[
IconButton(
icon: Icon(
Icons.pause_circle_outline,
color: Colors.blue,
),
onPressed: _showSheet),
Text('Time $_start'), // ,this is where dynamic text timer added
Spacer(),
FlatButton(
onPressed: _showSheet,
child: Text(
"Submit",
style: TextStyle(
fontSize: 18,
fontFamily: 'lato',
color: Colors.blue,
),
)),
],
),
]);
}
}