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.
 
 
 
 
 
 

275 lines
8.9 KiB

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/generated/l10n.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';
import 'package:flutter_screenutil/flutter_screenutil.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) => {
apiService = ApiService(Dio(),
context: context, 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: S.of(context).bianjidizhi,
titleColor: Colors.black,
leadingColor: Colors.black,
background: Colors.white,
),
body: Container(
child: Column(
children: [
Container(
margin: EdgeInsets.only(bottom: 26.h),
padding: EdgeInsets.only(top: 16.h, bottom: 16.h),
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(
S.of(context).xingming,
preAddress != null ? preAddress.username : "",
S.of(context).qingtianxiexingming,
nameController,
false),
editItem(
S.of(context).dianhua,
preAddress != null ? preAddress.phone : "",
S.of(context).qingtianxieshoujihao,
mobileController,
false),
InkWell(
onTap: () {
toMap();
},
child: editItem(
S.of(context).dizhi,
preAddress != null ? preAddress.address : "",
S.of(context).shouhuodizhi,
addressController,
true),
),
editItem(
S.of(context).xiangxidizhi,
preAddress != null ? preAddress.address : "",
S.of(context).menpaihao,
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(
S.of(context).baocun,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.sp,
color: Colors.white,
),
),
),
),
],
),
),
);
}
Map addressMap;
toMap() async {
Navigator.of(context).pushNamed('/router/address_map_page').then(
(value) => {
if (value != null)
{
addressMap = value,
addressController.text =
"${(value as Map)['province']}${(value as Map)['city']}${(value as Map)['area']}",
if (preAddress != null)
{
preAddress.province = addressMap['province'],
preAddress.city = addressMap['city'],
preAddress.area = addressMap['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 == "") {
SmartDialog.showToast(S.of(context).shouhuorenxingming, alignment: Alignment.center);
return;
}
if (mobile == null || mobile == "") {
SmartDialog.showToast(S.of(context).shouhuorenshoujihao, alignment: Alignment.center);
return;
}
if (city == null || city == "") {
SmartDialog.showToast(S.of(context).shurushouhuorendizhi, alignment: Alignment.center);
return;
}
if (address == null || address == "") {
SmartDialog.showToast(S.of(context).shouhuorenxiangxidizhi, alignment: Alignment.center);
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": preAddress != null ? preAddress.area : "",
"city": preAddress != null ? preAddress.city : "",
"province": preAddress != null ? preAddress.province : "",
"cityInfo": "",
"isDefault": true,
"latitude": preAddress != null ? preAddress.latitude : 0,
"longitude": preAddress != null ? preAddress.longitude : 0,
"phone": mobile,
"tag": "",
"id": preAddress != null ? preAddress.id : 0,
"username": name
});
}
if (baseData != null && baseData.isSuccess) {
SmartDialog.showToast(preAddress == null
? S.of(context).baocunchenggong
: S.of(context).xiugaichenggong, alignment: Alignment.center);
Navigator.of(context).pop();
}
}
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.sp,
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,
)
],
),
);
}
}