flutterdartflutter-getx

Flutter GetX navigate to same page but with different data


I am using GetX with my flutter project. In home page when user taps a product, it is navigated to ProductDetails like this

Get.to(() => const PropductDetails(), arguments: [
  {"Details": item}
]);

And in ProductDetails page there is list of related products, now when product is tapped, I want user navigate again to ProductDetails page but with new product details. When user taps back, he will be seeing the previously viewed Product details page.

I used same code as above in ProductDetails page

Get.to(() => const ProductDetails(), arguments: [
  {"Details": relatedItem}
]); 

Here is the minimal code of the ProductDetails view:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class ProductDetails extends StatelessWidget {
  const ProductDetails({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, //or set color with: Color(0xFF0000FF)
    ));

    return ProductDetailsBuilder(context).build();
  }
}

class ProductDetailsBuilder {
  ProductDetailsBuilder(this.context);
  final BuildContext context;

  final controller = Get.put(ProductDetailsController());

  Widget build() {
    return Scaffold(
      backgroundColor: Colors.white,
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.blue,
        elevation: 0,
        systemOverlayStyle: SystemUiOverlayStyle.light,
      ),
      // add this body tag with container and photoview widget
      body: relatedProducts(),
    );
  }

  Widget relatedProducts() {
    return Column(
      children: List.generate(controller.listRelatedProducts.length, (index) {
        var item = controller.listRelatedProducts[index];

        return Container(
          color: Colors.grey,
          width: double.infinity,
          child: ElevatedButton(
            child: Text(item.label),
            onPressed: () {
              Get.to(() => const ProductDetails(), arguments: [
                {"Details": item}
              ]);
            },
          ),
        );
      }),
    );
  }
}

But this doesn't seem to work. Can anybody please help me in this?

Thanks


Solution

  • You can set preventDuplicates to false:

    Get.to(() => const ProductDetails(), 
        arguments: [{"Details": item}], 
        preventDuplicates: false
    );