import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:huixiang/retrofit/data/address.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:shared_preferences/shared_preferences.dart'; class EditAddressPage extends StatefulWidget { final Map arguments; EditAddressPage({this.arguments}); @override State createState() { return _EditAddressPage(); } } class _EditAddressPage extends State { TextEditingController nameController = TextEditingController(); TextEditingController mobileController = TextEditingController(); TextEditingController addressController = TextEditingController(); TextEditingController houseNumberController = TextEditingController(); ApiService apiService; @override void initState() { super.initState(); SharedPreferences.getInstance().then((value) => { apiService = ApiService(Dio(), token: value.getString('token')), queryAddress(), }); } Address preAddress; queryAddress() async { if (widget.arguments == null) return; preAddress = Address.fromJson(widget.arguments); nameController.text = preAddress.username; mobileController.text = preAddress.phone; addressController.text = "${preAddress.province}${preAddress.city}${preAddress.area}"; houseNumberController.text = preAddress.address; setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar( title: "编辑地址", titleColor: Colors.black, leadingColor: Colors.black, background: Colors.white, ), body: Container( child: Column( children: [ Container( margin: EdgeInsets.only(bottom: 26), padding: EdgeInsets.only(top: 16, bottom: 16), decoration: BoxDecoration( color: Colors.white, boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ), ], borderRadius: BorderRadius.only( bottomLeft: Radius.circular(8), bottomRight: Radius.circular(8), ), ), child: Column( children: [ editItem("姓名", preAddress != null ? preAddress.username : "", "请填写收件人姓名", nameController, false), editItem("电话", preAddress != null ? preAddress.phone : "", "请填写收件人手机号", mobileController, false), InkWell( onTap: () { toMap(); }, child: editItem( "地址", preAddress != null ? preAddress.address : "", "请输入详细收货地址", addressController, true), ), editItem("详细地址", preAddress != null ? preAddress.address : "", "请输入门牌号", houseNumberController, false), ], ), ), InkWell( onTap: () { saveOrUpdate(); }, child: Container( color: Color(0xFF32A060), width: MediaQuery.of(context).size.width, padding: EdgeInsets.all(16), alignment: Alignment.center, child: Text( "保存", style: TextStyle( fontWeight: FontWeight.bold, fontSize: 16, color: Colors.white, ), ), ), ), ], ), ), ); } Map addressMap; toMap() async { Navigator.of(context).pushNamed('/router/address_map_page').then( (value) => { addressMap = value, addressController.text = "${(value as Map)['province']}${(value as Map)['city']}${(value as Map)['area']}", houseNumberController.text = "${(value as Map)['address']}", // setState(() {}) }, ); } saveOrUpdate() async { String name = nameController.text; String mobile = mobileController.text; String city = addressController.text; String address = houseNumberController.text; if (name == null || name == "") { Fluttertoast.showToast(msg: "请输入收货人姓名"); return; } if (mobile == null || mobile == "") { Fluttertoast.showToast(msg: "请输入收货人手机号"); return; } if (city == null || city == "") { Fluttertoast.showToast(msg: "请输入收货人地址"); return; } if (address == null || address == "") { Fluttertoast.showToast(msg: "请输入收货人详细地址"); return; } BaseData baseData; if (preAddress == null) { baseData = await apiService.addAddress({ "address": address, "area": addressMap != null ? addressMap['area'] : "", "city": addressMap != null ? addressMap['city'] : "", "cityInfo": "", "isDefault": true, "latitude": addressMap != null ? addressMap['latitude'] : 0, "longitude": addressMap != null ? addressMap['longitude'] : 0, "phone": mobile, "province": addressMap != null ? addressMap['province'] : "", "tag": "", "username": name }); } else { baseData = await apiService.updateAddress({ "address": address, "area": addressMap != null ? addressMap['area'] : "", "city": addressMap != null ? addressMap['city'] : "", "cityInfo": "", "isDefault": true, "latitude": addressMap != null ? addressMap['latitude'] : 0, "longitude": addressMap != null ? addressMap['longitude'] : 0, "phone": mobile, "province": addressMap != null ? addressMap['province'] : "", "tag": "", "username": name }); } if (baseData.isSuccess) { Fluttertoast.showToast(msg: preAddress == null ? "保存成功" : "修改成功"); Navigator.of(context).pop(); } else { Fluttertoast.showToast(msg: baseData.msg); } } Widget editItem(start, text, hide, controller, isClick) { return Container( margin: EdgeInsets.symmetric(horizontal: 16, vertical: 5), child: Row( children: [ Expanded( child: Text( start, style: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Color(0xFF353535), ), ), flex: 1, ), Expanded( child: TextField( controller: controller, enabled: start != "地址", keyboardType: start == "电话" ? TextInputType.phone : TextInputType.text, decoration: InputDecoration( border: InputBorder.none, hintText: hide, hintStyle: TextStyle( color: Color(0xFFA29E9E), ), contentPadding: EdgeInsets.all(0), ), ), flex: 3, ), if (isClick) Icon( Icons.keyboard_arrow_right, color: Colors.black, size: 24, ) else SizedBox( width: 24, ) ], ), ); } }