我的表单没有提交我输入的所有数据

时间:2019-10-25 06:46:51

标签: forms flutter submit textfield

我有一个form,当我在自己的Save中单击buttonappbar数据时,Stepper 1的数据被提交并显示为空我确实输入了数据。它会显示我在第二个Stepper中输入的数据,但是如果可以的话,第一个总是会被删除。特别是当我回到Continue时,从step 1step 2单击step 1时,有时会发现它是空的,我会输入数据吗?有人可以帮忙吗!

这是我的代码的外观:

import 'package:flutter/material.dart';
import 'package:flutter_app/app_localization.dart';
import 'package:google_fonts_arabic/fonts.dart';
import 'package:provider/provider.dart';

import '../add_ons/add_car.dart';
import '../providers/car_provider.dart';

import '../drawer/drawer.dart';
import '../screens/cars_screen.dart';
import '../app_localization.dart';

class CreateCar extends StatefulWidget {
  static const routeName = '/create-car';

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

class _CreateCarState extends State<CreateCar> {
  final _form = GlobalKey<FormState>();

  int currStep = 0;
  static AddCar data = new AddCar(
    id: null,
    name: '',
    price: '',
    date: DateTime.now(),
    sponsNum: '',
    model: '',
    engine: '',
    outColor: '',
    inColor: '',
    description: '',
    address: '',
    image: '',
  );
  void _saveForm() {
    final isValid = _form.currentState.validate();
    if (!isValid) {
      return;
    }
    _form.currentState.save();
    Provider.of<Cars>(context, listen: false).addCar(data);
    Navigator.of(context).pushNamed(CarsScreen.routeName);
    print('this works');
  }

  @override
  Widget build(BuildContext context) {
    List<Step> steps = [
      Step(
        title: Text(
          AppLocalizations.of(context).createTabTitle,
        ),
        isActive: true,
        state: StepState.indexed,
        content: Column(
          children: <Widget>[
            TextFormField(
              onSaved: (value) {
                data.name = value;
              },
              ),
            ),
            TextFormField(
              keyboardType: TextInputType.number,
              autocorrect: false,
              onSaved: (value) {
                data.price = value;
              },),
            ),
            DropdownButton(
              value: data.currencyT,
              isExpanded: true,
              items: [
                DropdownMenuItem(
                  child: Text(r'$'),
                  value: r'$',
                ),
                DropdownMenuItem(
                  child: Text('₺'),
                  value: '₺',
                )
              ],
              onChanged: (newValue) {
                setState(() {
                  data.currencyT = newValue;
                });
              },
            ),
            DropdownButton(
              value: data.country,
              isExpanded: true,
              items: [
                DropdownMenuItem(
                  child: Text('تركيا'),
                  value: 'تركيا',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.country = newValue;
                });
              },
            ),
            DropdownButton(
              value: data.city,
              isExpanded: true,
              items: [
                DropdownMenuItem(
                  child: Text('اسطنبول'),
                  value: 'اسطنبول',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.city = newValue;
                });
              },
            ),
            TextFormField(
              onSaved: (value) {
                data.address = value;
              },
              ),
            ),
          ],
        ),
      ),
      Step(
        title: Text(
          AppLocalizations.of(context).createCarDetails,
        ),
        isActive: true,
        state: StepState.indexed,
        content: Column(
          children: <Widget>[
            DropdownButton(
              isExpanded: true,
              value: data.category,
              items: [
                DropdownMenuItem(
                  child: Text('جيب'),
                  value: 'جيب',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.category = newValue;
                });
              },
            ),
            DropdownButton(
              isExpanded: true,
              value: data.company,
              items: [
                DropdownMenuItem(
                  child: Text('مارسيديس'),
                  value: 'مارسيديس',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.company = newValue;
                });
              },
            ),
            TextFormField(
              onSaved: (value) {
                data.model = value;
              },),
            ),
            DropdownButton(
              isExpanded: true,
              value: data.year,
              items: [
                DropdownMenuItem(
                  child: Text('2000'),
                  value: '2000',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.year = newValue;
                });
              },
            ),
            TextFormField(
              onSaved: (value) {
                data.engine = value;
              },),
            ),
            TextFormField(
              keyboardType: TextInputType.number,
              onSaved: (value) {
                data.distanceCovered = double.parse(value);
              },
              decoration: new InputDecoration(
                  labelText: AppLocalizations.of(context).carDetailsDistance,),
            ),
            DropdownButton(
              isExpanded: true,
              value: data.transmission,
              items: [
                DropdownMenuItem(
                  child: Text('يدوي'),
                  value: 'يدوي',
                ),
                DropdownMenuItem(
                  child: Text('أوتوماتيك'),
                  value: 'أوتوماتيك',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.transmission = newValue;
                });
              },
            ),
            DropdownButton(
              isExpanded: true,
              value: data.oilT,
              items: [
                DropdownMenuItem(
                  child: Text('الوقود السائل'),
                  value: 'الوقود السائل',
                ),
              ],
              onChanged: (newValue) {
                setState(() {
                  data.oilT = newValue;
                });
              },
            ),
            TextFormField(
              onSaved: (value) {
                data.outColor = value;
              },
              decoration: new InputDecoration(
                  labelText: AppLocalizations.of(context).carDetailsOutColor,
               ),
            ),
            TextFormField(
              onSaved: (value) {
                data.inColor = value;
              },

              decoration: new InputDecoration(
                  labelText: AppLocalizations.of(context).carDetailsInColor,
                 ),
            ),
            TextFormField(

              onSaved: (value) {
                data.description = value;
              },
              maxLines: 3,
              decoration: new InputDecoration(
                  labelText: AppLocalizations.of(context).detailsDescription,
                 ),
            ),
            TextFormField(
              keyboardType: TextInputType.text,
              onSaved: (value) {
                data.image = value;
              },
             ),
            ),
          ],
        ),
      ),
    ];

    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.save),
                onPressed: _saveForm,
              ),
              IconButton(
                icon: Icon(Icons.arrow_forward),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          ),
        ],
      ),
      drawer: MyDrawer(),
      body: Row(
        children: <Widget>[
          Form(
            key: _form,
            child: Expanded(
              child: Stepper(
                steps: steps,
                type: StepperType.vertical,
                currentStep: this.currStep,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

我通过将onSaved更改为onchanged来解决此问题,现在所有内容都已提交并且可以正常工作!