我的表格没有提交我输入的所有数据
My form isnt submitting all the data I enter
我有一个 form
,当我 Save
通过单击 appbar
中的 button
数据时, Stepper 1
的数据被提交并显示空 eventho 我最终输入了数据。它显示我在第二个 Stepper
中输入的数据,但如果有意义的话,第一个总是以某种方式被删除。特别是当我点击 Continue
从 step 1
到 step 2
如果我回到 step 1
我有时会发现它是空的 eventho 我会输入数据吗?有人可以帮忙吗!
我的代码如下所示:
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,
),
),
),
],
),
);
}
}
我通过将 onSaved
更改为 onchanged
解决了这个问题,现在所有内容都已提交并且工作正常!
我有一个 form
,当我 Save
通过单击 appbar
中的 button
数据时, Stepper 1
的数据被提交并显示空 eventho 我最终输入了数据。它显示我在第二个 Stepper
中输入的数据,但如果有意义的话,第一个总是以某种方式被删除。特别是当我点击 Continue
从 step 1
到 step 2
如果我回到 step 1
我有时会发现它是空的 eventho 我会输入数据吗?有人可以帮忙吗!
我的代码如下所示:
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,
),
),
),
],
),
);
}
}
我通过将 onSaved
更改为 onchanged
解决了这个问题,现在所有内容都已提交并且工作正常!