你需要为此发送一个post请求你需要收集Map中所有TextFiel的值你需要做什么TextEditingController是body测试数据,据我了解,更改字段时,你需要向地图添加一个值。如何做得更好请告诉我。
class CreateOrderWidget extends StatefulWidget {
CreateOrderWidget({Key key}) : super(key: key);
@override
_CreateOrderWidgetState createState() => _CreateOrderWidgetState();
}
class _CreateOrderWidgetState extends State<CreateOrderWidget> {
final formatdate = DateFormat("dd-MM-yyyy");
final formattime = DateFormat("HH:mm");
Map<String, dynamic> body = {'title':'Hello','adress':'Hello','task':'Hello','times':'Hello','dates':'Hello','city':'Hello','views': 'Hello','status':'Hello','worker':'Hello','name':'Hello','phone': 'Hello','price': 'Hello','stavka':'Hello','userid': 'Hello'};
// dataform = '{"title":"Hello","adress":"Hello","task":"Hello","times":"Hello","dates":"Hello","city":"Hello","views": "Hello","status":"Hello","worker":"Hello","name":"Hello","phone": "Hello","price": "Hello","stavka":"Hello","userid": "Hello"}';
void postApi() async {
var catJson = await CatAPI().addOrderApi(body);
setState(() {
print(catJson);
});
}
@override
void initState() {
super.initState();
postApi();
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
body: Container(
decoration: BoxDecoration(
color: Color.fromARGB(255, 255, 255, 255),
),
child: Container(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 36.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(height: 10.0),
Row(
children:<Widget>[
Text("Создание заказа" ,style: TextStyle(
color: Color.fromARGB(255, 0, 0, 0),
fontWeight: FontWeight.w400,
fontSize: 23,
),),]),
SizedBox(height: 18.0),
TextFormField(
keyboardType: TextInputType.text,
maxLength: 10,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Название задачи',
prefixText: '',
suffixText: '',
suffixStyle: TextStyle(color: Colors.green)),
maxLines: 1,
),
SizedBox(height: 18.0),
TextFormField(
maxLength: 200,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Опишите задачу подробно',
helperText: 'Максимум 200 символов.',
labelText: 'Подробное описание задачи',
),
maxLines: 3,
),
/////////////////////////////////////////////////////////////////
SizedBox(height: 18.0),
DateTimeField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
icon: Icon(Icons.date_range),
labelText: 'Укажите дату выполнения работ',
prefixText: '',
suffixText: '',
suffixStyle: TextStyle(color: Colors.green)),
format: formatdate,
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
},),
//////////////////////////////////
SizedBox(height: 18.0),
DateTimeField(
format: formattime,
decoration: const InputDecoration(
border: OutlineInputBorder(),
icon: Icon(Icons.timer),
labelText: 'Укажите время выполнения работ',
prefixText: '',
suffixText: '',
suffixStyle: TextStyle(color: Colors.green)),
onShowPicker: (context, currentValue) async {
final time = await showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
);
return DateTimeField.convert(time);
},
),
//////////////////////////////////////////////////
SizedBox(height: 18.0),
TextFormField(
maxLength: 2,
decoration: const InputDecoration(
border: OutlineInputBorder(),
icon: Icon(Icons.people),
labelText: 'Сколько нужно человек?',
prefixText: '',
suffixText: '',
suffixStyle: TextStyle(color: Colors.green)),
keyboardType: TextInputType.number,
),
SizedBox(height: 18.0),
TextFormField(
maxLength: 6,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
icon: Icon(Icons.payment),
border: OutlineInputBorder(),
labelText: 'Укажите оплату на человека в час',
prefixText: '',
suffixText: 'Руб',
suffixStyle: TextStyle(color: Colors.green)),
maxLines: 1,
),
SizedBox(height: 18.0),
TextFormField(
maxLength: 30,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
icon: Icon(Icons.gps_fixed),
border: OutlineInputBorder(),
labelText: 'Адрес',
prefixText: '',
suffixText: '',
suffixStyle: TextStyle(color: Colors.green)),
maxLines: 1,
),
SizedBox(height: 24.0),
RaisedButton(
color: Colors.blue,
child: Text("Отправить",style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 14,
),),
onPressed: (){postApi();},
),
],
),
),
),
),
);
}
}
这是一个简单的示例,可以帮助您了解如何从中获取数据
TextField
并使用它(您可以在DartPad中对其进行测试,以及有关TextField 和 Controller的文章):那你的呢
Map
,我假设这是要发送到的数据API
(格式Json
)。如果它像你一样复杂(有很多字段):那么最好让自己更容易,并使用Json
代码自动生成器使用序列化来处理模型,而不是Map
直接。下面是如何做到这一点:自动 Json 序列化、JSON 和序列化(都使用json_serializable库,注意依赖关系!)。并且不要忘记编程中的主要规则:如果逻辑重复多次,则必须将其移至单独的方法。