I have a custom widget to choose photos from the gallery that receives parameters in the following way:
class AddPhotoButton extends StatefulWidget {
late Uint8List image;
late String imagePath;
AddPhotoButton(
{Key? key,
required this.image,
required this.imagePath,
}) : super(key: key);
@override
AddPhotoButtonState createState() => AddPhotoButtonState();
}
This in the forward direction works as I intend, let me explain:
- From screen 1 I click on the widget, and choose an image.
- If there is something saved in the arguments I pass to it, the image will be displayed in the widget.
The problem comes when I want to do it the other way around, that is, I want the image I select in the Widget to be passed to the model. I had in mind that it would work similar to C# with Blazor, where all parameters automatically go by reference.
So if I call the widget like this:
Padding(
padding: const EdgeInsets.only(top: 30),
child: AddPhotoButton(image:model.image1, imagePath: model.image1path,),
),
in model.image1 the image should be saved.
But this doesn't happen, since the image is actually being saved inside the Widget.
Is there any way to achieve this? I've been looking around and it seems Dart doesn't accept passing by parameter reference. But I think there has to be some way to get it.
A few years ago I wrote an article about communication between widgets, I leave it here: https://medium.com/comunidad-flutter/comunicaci%C3%B3n-entre-widgets-5f61b6c2e56c
For your case, the easiest thing could be to use Callbacks.
You would have to add this parameter:
When you get the image or route inside the widget
AddPhotoButton
, you would do something like this:And the way to capture that value would be like this: