Home > OS >  How to move 2 steps back in flutter?
How to move 2 steps back in flutter?

Time:07-27

I have two screens

First one showing all products named productoverviewscreen

second one showing product details(whichever is clicked) named productdetailscreen

in second screen I have placed delete button to delete product with alertdialog for confirmation...

after deleting...should be moved to productoverviewscreen...I have used

two times pop() which I think awkward..is there any good way...show dialog must return value false or true.

here is my code

class ProductOverviewScreen extends StatefulWidget {
  @override
  State<ProductOverviewScreen> createState() => _ProductOverviewScreenState();
}

class _ProductOverviewScreenState extends State<ProductOverviewScreen> {
  Color appcolor = Colors.orange;
  var showonlyfav=false;

  List<Color> colors=[Colors.red,Colors.green,Colors.blue,Colors.orange,Colors.purple];
  @override
  Widget build(BuildContext context) {
//todo after changng this to statefull....appbar color changed on each menu selection

  //final cart=Provider.of<CartProvider>(context);


    print('I am rebuild');
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'MyShop',
        ),
        actions: [
          //TODO set fav to default
          PopupMenuButton(

            onSelected: (filteroptions selectedvalue){
              setState(() {
                if(selectedvalue==filteroptions.Fav)
                {
                  showonlyfav=true;
                }
                else
                {
                  showonlyfav=false;
                }
              });
            },
            icon: Icon(Icons.menu),

              itemBuilder: (context) {
            return [
              PopupMenuItem(
                  child: Text(
                    'Only Fav',
                  ),
                  value: filteroptions.Fav),
              PopupMenuItem(
                  child: Text(
                    'All',
                  ),
                  value: filteroptions.All),
            ];
          }),
          Consumer<CartProvider>(
            builder: (context,cartprovider,child){
              return  Badge(value: cartprovider.itemCount.toString(), child: IconButton(icon: Icon(Icons.shopping_cart), onPressed: () {

                Navigator.of(context).push(MaterialPageRoute(builder: (context){
                  return CartScreen();
                }));

              },), color: Colors.red);
            },

          ),

        ],
        backgroundColor: colors[Random().nextInt(5)],
      ),
      drawer: AppDrawer(),
      body: ProductsGrid(showonlyfav),
    );
  }
}

and here is my product detail screen

class ProductDetailScreen extends StatelessWidget {
  static const routname = 'productdetailscreen';


  @override
  Widget build(BuildContext context) {
    final id = ModalRoute.of(context)!.settings.arguments as String;
    final productdata = Provider.of<ProductsProvider>(context,listen: false);
    final loadedprouduct = productdata.getproductbyid(id);

    return Scaffold(

        appBar: AppBar(
          title: Text(loadedprouduct.title),
        ),

        body: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                height: 300,
                width: double.infinity,
                child: Image.network(
                  loadedprouduct.imageUrl,
                  fit: BoxFit.cover,
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Text(loadedprouduct.price.toStringAsFixed(2)),
              SizedBox(
                height: 10,
              ),
              Container(
                width: double.infinity,
                padding: EdgeInsets.symmetric(horizontal: 50),
                child: Text(
                  loadedprouduct.description.toString(),
                  softWrap: true,
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          ),
        ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.delete),
        onPressed: () {
        showDialog(context: context, builder: (ctx){
           return  AlertDialog(title: Text('Deletion....'),
            content: Text('Are u sure to Delete'),
            actions: [TextButton(onPressed: (){
              productdata.removeproduct(id);
              

Navigator.of(context).pop(true);
              Navigator.of(context).pop();



            }, child: Text('Yes')),
            TextButton(onPressed: (){
              Navigator.of(context).pop(false);
            }, child: Text('No'))],);
          });

        },
      ),);
  }
}

this is what I feel owkward

Navigator.of(context).pop(true);
    Navigator.of(context).pop();

CodePudding user response:

You directly use

 Navigator.of(context).pushReplacementNamed(
              "/routeName",
              arguments: true, // arguemnts
            );

Or

 Navigator.of(context).pushReplacement(
  MaterialPageRoute(
      builder: (context) => HomeScreen(),
      settings: RouteSettings(
        arguments: true,
      )),
);

And receive like

 final args = ModalRoute.of(context)?.settings.arguments;

More about navigate-with-arguments

CodePudding user response:

You can use popUntil method

  Navigator.popUntil(context, ModalRoute.withName('/login'));

  • Related