Flutter 没有导航到下一页上下文错误?

Flutter not navigating to next page context error?

这是我正在执行的代码,点击我需要转到另一个页面,但我收到错误消息导航器操作请求的上下文不包含导航器。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = 'SHA-WAY';
    var FirstPage = ['SURGERIES','OPERTAION THEATER TECHNICIAN','DRESSINGS OR BANDAGES','PHYSIOTHERAPY','NURSE MALE|FEMALE',
      'HOSPITAL LINEN OR STAFF UNIFORM','MEDICENS(ONLY RARE INJECTIONS)','OPERATION THEATRE INSTRUMENTS|EQUIPEMENTS'];

    return MaterialApp(
      title: title,
      home: Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Container(
        child: GridView.count(
          // Create a grid with 2 columns. If you change the scrollDirection to
          // horizontal, this produces 2 rows.
          crossAxisCount: 2,
          children:  [
            Card(
              child: InkWell(
                onTap: (){
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => surgeries()),
                  );
                },
                splashColor: Colors.blue,
                child: Center(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(Icons.content_cut,size: 60.0,),
                      Text(FirstPage[0]),
                    ],
                  )
                ),
              ),
            ),

这是我要导航的 class,

import 'package:flutter/material.dart';

class surgeries extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SURGERIES"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

不知道我做错了什么刚开始 FLUTTER,我卡住了

刚试过这个,成功了!

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'example',
      debugShowCheckedModeBanner: false,

      theme: ThemeData(

        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.

        primarySwatch: Colors.blue,
        buttonTheme: ButtonThemeData(
          buttonColor: Colors.blue,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
          textTheme: ButtonTextTheme.primary
        )
      ),
      home: Esempio1()

    );
    }
}




  class Esempio1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("WAKAWAKA"),
      ),
          body: Container(
        child: Column(
          children: <Widget>[
            RaisedButton(
              child: Text("aasdasd"),
              onPressed: (){
                Navigator.push(context, MaterialPageRoute(builder: (_)=> Esempio2()));
              },
            )
          ],
        ),
      ),
    );
  }
}

class Esempio2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("eheh"),
      ),
          body: Container(
        child: Text("AAAAA"),
      ),
    );
  }
}

你所有的 classes 应该是 MaterialApp 小部件的后代,为了实现这一点,用 MaterialApp 小部件包装你的 class 放在 runApp 方法上,然后删除您的小部件树中的所有其他 MaterialApp 个小部件。

void main() {
  runApp(MaterialApp(home:MyApp));
}

您可以使用 official doc

中显示的方法
  1. 您必须在 main.dart
  2. 中定义路由
MaterialApp(
  // Start the app with the "/" named route. In this case, the app starts
  // on the FirstScreen widget.
  initialRoute: '/',
  routes: {
    // When navigating to the "/" route, build the FirstScreen widget.
    '/': (context) => FirstScreen(),
    // When navigating to the "/second" route, build the SecondScreen widget.
    '/second': (context) => SecondScreen(),
  },
);
  1. 然后你可以像这样导航到第一个屏幕到另一个 onTap 或 onPressed 事件,
Navigator.pushNamed(context, '/second');

注意:您的整个应用中应该只有一个 MaterialApp。 如需更多信息,请前往 official Page