Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

185 rindas
5.4 KiB

  1. import 'package:chat/data/constants.dart';
  2. import 'package:chat/generated/i18n.dart';
  3. import 'package:chat/utils/CustomUI.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:oktoast/oktoast.dart';
  6. //自定义 地址选择
  7. class SelectPage extends StatefulWidget {
  8. @required
  9. final String title;
  10. @required
  11. final dataId;
  12. final str;
  13. final callback;
  14. final provinces;
  15. final cities;
  16. final isSingle;
  17. final int mostNum;
  18. SelectPage(
  19. {Key key,
  20. this.str,
  21. this.title,
  22. this.dataId,
  23. this.callback,
  24. this.provinces,
  25. this.cities,
  26. this.isSingle,
  27. this.mostNum})
  28. : super(key: key);
  29. _SelectPageState createState() => _SelectPageState();
  30. }
  31. class _SelectPageState extends State<SelectPage> {
  32. String leftSelection;
  33. Set<String> tempRankId = new Set();
  34. @override
  35. void initState() {
  36. super.initState();
  37. leftSelection = widget.provinces.keys.first;
  38. tempRankId = widget.dataId.toSet();
  39. }
  40. @override
  41. void dispose() {
  42. tempRankId.clear();
  43. super.dispose();
  44. }
  45. List<Widget> _buildLeftSelection() {
  46. List<Widget> result = [];
  47. widget.provinces.forEach((k, v) => result.add(_buildItem(k, v, true)));
  48. return result;
  49. }
  50. List<Widget> _buildRightSelection() {
  51. List<Widget> result = [];
  52. widget.cities[leftSelection].forEach(
  53. (k, v) => result.add(_buildItem('$leftSelection-$k', v, false)));
  54. return result;
  55. }
  56. Widget _buildItem(key, value, isLeft) {
  57. return InkWell(
  58. onTap: () {
  59. if (isLeft) {
  60. setState(() {
  61. leftSelection = key;
  62. });
  63. } else {
  64. setState(() {
  65. // //常驻城市不能跨国家选中
  66. if (!widget.isSingle &&
  67. tempRankId.length > 0 &&
  68. tempRankId.first.split('-')[0] != leftSelection) {
  69. showToast('常驻城市不能跨国家');
  70. return;
  71. }
  72. if (tempRankId.contains(key)) {
  73. if (!widget.isSingle) tempRankId.remove(key);
  74. } else {
  75. if (tempRankId.length >= widget.mostNum) {
  76. showToast(I18n.of(context)
  77. .most
  78. .replaceFirst('/s1', widget.mostNum.toString()));
  79. return;
  80. }
  81. tempRankId.add(key);
  82. }
  83. });
  84. //单选直接关闭
  85. if (widget.isSingle) {
  86. if (widget.dataId.length != 0 && tempRankId.length >= 2) {
  87. tempRankId.remove(widget.dataId.first);
  88. }
  89. widget.callback(tempRankId);
  90. }
  91. }
  92. },
  93. highlightColor: Colors.transparent,
  94. radius: 0.0,
  95. child: new Container(
  96. height: 50,
  97. child: new Align(
  98. alignment: Alignment.center,
  99. child: new Text(
  100. value,
  101. textScaleFactor: 1.0,
  102. textAlign: TextAlign.center,
  103. style: isLeft
  104. ? TextStyle(
  105. color: leftSelection == key
  106. ? Colors.white
  107. : Constants.BlackTextColor)
  108. : TextStyle(
  109. color: tempRankId.contains(key)
  110. ? Constants.BlueTextColor
  111. : Constants.BlackTextColor),
  112. ),
  113. ),
  114. decoration: BoxDecoration(
  115. color:
  116. leftSelection == key ? Constants.BlueTextColor : Colors.white,
  117. border: Border(top: BorderSide(color: Colors.grey, width: 0.5)),
  118. ),
  119. ));
  120. }
  121. @override
  122. Widget build(BuildContext context) {
  123. Widget appBar = new AppBar(
  124. title: new Text(widget.title, textScaleFactor: 1.0),
  125. elevation: 0,
  126. leading: CustomUI.buildCustomLeading(context),
  127. actions: <Widget>[
  128. new Container(
  129. alignment: Alignment.center,
  130. child: new InkWell(
  131. child: new Padding(
  132. padding:
  133. EdgeInsets.only(right: 15, left: 15, top: 10, bottom: 10),
  134. child: new Text(I18n.of(context).determine,
  135. textScaleFactor: 1.0, style: Constants.AppBarActionTextStyle),
  136. ),
  137. onTap: () {
  138. widget.callback(tempRankId);
  139. },
  140. ),
  141. )
  142. ],
  143. centerTitle: true,
  144. );
  145. return Scaffold(
  146. body: SafeArea(
  147. child: Center(
  148. child: Container(
  149. height: MediaQuery.of(context).size.height,
  150. width: MediaQuery.of(context).size.width,
  151. child: Row(
  152. children: <Widget>[
  153. Container(
  154. width: MediaQuery.of(context).size.width * 0.5,
  155. child: ListView(
  156. children: _buildLeftSelection(),
  157. ),
  158. decoration: BoxDecoration(
  159. border:
  160. Border(right: BorderSide(color: Colors.grey, width: 1)),
  161. )),
  162. Container(
  163. width: MediaQuery.of(context).size.width * 0.5,
  164. child: ListView(
  165. children: _buildRightSelection(),
  166. ),
  167. ),
  168. ],
  169. ),
  170. ),
  171. )),
  172. appBar: appBar,
  173. );
  174. }
  175. }