颤振不导航到下一页上下文错误?

时间:2019-11-21 11:06:42

标签: flutter routing navigator

这是我正在执行的代码,在点击时我需要转到另一页,但出现错误请求的导航器操作,且上下文不包含导航器。 < / p>

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]),
                    ],
                  )
                ),
              ),
            ),

这是我要浏览的课程,

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并且卡住了

3 个答案:

答案 0 :(得分:1)

您可以使用official doc中显示的方法,

  1. 您必须在main.dart中定义路线
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. 然后,您可以导航到“第一个”屏幕,以此类推ononTap或onPressed事件,
Navigator.pushNamed(context, '/second');

注意:整个应用中应该只有一个MaterialApp。 有关更多信息,请访问official Page

答案 1 :(得分:0)

您的所有类都应该是MaterialApp小部件的后代,为此,请用MaterialApp小部件包装放在runApp方法上的类,然后从您的所有其他MaterialApp小部件中删除小部件树。

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

答案 2 :(得分:-1)

只需尝试一下即可工作!

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"),
      ),
    );
  }
}