You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.2 KiB

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<String, dynamic> arguments;
EditAddressPage({this.arguments});
@override
State<StatefulWidget> createState() {
return _EditAddressPage();
}
}
class _EditAddressPage extends State<EditAddressPage> {
TextEditingController nameController = TextEditingController();
TextEditingController mobileController = TextEditingController();
TextEditingController addressController = TextEditingController();
TextEditingController houseNumberController = TextEditingController();
ApiService apiService;
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
4 years ago
apiService = ApiService(Dio(), context: context, token: value.getString('token')),
4 years ago
queryAddress(),
});
}
Address preAddress;
queryAddress() async {
4 years ago
if (widget.arguments == null) return;
preAddress = Address.fromJson(widget.arguments);
4 years ago
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: [
4 years ago
editItem("姓名", preAddress != null ? preAddress.username : "",
"请填写收件人姓名", nameController, false),
editItem("电话", preAddress != null ? preAddress.phone : "",
"请填写收件人手机号", mobileController, false),
InkWell(
onTap: () {
4 years ago
toMap();
},
4 years ago
child: editItem(
"地址",
preAddress != null ? preAddress.address : "",
"请输入详细收货地址",
addressController,
true),
),
4 years ago
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,
),
),
),
),
],
),
),
);
}
4 years ago
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']}",
4 years ago
if(preAddress != null) {
preAddress.province = addressMap['province'],
preAddress.city = addressMap['city'],
preAddress.area = addressMap['area'],
},
4 years ago
houseNumberController.text = "${(value as Map)['address']}",
// setState(() {})
},
);
}
saveOrUpdate() async {
String name = nameController.text;
String mobile = mobileController.text;
4 years ago
String city = addressController.text;
String address = houseNumberController.text;
if (name == null || name == "") {
Fluttertoast.showToast(msg: "请输入收货人姓名");
4 years ago
return;
}
4 years ago
if (mobile == null || mobile == "") {
Fluttertoast.showToast(msg: "请输入收货人手机号");
4 years ago
return;
}
4 years ago
if (city == null || city == "") {
Fluttertoast.showToast(msg: "请输入收货人地址");
4 years ago
return;
}
4 years ago
if (address == null || address == "") {
Fluttertoast.showToast(msg: "请输入收货人详细地址");
4 years ago
return;
}
BaseData baseData;
4 years ago
if (preAddress == null) {
baseData = await apiService.addAddress({
"address": address,
4 years ago
"area": addressMap != null ? addressMap['area'] : "",
"city": addressMap != null ? addressMap['city'] : "",
"cityInfo": "",
"isDefault": true,
4 years ago
"latitude": addressMap != null ? addressMap['latitude'] : 0,
"longitude": addressMap != null ? addressMap['longitude'] : 0,
"phone": mobile,
4 years ago
"province": addressMap != null ? addressMap['province'] : "",
"tag": "",
"username": name
});
} else {
baseData = await apiService.updateAddress({
"address": address,
4 years ago
"area": preAddress != null ? preAddress.area : "",
"city": preAddress != null ? preAddress.city : "",
"province": preAddress != null ? preAddress.province : "",
4 years ago
"cityInfo": "",
"isDefault": true,
4 years ago
"latitude": preAddress != null ? preAddress.latitude : 0,
"longitude": preAddress != null ? preAddress.longitude : 0,
"phone": mobile,
"tag": "",
4 years ago
"id": preAddress != null ? preAddress.id : 0,
"username": name
});
}
4 years ago
if (baseData != null && 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,
4 years ago
enabled: start != "地址",
keyboardType:
start == "电话" ? TextInputType.phone : TextInputType.text,
decoration: InputDecoration(
border: InputBorder.none,
hintText: hide,
hintStyle: TextStyle(
color: Color(0xFFA29E9E),
),
contentPadding: EdgeInsets.all(0),
),
),
4 years ago
flex: 3,
),
if (isClick)
Icon(
Icons.keyboard_arrow_right,
color: Colors.black,
size: 24,
)
else
SizedBox(
width: 24,
)
],
),
);
}
}