Navigator operation requested with a context that does not include a Navigator

The compiler gives this error whenever i click the button

I have made two different files in a project , in my main.dart have ,


    import "package:flutter/material.dart";
    import 'package:try_new/new.dart';


     void main() {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const MyApp2());
    }

    class MyApp2 extends StatefulWidget {
      const MyApp2({super.key});
      @OverRide
        
  State createState() => _MyApp2State();
}

class _MyApp2State extends State {
  @OverRide
  Widget build(BuildContext context1) {
    return MaterialApp(
      title: "App",
      home: Scaffold(
          backgroundColor: const Color.fromARGB(255, 6, 2, 11),
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                Padding(
                    padding: const EdgeInsets.only(top: 140.0),
                    child: Container(
                        alignment: Alignment.topCenter,
                        child: Image.asset(
                          "assets/images/two.png",
                          height: 120,
                          width: 100,
                          fit: BoxFit.cover,
                        ))),
                const SizedBox(height: 10),
                const Text(
                  "Github",
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 48),
                ),
                const SizedBox(height: 190),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SizedBox(
                      child: Material(
                    color: const Color.fromARGB(255, 16, 16, 16),
                    borderRadius: BorderRadius.circular(6),
                    child: TextFormField(
                      style: const TextStyle(color: Colors.white),
                      decoration: const InputDecoration(
                          border: OutlineInputBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(6))),
                          hintText: "Github username",
                          hintStyle: TextStyle(
                              color: Color.fromARGB(255, 115, 115, 113),
                              fontWeight: FontWeight.w500,
                              fontSize: 22),
                          contentPadding:
                              EdgeInsets.only(top: 15, left: 20, bottom: 15)),
                    ),
                  )),
                ),
                const SizedBox(
                  height: 2,
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Material(
                    elevation: 1.0,
                    borderRadius: const BorderRadius.all(Radius.circular(8)),
                    color: const Color.fromARGB(255, 43, 136, 212),
                    child: SizedBox(
                      width: 400,
                      child: Ink(
                        decoration: const BoxDecoration(
                            color: Color.fromARGB(255, 43, 136, 212),
                            borderRadius: BorderRadius.all(Radius.circular(8))),
                        child: TextButton(
                            onPressed: () {
                              Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (BuildContext context) =>
                                          const MyApp()));
                            },
                            child: const Text(
                              "Get Your Following Now",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 14,
                                fontWeight: FontWeight.w500,
                              ),
                            )),
                      ),
                    ),
                  ),
                )
              ],
            ),
          )),
    );
  }
}

and i have the other file named as new.dart , which contains the code


import "package:flutter/material.dart";

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @OverRide
  State createState() {
    return _MyAppState2();
  }
}

class _MyAppState2 extends State {
  @OverRide
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: const Color.fromARGB(255, 250, 250, 245),
          elevation: 0.0,
          leading: const Icon(
            Icons.arrow_back_ios,
            color: Color.fromARGB(255, 160, 160, 160),
            size: 25,
          ),
        ),
        backgroundColor: const Color.fromARGB(255, 250, 250, 245),
        body: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              const SizedBox(
                height: 15,
              ),
              Container(
                alignment: Alignment.topCenter,
                child: const CircleAvatar(
                  backgroundImage: AssetImage("assets/images/uncle.png"),
                  radius: 55,
                ),
              ),
              const SizedBox(
                height: 15,
              ),
              const Text(
                "afgprogrammer",
                style: TextStyle(fontSize: 20),
              ),
              const SizedBox(
                height: 10,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundImage: AssetImage("assets/images/1.png"),
                      radius: 38,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.0, left: 10),
                      child: Text(
                        "sintaxi",
                        style: TextStyle(
                            fontSize: 24,
                            color: Color.fromARGB(255, 141, 139, 139)),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              const Divider(
                color: Colors.grey,
              ),
              const SizedBox(
                height: 10,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundImage: AssetImage("assets/images/2.png"),
                      radius: 38,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.0, left: 10),
                      child: Text(
                        "mikeric",
                        style: TextStyle(
                            fontSize: 24,
                            color: Color.fromARGB(255, 155, 153, 153)),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              const Divider(
                color: Colors.grey,
              ),
              const SizedBox(
                height: 10,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundImage: AssetImage("assets/images/3.png"),
                      radius: 38,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.0, left: 10),
                      child: Text(
                        "jzaefferer",
                        style: TextStyle(
                            fontSize: 24,
                            color: Color.fromARGB(255, 161, 159, 159)),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              const Divider(
                color: Colors.grey,
              ),
              const SizedBox(
                height: 10,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundImage: AssetImage("assets/images/4.png"),
                      radius: 38,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.0, left: 10),
                      child: Text(
                        "cowboy",
                        style: TextStyle(
                            fontSize: 24,
                            color: Color.fromARGB(255, 141, 140, 140)),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              const Divider(
                color: Colors.grey,
              ),
              const SizedBox(
                height: 10,
              ),
              const Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    CircleAvatar(
                      backgroundImage: AssetImage("assets/images/5.png"),
                      radius: 38,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 9.0, left: 10),
                      child: Text(
                        "nat",
                        style: TextStyle(
                            fontSize: 24,
                            color: Color.fromARGB(255, 157, 156, 156)),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              const Divider(
                color: Colors.grey,
              ),
            ],
          ),
        ));
  }
}

When the button is clicked it should show the class present in new.dart

1

There are 1 answers

0
Md. Yeasin Sheikh On BEST ANSWER

You need to separate context from Material to push on new route. You can wrap with Builder widget.

return MaterialApp(
  title: "App",
  home: Builder(builder: (context) {
    return Scaffold(

it would be better and you can

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    MaterialApp(
      title: "App",
      home: MyApp2(),
    ),
  );
}