Turf
Turf

Reputation: 434

Skip a particular page flutter

I'm trying to skip a particular page with Navigator.pop. I made a page that is used to do some filtered searches and when I click on the record I want it takes me to a particular page and up to here it works as it should, but when I am on the new page and I press the back button it takes me back to the page " search "instead of taking me to the page before the search form, so I think I should skip a state of the navigation stack ONLY IF the state directs to the search page otherwise it shouldn't skip.

This code is used for the back button in the AppBar:

onPressed: () => Navigator.of(context).pop(),

This is my search page

import 'package:flutter/material.dart';
import 'package:solaris/lista_data.dart';

import 'constants.dart';
class LinkItemsSearch extends SearchDelegate<LinkItem>{

  @override
  List<Widget> buildActions(BuildContext context) {
    return [IconButton(icon: Icon(Icons.clear),onPressed: () { query=""; },)];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(onPressed: () { Navigator.pop(context); }, icon: Icon(Icons.arrow_back),);
  }

  @override
  Widget buildResults(BuildContext context) {
    // TODO: implement buildResults
    throw UnimplementedError();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    final mylist = query.isEmpty? loadLinkItem():loadLinkItem().where((p) => p.title.startsWith(query)).toList();
    return mylist.isEmpty?
    Container(
      color: blue,
      child: Center(
          child: Text('No Result Found . . .', style: TextStyle(color: Colors.white,fontSize: 20,)
          )
      ),
    ):Container(
      color: blue,
      child: ListView.builder(
          itemCount: mylist.length,
          itemBuilder: (context,index){
            final LinkItem listitem = mylist[index];
            return Container(
              color: blue,
              child: ListTile(onTap: (){ showResults(context);},
                title:InkWell(
                  onTap: () { Navigator.push(context,MaterialPageRoute(builder: (context) => listitem.link)); },
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget> [
                      Text(listitem.title, style: TextStyle(color: Colors.white),),
                      Text(listitem.description, style: TextStyle(color: Colors.white,fontSize: 14),),
                      Divider(color: white,),
                    ],
                  ),
                ),
              ),
            );
          }
      ),
    );
  }

}

Upvotes: 1

Views: 1142

Answers (2)

Gustavo Guzman
Gustavo Guzman

Reputation: 146

If you have named routes you could use Navigator.popUntil(context, ModalRoute.withName('/nameOfMyRoute')). I think this should work.

Upvotes: 2

AbdulAziz Umar
AbdulAziz Umar

Reputation: 433

Use Navigator.pushReplacement() instead of Navigator.push()

This will replace the new page with the one already on the screen instead of pushing it on top of it.

Upvotes: 4

Related Questions