How to pass an argument from a data table to a modal in flutter

Issue

I’m trying to pass an argument from data table and pushing the values to a modal in a flutter web project. I tried reading a few topics but I’m struggling to find a solution.
Here is how am trying to pass the data to the modal, which I don’t think is correct!-but am newbie in flutter. The solution works for me when I route to the page instead of a modal.

previewEstate(element) {
  showDialog(
    context: context,
    barrierDismissible: true,
    builder: (BuildContext context) {
      return PreviewEstateModal();
      // Navigator.pushReplacement(
      //   context,
      //   PageRouteBuilder(
      //     pageBuilder: (_, __, ___) => PreviewEstateModal(),
      //     transitionDuration: Duration(seconds: 0),
      //     settings: RouteSettings(arguments: element),
      //   ),
      // );
    },
  );
}

The commented part of my code works if I route to that page i.e am able to receive the route setting argument, but I want to do this by using a modal. What could I be doing wrong?

Solution

You can try with following code

previewEstate(element) {
  showDialog(
    context: context,
    barrierDismissible: true,
    builder: (BuildContext context) {
      return PreviewEstateModal(estate: element); // here you pass the arguments
      
    },
  );
}

In your PreviewEstateModal, initialize the argument(EstateSearch)

class PreviewEstateModal extends StatelessWidget {
  final EstateSearch estate;

  PreviewEstateModal({this.estate});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Text(
          '${estate}',/// initilize estate property
        ),
      ),
    );
  }
}

Answered By – Jahidul Islam

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

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