我尝试在启动屏幕之后导航到新屏幕,当我尝试在init方法中导航到新屏幕时,显示错误,
E / flutter(5636):[错误:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:请求的导航器操作的上下文不包含导航器。
E / flutter(5636):用于从导航器推入或弹出路线的上下文必须是作为导航器小部件的后代的小部件的上下文。
我的代码
import 'package:flutter/material.dart';
import 'package:igloled_app/home.dart';
import 'dart:async';
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
Future.delayed(Duration(seconds: 3),(){
print('3 sec done');
Navigator.push(context, MaterialPageRoute(builder: (context){
return AppHeader();
}));
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white70,
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(flex: 2,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20.0),
child: Container(
margin: EdgeInsets.all(10.0),
child: Image.asset('images/splash_logo.png')
),
)
],
),
),
),
Expanded(flex: 1,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.green),),
Padding(padding: EdgeInsets.only(top: 20.0),
),
Text('Make Your Life Brighter',
style: TextStyle(
color: Colors.lightGreen,
fontStyle: FontStyle.italic,
fontSize: 20.0,
),
),
],
),
)
],
),
],
),
),
);
}
}
}
有人可以帮我吗? 预先谢谢你。
答案 0 :(得分:1)
这是正确执行操作的示例代码。
void main() => runApp(MaterialApp(home: FirstScreen()));
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
void initState() {
super.initState();
Future.delayed(Duration(seconds: 3), () {
print('3 sec done');
Navigator.push(context, MaterialPageRoute(builder: (context) {
return SecondScreen();
}));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title: Text("First screen")));
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title: Text("Second screen")));
}
}
答案 1 :(得分:1)
您的MaterialApp
不在您的上下文中!
将MaterialApp
移至runApp
方法的轻微修复。
但是请注意,但是所有不会使用setState
到另一个类或runApp
方法的地下窗口小部件都具有代码的最佳功能。