Skip to content Skip to sidebar Skip to footer

Flutter Call Form Save Function From Another File

I have this event form, it's to create or edit events data. The save button is inside the app bar action, and the form is inside the body. In this project, I have all of the widget

Solution 1:

You can copy paste run each files below Step 1: Use final keyForm = GlobalKey<EventFormFormState>(); Step 2: Pass keyForm to EventFormForm(key: keyForm) Step 3: In IconButton call keyForm.currentState.saveForm();

IconButton(
          icon: Icon(Icons.save),
          onPressed: () {
            keyForm.currentState.saveForm();
          })

working demo

enter image description here

full code

enter image description here

main.dart

import'package:flutter/material.dart';
import'event_form.dart';
import'event_form_form.dart';

voidmain() => runApp(MyApp());
finalkeyForm= GlobalKey<EventFormFormState>();

classMyAppextendsStatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: EventForm(),
    );
  }
}

event_form.dart

import'package:flutter/material.dart';

import'event_form_appbar.dart';
import'event_form_body.dart';
classEventFormextendsStatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: EventFormAppBar(), body: EventFormBody());
  }
}

event_form_appbar.dart

import'package:flutter/material.dart';
import'main.dart';

classEventFormAppBarextendsPreferredSize {
  @override
  Widget build(BuildContext context) {
    return AppBar(actions: <Widget>[
      IconButton(
          icon: Icon(Icons.save),
          onPressed: () {
            keyForm.currentState.saveForm();
          })
    ]);
  }

  @overridegetpreferredSize=> Size.fromHeight(50);
}

event_form_body.dart

import'package:flutter/material.dart';
import'main.dart';
import'event_form_form.dart';

classEventFormBodyextendsStatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SingleChildScrollView(
        child: EventFormForm(key: keyForm),
      ),
    );
  }
}

event_form_form.dart

import'package:flutter/material.dart';

classEventFormFormextendsStatefulWidget {
  EventFormForm({Key key}) : super(key: key);

  @overrideEventFormFormStatecreateState() {
    returnEventFormFormState();
  }
}

classEventFormFormStateextendsState<EventFormForm> {
  final _formKey = GlobalKey<FormState>();

  @overrideWidgetbuild(BuildContext context) {
    returnForm(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          TextFormField(
            validator: (value) {
              if (value.isEmpty) {
                return'Please enter some text';
              }
              returnnull;
            },
          ),
          Padding(
            padding: constEdgeInsets.symmetric(vertical: 16.0),
            child: RaisedButton(
              onPressed: () {
                saveForm();
              },
              child: Text('Submit'),
            ),
          ),
        ],
      ),
    );
  }

  voidsaveForm() {
    print("execute save Form");
    if (_formKey.currentState.validate()) {
      // If the form is valid, display a Snackbar.Scaffold.of(context)
          .showSnackBar(SnackBar(content: Text('Processing Data')));
    }
  }
}

Post a Comment for "Flutter Call Form Save Function From Another File"