'Navigator.push it doesn't work how can i fix it?

I created 3 files, in the second file I put it inside the Elevatedbutton, but when I press the button I don't go to the other pagethe first it has this code :

import 'package:flutter/material.dart';
import 'Firstpage.dart';
 void main (){
   runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirstPage(),
    );
  }
}

in the second file i have :

import 'package:flutter/material.dart';

import 'planetspage.dart';

class FirstPage extends StatefulWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  var lang = "en";


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Stack(
          children: [
            Image.asset(
              'images/firstwal.jpg',
              width: double.infinity,
              fit: BoxFit.cover,
            ),
            Container(
              padding: const EdgeInsets.all(15),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  const Text(
                    'Explore space',
                    style: TextStyle(
                        fontSize: 35,
                        color: Colors.white,
                        fontWeight: FontWeight.w500),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  const Text(
                    'a system of millions or billions of stars, together with gas and dust, held together by gravitational attraction.',
                    style: TextStyle(
                        fontWeight: FontWeight.w600,
                        fontSize: 20,
                        color: Colors.white),
                  ),
                  const SizedBox(
                    height: 30,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      ClipRRect(
                        borderRadius: const BorderRadius.all(Radius.circular(30)),
                        child: Container(
                            padding: const EdgeInsets.symmetric(horizontal: 40),
                            color: Colors.white,
                            child: DropdownButton(
                              items: [
                                "en",
                                "ar",
                              ]
                                  .map((e) => DropdownMenuItem(
                                        child: Text(e),
                                        value: e,
                                      ))
                                  .toList(),
                              onChanged: (val) {
                                setState(() {
                                  lang = val.toString();
                                });
                              },
                              value: lang,
                              style:
                                  const TextStyle(color: Colors.black87, fontSize: 20),
                              iconEnabledColor: Colors.black87,

                            )),
                      ),
                      ClipRRect(
                        borderRadius: const BorderRadius.all(Radius.circular(30)),
                        child: ElevatedButton(
                            onPressed: () {

                                Navigator.push(context, MaterialPageRoute(builder: (context)=> const enPlanetspage()));
                            },
                            style: ElevatedButton.styleFrom(
                                primary: Colors.deepPurple),
                            child: Container(
                              padding: const EdgeInsets.symmetric(
                                  vertical: 10, horizontal: 20),
                              child: Row(
                                children: const [
                                  Text(
                                    'Next',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 20,
                                    ),
                                  ),
                                  Icon(Icons.navigate_next_outlined),
                                ],
                              ),
                            )),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

when i press the button Navigator.push doesn't work how can i fix it? I created 3 files, in the second file I put it inside the Elevatedbutton, but when I press the button I don't go to the other pagethe first it has this code :



Solution 1:[1]

Try using:

onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context)=> const enPlanetspage()));

Check if your image files are mentioned in the pubsec.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valentin Vignal