gpt4 book ai didi

android - flutter :导航问题

转载 作者:IT王子 更新时间:2023-10-29 07:23:04 24 4
gpt4 key购买 nike

我无法导航显示错误的屏幕“使用不包含导航器的上下文请求的导航器操作,我尝试了很多解决方案,其中在 Builder 中使用了导航器”与 stateless小部件,但此处导航会在几秒钟后自动完成 override intiSate 中的方法.我的目标是在几秒钟后浏览屏幕。

class Splash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return testWidget;
}
}

Widget testWidget = new MediaQuery(
data: new MediaQueryData(),
child: new MaterialApp( title: 'xxxxxxxxxxxxx',
home: SplashScreen(),
debugShowCheckedModeBanner: false,
routes: <String, WidgetBuilder>{
'/login': (BuildContext context) => new Login(),
},
)

);

class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
@override
Future initState () {
super.initState();
new Future.delayed(
const Duration(seconds: 2), () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),
));
}
@override
Widget build(BuildContext context) {

return(Scaffold(
body: Container(
height: double.infinity,
width: double.infinity,
child: Image.asset('assets/images/crop.jpg',fit:BoxFit.fill),
),
));
//build
}
}

显示错误

Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

最佳答案

代码更正:

MaterialApp Should always be the Root Widget of all Widgets. That Way Navigator is always Available.

void main() => runApp(Splash());

class Splash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: testWidget,
debugShowCheckedModeBanner: false,
);
}
}

Widget testWidget =
new MediaQuery(data: new MediaQueryData(), child: new SplashScreen());

class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
@override
void initState() {
super.initState();
Future.delayed(
const Duration(seconds: 2),
() => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Login()),
));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Spalsh'),),
body: Container(
height: double.infinity,
width: double.infinity,
// child: Image.asset('assets/images/crop.jpg', fit: BoxFit.fill),
),
);
//build
}
}

class Login extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Container(),
);
}
}

关于android - flutter :导航问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54568459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com