我有一个form
,当我单击我的Save
中的一个button
来appbar
数据时,Stepper 1
的数据将被提交并显示为空事件我最终还是要输入数据。它会显示我在第二个Stepper
中输入的数据,但是如果有必要的话,第一个总是会被删除。特别是当我返回到Continue
时,从step 1
到step 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,
),
),
),
],
),
);
}
}
我通过将onSaved
更改为onchanged
解决了这个问题,现在所有文件都已提交并且可以正常工作!
如果onSaved
用TextFormField
小部件包装并且保存Form
,则将触发Form
方法。根据文档:
不需要表格祖先。表格只是使其更容易一次保存,重置或验证多个字段。
onSaved:保存表单时使用最终值调用的可选方法通过FormState.save。
因为您没有使用Form
小部件,可以使用onFieldSubmitted
,因此当用户完成编辑TextFormField
时,将触发onFieldSubmitted
。
如果使用onChanged
,则每次用户在TextFormField
上键入该方法都会触发。