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/data/address.dart'; import 'package:huixiang/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 arguments; EditAddressPage({required 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(), context: context, token: value.getString('token')), queryAddress(), }); } Address? preAddress; queryAddress() async { preAddress = Address.fromJson(widget.arguments); nameController.text = "${preAddress?.username}"; mobileController.text = "${preAddress?.phone}"; addressController.text = "${preAddress?.province}${preAddress?.city}${preAddress?.area}"; houseNumberController.text = "${preAddress?.addressStr}"; 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: GestureDetector( behavior: HitTestBehavior.translucent, onTap: () { FocusScope.of(context).requestFocus(FocusNode()); }, child:Column( children: [ Container( margin: EdgeInsets.only(bottom: 23.h), padding: EdgeInsets.only(top: 16.h, bottom: 15.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?.username ?? "", S.of(context).qingtianxiexingming, nameController, false), editItem( S.of(context).dianhua, preAddress?.phone ?? "", S.of(context).qingtianxieshoujihao, mobileController, false), InkWell( onTap: () { toMap(); }, child: editItem( S.of(context).dizhi, preAddress?.addressStr ?? "", S.of(context).shouhuodizhi, addressController, true), ), editItem( S.of(context).xiangxidizhi, preAddress?.addressStr ?? "", S.of(context).menpaihao, houseNumberController, false), ], ), ), InkWell( onTap: () { saveOrUpdate(); }, child: Container( width: MediaQuery.of(context).size.width, margin:EdgeInsets.symmetric(horizontal: 16.w), padding: EdgeInsets.all(16), decoration: BoxDecoration( borderRadius: BorderRadius.circular(26), color: Color(0xFF32A060), ), 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 as Map, addressController.text = "${(addressMap)?['province']}${(value)['city']}${(value)['area']}", { preAddress?.province = addressMap?['province'], preAddress?.city = addressMap?['city'], preAddress?.area = addressMap?['area'], }, houseNumberController.text = "${(value)['address']}", } // setState(() {}) }, ); } saveOrUpdate() async { String name = nameController.text; String mobile = mobileController.text; String city = addressController.text; String address = houseNumberController.text; if (name.isEmpty) { SmartDialog.showToast(S.of(context).shouhuorenxingming, alignment: Alignment.center); return; } if (mobile.isEmpty) { SmartDialog.showToast(S.of(context).shouhuorenshoujihao, alignment: Alignment.center); return; } if (city.isEmpty) { SmartDialog.showToast(S.of(context).shurushouhuorendizhi, alignment: Alignment.center); return; } if (address.isEmpty) { SmartDialog.showToast(S.of(context).shouhuorenxiangxidizhi, alignment: Alignment.center); return; } BaseData? baseData = await apiService?.updateAddress({ "address": address, "area": preAddress?.area ?? "", "city": preAddress?.city ?? "", "province": preAddress?.province ?? "", "cityInfo": "", "isDefault": true, "latitude": preAddress?.latitude ?? 0, "longitude": preAddress?.longitude ?? 0, "phone": mobile, "tag": "", "id": preAddress?.id ?? 0, "username": name }); if (baseData?.isSuccess ?? false) { 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, ) ], ), ); } }