Mandel
Mandel

Reputation: 223

Default back button not showing after pushing new screen

I had pushed a new Screen from my main screen and it had navigated smoothly and also showed the back button however now that I run the button, the default button is not showing in my code.

The only change that I made was that I changed the route in my main file from taskScreen() to task(). But this was essential as i wanted to make some more routes from my second screen and so it had to return a Material App. How can i get the default back button back?

My code:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: DefaultTabController(length: 2,child: MyHomePage(title: '')),
      routes: <String, WidgetBuilder>{
       "/TaskScreen": (BuildContext context) => new task(), //CHANGED HERE 
      },
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

 @override
  Widget build(BuildContext context){

   return Scaffold(

     appBar: AppBar(
    ..
     ),
     body: ...
 }
}

My second screen

class task extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        title: 'Task',
        home: new taskScreen(),
        routes: <String, WidgetBuilder>{
          "/Completed": (BuildContext context) => new Completed()
        }
    );
  }
}

class taskScreen extends StatefulWidget{
  @override
  taskState createState() => new taskState();
}

class taskState extends State<taskScreen> {

  @override
  Widget build(BuildContext context) {

  Column taskScreen = Column (...)

    return Scaffold(
      appBar: AppBar(
          title: Text('Task Screen')),
      body: taskScreen,
    );
  }

}

Upvotes: 2

Views: 1954

Answers (3)

tommmm
tommmm

Reputation: 322

Remove the second routes MaterialApp and replace it with an AppBar

Upvotes: -1

user14430732
user14430732

Reputation: 21

For me it happened when I added a draw to all my various AppBars ... quick googling provided a solution which worked for me: I added to the AppBar the following which let flutter take a conscious decision whether there is or is not what to go back to ...

automaticallyImplyLeading: true,

Then I added defined how the "leading" should look like (also to the appbar):

leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context, false),
        ),

Making a long story short: Adding the following to the respective AppBar did the work:

        leading: IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () => Navigator.pop(context, false),
    ),

Upvotes: 2

Arthur Khabirov
Arthur Khabirov

Reputation: 146

Class name must to start with uppercase character. You have wrong structure, MaterialApp must to be only one time. https://dart.dev/guides/language/language-tour

Upvotes: 2

Related Questions