address_edit.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:provider/provider.dart';
  4. import 'package:bot_toast/bot_toast.dart';
  5. import 'package:twong/api/index.dart';
  6. import 'package:twong/utils/index.dart';
  7. import 'package:twong/config/style.dart';
  8. import 'package:twong/models/address.dart';
  9. import 'package:twong/widgets/app_bar.dart';
  10. import 'package:twong/providers/address.dart';
  11. import 'package:twong/widgets/address_selecter.dart';
  12. import 'package:twong/widgets/circle_check_box.dart';
  13. class EditAddressPage extends StatefulWidget {
  14. final Address data;
  15. EditAddressPage(this.data);
  16. @override
  17. State<StatefulWidget> createState() {
  18. return _EditAddressPageState();
  19. }
  20. }
  21. class _EditAddressPageState extends State<EditAddressPage> {
  22. bool _isAdd = true;
  23. Address _address = Address();
  24. TextEditingController _nameController = TextEditingController();
  25. TextEditingController _phoneController = TextEditingController();
  26. TextEditingController _detailController = TextEditingController();
  27. @override
  28. void initState() {
  29. super.initState();
  30. if(widget.data != null) {
  31. _isAdd = false;
  32. _address = widget.data;
  33. }
  34. }
  35. @override
  36. Widget build(BuildContext context) {
  37. return Scaffold(
  38. backgroundColor: DColors.back,
  39. appBar: DAppBar(_isAdd ? "添加收货地址" : "编辑收货地址",
  40. actions: _isAdd ? null : [
  41. InkWell(
  42. onTap: _delClick,
  43. highlightColor: Colors.transparent,
  44. child: Container(
  45. margin: EdgeInsets.only(right: 12.px),
  46. alignment: Alignment.center,
  47. child: Text("删除地址"),
  48. ),
  49. )
  50. ]),
  51. body: SafeArea(
  52. child: Container(
  53. margin: EdgeInsets.all(12.px),
  54. child: ListView(
  55. physics: ClampingScrollPhysics(),
  56. children: [
  57. _buildName(),
  58. Divider(),
  59. _buildPhone(),
  60. Divider(),
  61. _buildAddress(),
  62. Divider(),
  63. _buildDetail(),
  64. Divider(),
  65. _buildDefault(),
  66. Divider(),
  67. FlatButton(
  68. color: DColors.Main,
  69. shape: StadiumBorder(),
  70. child: Text("立即保存", style: TextStyle(color: Colors.white)),
  71. onPressed: _saveClick,
  72. )
  73. ],
  74. ),
  75. ),
  76. ),
  77. );
  78. }
  79. _delClick() {
  80. Utils.loading();
  81. Network.inst.delAddress(_address.id).then((res) {
  82. Utils.closeLoading();
  83. Provider.of<AddressModel>(context, listen: false)
  84. .delete(_address.id);
  85. BotToast.showText(text: "删除成功");
  86. Navigator.pop(context);
  87. }).catchError((err) {
  88. Utils.closeLoading();
  89. Log.e(err);
  90. });
  91. }
  92. _saveClick () {
  93. _address.id = widget.data?.id;
  94. // _address.is_default = _default ? 1 : 0;
  95. _address.real_name = _nameController.text;
  96. _address.phone = _phoneController.text;
  97. _address.detail = _detailController.text;
  98. // var areas = _area.split(" ");
  99. // _address.province = areas[0];
  100. // _address.city = areas[1];
  101. // _address.district = areas[2];
  102. Utils.loading();
  103. Network.inst.editAddress(_address).then((value) {
  104. Utils.closeLoading();
  105. if(value["id"] != null) {
  106. _address.id = num.parse(value["id"].toString());
  107. }
  108. Provider.of<AddressModel>(context, listen: false)
  109. .update(_address.id, _address);
  110. BotToast.showText(text: "保存成功");
  111. Navigator.pop(context);
  112. }).catchError((err, stack) {
  113. Log.e(err.toString() + stack.toString());
  114. });
  115. }
  116. Widget _buildName() {
  117. if(_address.real_name != null) {
  118. _nameController.text = _address.real_name;
  119. }
  120. return Container(
  121. height: 38.px,
  122. child: Row(
  123. children: [
  124. Container(
  125. width: 64.px,
  126. child: Text("收货人"),
  127. ),
  128. Expanded(child: TextField(
  129. controller: _nameController,
  130. style: TextStyle(fontSize: 13.px),
  131. decoration: InputDecoration(
  132. border: InputBorder.none,
  133. hintText: "收货人"
  134. ),
  135. ))
  136. ],
  137. ));
  138. }
  139. Widget _buildPhone() {
  140. if(_address.phone != null) {
  141. _phoneController.text = _address.phone;
  142. }
  143. return Container(
  144. height: 38.px,
  145. child: Row(
  146. children: [
  147. Container(
  148. width: 64.px,
  149. child: Text("联系电话"),
  150. ),
  151. Expanded(child: TextField(
  152. controller: _phoneController,
  153. style: TextStyle(fontSize: 13.px),
  154. keyboardType: TextInputType.phone,
  155. decoration: InputDecoration(
  156. border: InputBorder.none,
  157. hintText: "联系电话"
  158. ),
  159. ))
  160. ],
  161. ));
  162. }
  163. Widget _buildAddress() {
  164. var _area = _address.province == null ? "所在区域" :
  165. "${_address.province} ${_address.city} ${_address.district}";
  166. return Container(
  167. height: 38.px,
  168. child: Row(
  169. children: [
  170. Container(
  171. width: 64.px,
  172. child: Text("所在区域"),
  173. ),
  174. Expanded(child: InkWell(
  175. highlightColor: Colors.transparent,
  176. onTap: _choiceAddressDialog,
  177. child: Text(_area, style: TextStyle(color: _address.province ==
  178. null ? Colors.grey : Colors.black)),
  179. )),
  180. Icon(Icons.chevron_right, color: Colors.grey)
  181. ],
  182. ));
  183. }
  184. void _choiceAddressDialog() {
  185. FocusScope.of(context).requestFocus(new FocusNode());
  186. showModalBottomSheet(
  187. context: context,
  188. shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(
  189. topLeft: Radius.circular(45.px), topRight: Radius.circular(12.px)
  190. )),
  191. builder: (BuildContext context) {
  192. return AddressSelecter(
  193. onSelected: (province, city, county, code) {
  194. _address.province = province;
  195. _address.city = city;
  196. _address.district = county;
  197. _address.city_id = code;
  198. setState(() {});
  199. },
  200. title: '选择地址',
  201. selectedColor: Colors.red,
  202. unselectedColor: Colors.black);
  203. }
  204. );
  205. }
  206. Widget _buildDetail() {
  207. if(_address.detail != null) {
  208. _detailController.text = _address.detail;
  209. }
  210. return Container(
  211. height: 64.px,
  212. child: Row(
  213. children: [
  214. Container(
  215. width: 64.px,
  216. alignment: Alignment.topLeft,
  217. margin: EdgeInsets.only(top: 6.px),
  218. child: Text("详细地址"),
  219. ),
  220. Expanded(child: TextField(
  221. maxLines: 2,
  222. style: TextStyle(fontSize: 13.px),
  223. controller: _detailController,
  224. decoration: InputDecoration(
  225. border: InputBorder.none,
  226. hintText: "详细地址:如道路、门牌号、小区、楼栋号、单元室等",
  227. ),
  228. ))
  229. ],
  230. ));
  231. }
  232. Widget _buildDefault() {
  233. return Container(
  234. height: 38.px,
  235. child: Row(
  236. children: [
  237. Expanded(child: Text("设为默认收货地址")),
  238. Container(
  239. margin: EdgeInsets.only(right: 6.px),
  240. child: CircleCheckBox((value) {
  241. setState(() {
  242. _address.is_default = value ? 1 : 0;
  243. });
  244. }, initValue: _address.is_default == 1),
  245. )
  246. ],
  247. ));
  248. }
  249. }