如何在条件为真后弹出页面?
how to pop page after condition is true in flutter?
我正在尝试构建一个计时器应用程序,它会在时间变为空后弹出屏幕
示例代码如下
Padding(
padding: const EdgeInsets.all(8.0),
child: CountdownTimer(
endTime: endTime,
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
Navigator.of(context).pop();
} else {
return Text(
'${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
}
}),
错误如下
E/flutter (21479): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed
assertion: line 4334 pos 12: '<optimized out>': is not true.
E/flutter (21479): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter (21479): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (21479): #2 NavigatorState._pushEntry (package:flutter/src/widgets/navigator.dart:4334:12)
E/flutter (21479): #3 NavigatorState.push (package:flutter/src/widgets/navigator.dart:4270:5)
E/flutter (21479): #4 showGeneralDialog (package:flutter/src/widgets/routes.dart:1835:66)
E/flutter (21479): #5 Alert.show (package:rflutter_alert/src/alert.dart:57:18)
您可以复制粘贴 运行 下面的完整代码
您可以使用 addPostFrameCallback
和 return Container()
代码片段
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pop();
});
return Container();
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("test"),
),
body: Center(
child: RaisedButton(
child: Text('To CountDown page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CountDownPage()),
);
},
),
));
}
}
class CountDownPage extends StatefulWidget {
CountDownPage({Key key}) : super(key: key);
@override
_CountDownPageState createState() => _CountDownPageState();
}
class _CountDownPageState extends State<CountDownPage> {
int endTime = DateTime.now().millisecondsSinceEpoch + 1000 * 3;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("count down"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: CountdownTimer(
endTime: endTime,
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pop();
});
return Container();
} else {
return Text(
'${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
}
})),
],
),
),
);
}
}
在你的情况下,时间可能不会为空,因为它是倒计时它将得到 00:00:00:00 所以将你的条件更改为 time.toString()=='00:00:00:00'
我正在尝试构建一个计时器应用程序,它会在时间变为空后弹出屏幕 示例代码如下
Padding(
padding: const EdgeInsets.all(8.0),
child: CountdownTimer(
endTime: endTime,
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
Navigator.of(context).pop();
} else {
return Text(
'${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
}
}),
错误如下
E/flutter (21479): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed
assertion: line 4334 pos 12: '<optimized out>': is not true.
E/flutter (21479): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter (21479): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter (21479): #2 NavigatorState._pushEntry (package:flutter/src/widgets/navigator.dart:4334:12)
E/flutter (21479): #3 NavigatorState.push (package:flutter/src/widgets/navigator.dart:4270:5)
E/flutter (21479): #4 showGeneralDialog (package:flutter/src/widgets/routes.dart:1835:66)
E/flutter (21479): #5 Alert.show (package:rflutter_alert/src/alert.dart:57:18)
您可以复制粘贴 运行 下面的完整代码
您可以使用 addPostFrameCallback
和 return Container()
代码片段
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pop();
});
return Container();
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("test"),
),
body: Center(
child: RaisedButton(
child: Text('To CountDown page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CountDownPage()),
);
},
),
));
}
}
class CountDownPage extends StatefulWidget {
CountDownPage({Key key}) : super(key: key);
@override
_CountDownPageState createState() => _CountDownPageState();
}
class _CountDownPageState extends State<CountDownPage> {
int endTime = DateTime.now().millisecondsSinceEpoch + 1000 * 3;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("count down"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: CountdownTimer(
endTime: endTime,
widgetBuilder: (_, CurrentRemainingTime time) {
if (time == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pop();
});
return Container();
} else {
return Text(
'${(time.hours == null) ? "00" : time.hours}:${(time.min == null) ? "00" : time.min}:${(time.sec == null) ? "00" : time.sec}');
}
})),
],
),
),
);
}
}
在你的情况下,时间可能不会为空,因为它是倒计时它将得到 00:00:00:00 所以将你的条件更改为 time.toString()=='00:00:00:00'