Navigating to screen on button click but it is navigating without the button press

Issue

import 'package:flutter/material.dart';
import './home.dart';
class Question extends StatelessWidget {
Question({super.key});
TextEditingController durationController = 
TextEditingController();
TextEditingController lengthController = TextEditingController();
@override
Widget build(BuildContext context) {
return Container(
  margin: const EdgeInsets.only(top: 80),
  child: Column(
    children: <Widget>[
      Container(
        margin: const EdgeInsets.only(top: 30, right: 180),
        child: const Text("enter the period duration",
            style: TextStyle(fontSize: 18)),
      ),
      Card(
          child: TextField(
        decoration: const InputDecoration(
          labelText: "period duration",
        ),
        controller: durationController,
        keyboardType: TextInputType.number,
      )),
      Container(
        margin: const EdgeInsets.only(top: 30, right: 180),
        child: const Text("enter the period length",
            style: TextStyle(fontSize: 18)),
      ),
      Card(
          child: TextField(
        decoration: const InputDecoration(
          labelText: "period length",
        ),
        controller: lengthController,
        keyboardType: TextInputType.number,
      )),
      OutlinedButton(
          onPressed: () {
            //ClearSession();
            Navigator.pop(context, true);
            Navigator.pushReplacement(context,
                MaterialPageRoute(builder: (context) => const Home()));
          },

          child: const Text("Submit")),
    ],
  ),
);

}
}
//my aim is on pressing the button , next screen have to appear and in next screen there should be 3 icon but instead of appearing new screen when hitting button , it is showing the icon page in Question widget

// next screen widget class is

import 'package:flutter/material.dart';
class Home extends StatefulWidget {
 const Home({super.key});
@override
State<Home> createState() => _HomeState();
 }

  class _HomeState extends State<Home> {
  @override
 Widget build(BuildContext context) {
return Container(
  child: Column(
    children: <Widget>[
      Row(
        children: <Widget>[
          Container(child: Icon(Icons.home)),
          Container(child: Icon(Icons.calendar_month)),
          Container(
            child: Text(" home button"),
          )
        ],
      )
    ],
  ),
);

}
}

//my aim is on pressing the button , next screen have to appear and in next screen there should be 3 icon but instead of appearing new screen when hitting button , it is showing the icon page in Question widget

Solution

There can potential problem if this is a first screen and you use the pop method then you will get an error because you have nothing to pop.

Try to check the first possibility of navigation poping like in the example below

OutlinedButton(
    onPressed: () {
        //ClearSession();
        if (Navigator.canPop(context)) {
            Navigator.pop(context, true);
         }
         Navigator.pushReplacement(context, 
             MaterialPageRoute(builder: (context) => const Home()));
     },
     child: const Text("Submit"),
 ),

Answered By – powerman23rus

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *