GaspardMerten / flutter_auto_form

A package simplifying the process of creating user friendly forms in Flutter
MIT License
26 stars 3 forks source link

Submit form from a button located somewhere other than below the form #6

Closed point-source closed 3 years ago

point-source commented 3 years ago

How can I programmatically cause the form to submit? Sometimes, I want to locate the submit button elsewhere or put something between the form and the button in such a way as a child widget would not necessarily allow. Is there a way to get a callback or provide a key so I can tell the form to submit?

Alternatively, a way to just access the current form state would be fine as well.

GaspardMerten commented 3 years ago

Hello @point-source,

You can implement such behaviour using the key argument of the AFWidget to access the AFWidgetState.submitForm method !

Here is a little example:

  final GlobalKey<AFWidgetState> formStateKey = GlobalKey<AFWidgetState>();

  @override
  Widget build(BuildContext context) {
    return ...(
      action: SizedBox(
        width: 180,
        child: Button(
          expanded: true,
          text: campaign == null ? 'Create campaign' : 'Update campaign',
          onPressed: () => formStateKey.currentState.submitForm(),
        ),
      ),
      child: AFWidget(key: formStateKey, ...),
    );
  }
point-source commented 3 years ago

Oh man, I was so close! I tried a key but used the wrong type in the generic. 🤦

Thanks! Closing this