问题描述
//main.dart file
import 'package:Flutter/material.dart';
import 'package:messenger/widgets/splash_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',debugShowCheckedModeBanner: false,theme: ThemeData(
scaffoldBackgroundColor: Colors.white,appBarTheme: AppBarTheme(
brightness: Brightness.light,color: Colors.white,elevation: 0.0,iconTheme: IconThemeData(color: Colors.black),actionsIconTheme: IconThemeData(color: Colors.black)),fontFamily: "Ubuntu",primarySwatch: Colors.blue,primaryColor: Color(0xff0084FF),),home: Column(
children: <Widget>[
SizedBox(height: 30.0,ElevatedButton(
onpressed: () => navigation(context),child: Text("to splash screen"))
],);
}
navigation(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(builder: (context)=>SplashScreen()));
}
}
//SplashScreen.dart file
import 'package:Flutter/material.dart';
import 'package:Flutter/widgets.dart';
import 'package:messenger/widgets/login_button.dart';
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
Widget build(BuildContext context) {
double imageSize = MediaQuery.of(context).size.width / 3;
return Scaffold(
body: Center(
child: Image.asset(
"images/logo.png",width: imageSize,height: imageSize,fit: BoxFit.cover,bottomNavigationBar: Padding(
padding: EdgeInsets.symmetric(
horizontal: 16.0,vertical: 16.0,child: Column(
mainAxisSize: MainAxisSize.min,children: <Widget>[
LoginButton(text: "Continue with Facebook",onpressed: (){
},colour: Color(0xff3b5998),leading: Image.asset("images/facebook-logo.png",],);
}
}
使用不包含导航器的上下文请求导航器操作。
也许我没有很好地理解上下文的概念,这就是为什么我得到
错误。为什么会发生此错误? SplashScreen 的第二个小部件具有
语境。但我仍然收到错误。我的代码有什么问题吗
错过了吗?
解决方法
兄弟,这不是在导航器中调用 Navigator.push
的好方法,请尝试在特定函数中调用,或者您可以在 ink well
中调用它,因此请删除
**Navigator(BuildContext context)** 之后它就可以为你工作了。
**这里是可以解决你导航问题的代码**
enter code here
import 'package:flutter/material.dart';
import 'screen/splashScreen.dart';
class DemoNavigation extends StatefulWidget {
const DemoNavigation({Key? key}) : super(key: key);
@override
_DemoNavigationState createState() => _DemoNavigationState();
}
class _DemoNavigationState extends State<DemoNavigation> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),body: Container(
child: Column(
children: [
Text("Navigator Demo"),ElevatedButton(
onPressed: () {
// here is the function that your are using
navigation(context);
},child: Text("TO Next Screen"),),],);
}
/// here you need to change the context that your are passing to build
/// copy this
void navigation(BuildContext context) {
Navigator.push(context,MaterialPageRoute(builder: (_) =>
SplashScreen()));
}
}