I have a stateless widget that returns a button widget inside of the builder method. I want this stateless widget to be called from different parts of the application but I want the widget to return a button that I pass in with a function to be called. How can I do this?
The buttons that I will be passing in could be –
- ElevatedButton
- OutlineButton
- IconButton
Here is my stateless widget that returns an ElevatedButton.
class FileUploadMutation extends StatelessWidget {
const FileUploadMutation(this.user, this.switchScreenFn, {Key? key})
: super(key: key);
final User user;
final Function switchScreenFn;
@override
Widget build(BuildContext context) {
return Mutation(
options: MutationOptions(
document: gql(fileUploadMutation),
),
builder: (runMutation, result) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
elevation: 0,
),
onPressed: () async {
List<MultipartFile> files = user.files;
runMutation(
{
'containerName': containerName,
'files': files,
},
);
},
child: const Text("Continue"),
);
},
);
}
}
2
Answers
I don’t understand your code but I think you need to Crete a button that can use anywhere ? if so you can Crete your button like this
when you need to use this button just use like this
Create your custom Widget like this and call it and define anywhere in your code . onPresses parameter will execute your method call .
And Custom widget will look like .You call :-