Flutter 颤振-如何开始列表中的下一个倒计时
我有一张倒计时表,上面有秒数 当列表上的第一个倒计时结束时,我想开始下一个倒计时,以此类推 我怎样才能做到这一点 谢谢大家! 我的代码到现在为止Flutter 颤振-如何开始列表中的下一个倒计时,flutter,dart,Flutter,Dart,我有一张倒计时表,上面有秒数 当列表上的第一个倒计时结束时,我想开始下一个倒计时,以此类推 我怎样才能做到这一点 谢谢大家! 我的代码到现在为止 导入'dart:async'; 进口“包装:颤振/cupertino.dart”; 进口“包装:颤振/材料.省道”; 导入“package:timer_count_down/timer_controller.dart”; void main()=>runApp(MyApp()); 类MyApp扩展了无状态小部件{ @凌驾 小部件构建(构建上下文){ 返
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:timer_count_down/timer_controller.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
themeMode:themeMode.dark,
首页:Ex(),
);
}
}
类Ex扩展StatefulWidget{
@凌驾
State createState()=>ExpandableListView();
}
类ExpandableListView扩展状态{
最终倒计时控制器=倒计时控制器();
列表=[3,4,5,6];
定时器(u定时器),;
int计数器=3;
无效开始时间(秒){
如果(_timer!=null){
_timer.cancel();
}
_计时器=计时器。周期性(持续时间(小时:0,分钟:0,秒:秒),(计时器){
设置状态(){
如果(计数器>0){
计数器--;
}否则{
_timer.cancel();
}
});
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
主体:容器(
身高:500,
宽度:200,
子:列(
儿童:[
容器(
身高:200,
宽度:200,
子项:ListView.builder(
itemCount:list.length,
itemBuilder:(上下文,索引){
返回文本(列表[index].toString());
},
),
),
升起的按钮(
已按下:(){
设置状态(){
_startTimer(列表[0]);
});
},
),
],
),
),
);
}
}
我添加了布尔值列表,用于控制倒计时小部件是否暂停。下面是对代码的调整
为ListView构建项的主要小部件是
Widget buildItem(int index) {
return !isPauseList[index]
? Countdown(
seconds: list[index],
build: (BuildContext context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
controller: controller,
onFinished: () {
if (index < isPauseList.length) {
setState(() {
isPauseList[index + 1] = false;
});
}
},
)
: Text(list[index].toString());
Widget构建项(int索引){
return!isPauseList[索引]
?倒计时(
秒:列表[索引],
build:(BuildContext上下文,双时间)=>Text(time.toString()),
间隔:持续时间(毫秒:100),
控制器:控制器,
onFinished:(){
if(索引
}
下面是您的全部代码
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:timer_count_down/timer_controller.dart';
import 'package:timer_count_down/timer_count_down.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.dark,
home: Ex(),
);
}
}
class Ex extends StatefulWidget {
@override
State<StatefulWidget> createState() => ExpandableListView();
}
class ExpandableListView extends State<Ex> {
final CountdownController controller = CountdownController();
List<int> list = [3, 4, 5, 6];
List<bool> isPauseList = [true, true, true, true];
Timer _timer;
int counter = 3;
void _startTimer() {
setState(() {
this.isPauseList[0] = false;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
height: 500,
width: 200,
child: Column(
children: [
Container(
height: 200,
width: 200,
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
// return Text(list[index].toString());
return buildItem(index);
},
),
),
RaisedButton(
onPressed: () {
setState(() {
_startTimer();
});
},
),
],
),
),
);
}
Widget buildItem(int index) {
return !isPauseList[index]
? Countdown(
seconds: list[index],
build: (BuildContext context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
controller: controller,
onFinished: () {
if (index < isPauseList.length) {
setState(() {
isPauseList[index + 1] = false;
});
}
},
)
: Text(list[index].toString());
}
}
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:timer_count_down/timer_controller.dart”;
导入“package:timer_count_down/timer_count_down.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
themeMode:themeMode.dark,
首页:Ex(),
);
}
}
类Ex扩展StatefulWidget{
@凌驾
State createState()=>ExpandableListView();
}
类ExpandableListView扩展状态{
最终倒计时控制器=倒计时控制器();
列表=[3,4,5,6];
列表isPauseList=[true,true,true,true];
定时器(u定时器),;
int计数器=3;
void _startTimer(){
设置状态(){
this.isPauseList[0]=false;
});
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
主体:容器(
身高:500,
宽度:200,
子:列(
儿童:[
容器(
身高:200,
宽度:200,
子项:ListView.builder(
itemCount:list.length,
itemBuilder:(上下文,索引){
//返回文本(列表[index].toString());
返回项目(索引);
},
),
),
升起的按钮(
已按下:(){
设置状态(){
_startTimer();
});
},
),
],
),
),
);
}
小部件构建项(int索引){
return!isPauseList[索引]
?倒计时(
秒:列表[索引],
build:(BuildContext上下文,双时间)=>Text(time.toString()),
间隔:持续时间(毫秒:100),
控制器:控制器,
onFinished:(){
if(索引
我添加了布尔值列表,用于控制倒计时小部件是否暂停。下面是对代码的调整
为ListView构建项的主要小部件是
Widget buildItem(int index) {
return !isPauseList[index]
? Countdown(
seconds: list[index],
build: (BuildContext context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
controller: controller,
onFinished: () {
if (index < isPauseList.length) {
setState(() {
isPauseList[index + 1] = false;
});
}
},
)
: Text(list[index].toString());
Widget构建项(int索引){
return!isPauseList[索引]
?倒计时(
秒:列表[索引],
build:(BuildContext上下文,双时间)=>Text(time.toString()),
间隔:持续时间(毫秒:100),
控制器:控制器,
onFinished:(){
if(索引
}
下面是您的全部代码
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:timer_count_down/timer_controller.dart';
import 'package:timer_count_down/timer_count_down.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
themeMode: ThemeMode.dark,
home: Ex(),
);
}
}
class Ex extends StatefulWidget {
@override
State<StatefulWidget> createState() => ExpandableListView();
}
class ExpandableListView extends State<Ex> {
final CountdownController controller = CountdownController();
List<int> list = [3, 4, 5, 6];
List<bool> isPauseList = [true, true, true, true];
Timer _timer;
int counter = 3;
void _startTimer() {
setState(() {
this.isPauseList[0] = false;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: Container(
height: 500,
width: 200,
child: Column(
children: [
Container(
height: 200,
width: 200,
child: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
// return Text(list[index].toString());
return buildItem(index);
},
),
),
RaisedButton(
onPressed: () {
setState(() {
_startTimer();
});
},
),
],
),
),
);
}
Widget buildItem(int index) {
return !isPauseList[index]
? Countdown(
seconds: list[index],
build: (BuildContext context, double time) => Text(time.toString()),
interval: Duration(milliseconds: 100),
controller: controller,
onFinished: () {
if (index < isPauseList.length) {
setState(() {
isPauseList[index + 1] = false;
});
}
},
)
: Text(list[index].toString());
}
}
导入'dart:async';
进口“包装:颤振/cupertino.dart”;
进口“包装:颤振/材料.省道”;
导入“package:timer_count_down/timer_controller.dart”;
导入“package:timer_count_down/timer_count_down.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
他们