Home > Software design >  Flutter Navigator - push to Another page
Flutter Navigator - push to Another page

Time:04-03

i have a problem when i use Navigator.of(context).push, at ElevatedButton it doesn't work , i have two pages Home_Screen And The another one is Body from another file , can you help me ?

` body: Container(

     padding: EdgeInsets.symmetric(vertical: 10,horizontal: 5),
        child: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        child: Column(  
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
              Text('PROMOTION',style: TextStyle(color:Colors.orange, fontSize: 20 ),),
              FloatingActionButton(
                onPressed: (){
                  Navigator.of(context).push( MaterialPageRoute(builder: (context) => Body(), 
                ),);
                },
                child: Text('Starter',style: TextStyle(color: Colors.grey),),),

`

CodePudding user response:

Your navigator code is wrong. You have to pass context before MateriaPageRoute too. Your Navigator must be like above :

 Navigator.push(
   context,
   MaterialPageRoute(builder: (context) => Body()));

CodePudding user response:

Change your following code:

Navigator.of(context).push( MaterialPageRoute(builder: (context) => Body()),);

to

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

You need to pass a context and route to push method.

For more information about navigator, please go through the following link: https://docs.flutter.dev/cookbook/navigation/navigation-basics

CodePudding user response:

@vaidarbhi: the form is correct in both cases...

@yassir outisket: Body should be a PAGE with scaffold or wont work.

Try next code for make a test with your function: Navigator.of(context).push( MaterialPageRoute(builder: (context) => TestPage(), ),);

import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {

  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Test'),
        ),
        body: Text("This is a test")
    );
  }
}
  • Related