'How do I use :onpressed correctly in Flutter TextFormField?

I am new to Flutter. I am trying to have on onPressed event that will save data to my api. Unfortunately when a onPressed button is clicked only one field is saved (title) and no other fields are saved. I have found that onFieldSubmitted and onChanged works easily but I would prefer to have the data saved once the client has viewed their input.

Cheers for the help in advance.

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tradepm_v5_client/app/data/model/project.dart';
import 'package:tradepm_v5_client/app/modules/admin/admin_project/controllers/actions/new_project_controller.dart';

class NewProjectView3 extends StatefulWidget {
  const NewProjectView3({Key? key}) : super(key: key);

  @override
  _NewProjectView3State createState() => _NewProjectView3State();
}

class _NewProjectView3State extends State<NewProjectView3>
    with AutomaticKeepAliveClientMixin {
  int currentStep = 0;
  final title = TextEditingController();
  final description = TextEditingController();
  final street_number = TextEditingController();
  final street_name = TextEditingController();
  final suburb = TextEditingController();
  final city = TextEditingController();
  final post_code = TextEditingController();
  final controller = Get.put(NewProjectController());
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Stepper(
        controlsBuilder: (BuildContext context, ControlsDetails details) {
          final isLastStep = currentStep == getSteps().length - 1;

          return

            Padding(
              padding: const EdgeInsets.fromLTRB(0, 12, 0, 0),
              child: Row(
                children: <Widget>[
                  if (isLastStep)
                    Expanded(
                      child: ElevatedButton(
                        child: Text('SUBMIT'),
                        onPressed: () {
                          details.onStepContinue;
                            _formKey.currentState!.save();
                        }

                      ),
                    ),

                  if (currentStep != 2)
                    Expanded(
                      child: ElevatedButton(
                        child: Text('NEXT'),
                        onPressed: details.onStepContinue,
                      ),
                    ),
                  SizedBox(width: 12),
                  if (currentStep != 0)
                    Expanded(
                      child: ElevatedButton(
                        onPressed: details.onStepCancel,
                        child: const Text('BACK'),
                      ),
                    ),

                ],
              ),
            );
        },

        type: StepperType.horizontal,
        steps:getSteps(),
        currentStep: currentStep,
        onStepTapped: (int step)
        {
          setState(() {
            currentStep = step;
          });
        },
        onStepCancel: ()
        {
          currentStep > 0 ?
          setState(() => currentStep -= 1) : null;
        },
        onStepContinue: ()
        {
          currentStep < 2 ?
          setState(() => currentStep += 1): null;
        },
      ),
    );
  }

  List<Step> getSteps()
  {
    return[
      Step(
        title: new Text('Details'),
        content: Form(
          key: _formKey,
          child: Container(
            child: Column(
              children: [
                TextFormField(
                  maxLength: 50,
                  controller: title,
                  onSaved: (value) async {
                    var newProject = Project(
                      title: value,
                    );
                    controller.project.value.title =
                        value;
                    await controller
                        .createProject(newProject);
                  },
                  decoration: const InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: 'Title',
                  ),
                ),
                const SizedBox(
                  height: 8,
                ),
                TextFormField(
                  maxLength: 100,
                  controller: description,
                  onSaved: (value) async {
                      controller.project.value
                          .description = value;
                      await controller.updateProject(
                          controller.project.value);
                  },
                  decoration: const InputDecoration(

                    border: OutlineInputBorder(),
                    labelText: 'Description',

                  ),
                ),
                const SizedBox(
                  height: 8,
                ),

              ],
            ),
          ),
        ),
        isActive: currentStep >= 0,
        state: currentStep == 0 ?
        StepState.editing : StepState.complete,
      ),
      Step(
        title: new Text('Address'),
        content: Container(
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: street_number,
                      maxLength: 4,
                      onSaved: (value) async {
                        if (controller.project.value.id !=
                            '') {
                          controller.project.value
                              .streetNumber = value;
                          await controller.updateProject(
                              controller.project.value);
                        }
                      },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Number',
                      ),
                    ),
                  ),
                  SizedBox(width: 10.0),
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: post_code,
                      maxLength: 4,
                      onSaved: (value) async {
                          controller.project.value
                              .postCode = value;
                          await controller.updateProject(
                              controller.project.value);
                        },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Post Code',
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 8,
              ),
              TextFormField(
                controller: street_name,
                maxLength: 25,
                onSaved: (value) async {
                    controller.project.value
                        .streetName = value;
                    await controller.updateProject(
                        controller.project.value);
                },
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Street Name',

                ),
              ),
              const SizedBox(
                height: 8,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: suburb,
                      maxLength: 25,
                      onSaved: (value) async {
                          controller.project.value
                              .suburb = value;
                          await controller.updateProject(
                              controller.project.value);
                      },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'Suburb',

                      ),
                    ),
                  ),
                  SizedBox(width: 10.0),
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: city,
                      maxLength: 25,
                      onSaved: (value) async {
                          controller.project.value
                              .city = value;
                          await controller.updateProject(
                              controller.project.value);
                        },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                        labelText: 'City',
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
        isActive: currentStep >= 1,
        state: currentStep == 1 ?
        StepState.editing : currentStep < 1 ? StepState.disabled: StepState.complete,
      ),
      Step(
        title: new Text("Confirm"),
        content: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('Title: ${title.text}'),
                Text('Description: ${description.text}'),
                Text('Street Number : ${street_number.text}'),
                Text('Street Name : ${street_name.text}'),
                Text('Suburb : ${suburb.text}'),
                Text('City : ${city.text}'),
                Text('Post Code : ${post_code.text}'),
              ],
            )),
        isActive:currentStep >= 2,
        state: currentStep == 2 ?
        StepState.editing : currentStep < 2 ? StepState.disabled: StepState.complete,
      ),
    ];
  }
  @override
  bool get wantKeepAlive => true;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source