Hibok
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.
 
 
 
 
 
 

238 line
7.6 KiB

  1. import 'package:chat/data/constants.dart';
  2. import 'package:chat/generated/i18n.dart';
  3. import 'package:chat/models/money_change.dart';
  4. import 'package:chat/r.dart';
  5. import 'package:chat/utils/CustomUI.dart';
  6. import 'package:chat/utils/MessageMgr.dart';
  7. import 'package:chat/utils/screen.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:oktoast/oktoast.dart';
  11. import 'package:provider/provider.dart';
  12. class CoinBagPage extends StatefulWidget {
  13. @override
  14. _CoinBagPageState createState() => _CoinBagPageState();
  15. }
  16. class _CoinBagPageState extends State<CoinBagPage> {
  17. final TextEditingController _textCtrl = TextEditingController();
  18. FocusNode editFocus = FocusNode();
  19. final TextEditingController _amountCtrl = TextEditingController();
  20. FocusNode amountFocus = FocusNode();
  21. var curAmount = 0;
  22. var curDesc = '';
  23. @override
  24. void initState() {
  25. super.initState();
  26. print('CoinBagPage initState');
  27. }
  28. @override
  29. void dispose() {
  30. _textCtrl.dispose();
  31. amountFocus.dispose();
  32. _amountCtrl.dispose();
  33. editFocus.dispose();
  34. super.dispose();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. if (curDesc == '') {
  39. curDesc = I18n.of(context).little;
  40. }
  41. return Scaffold(
  42. appBar: AppBar(
  43. title: Text(I18n.of(context).red_money),
  44. centerTitle: true,
  45. leading: CustomUI.buildCustomLeading(context),
  46. ),
  47. body: SafeArea(
  48. child: GestureDetector(
  49. onTap: () {
  50. if (editFocus.hasFocus) {
  51. editFocus.unfocus();
  52. }
  53. if (amountFocus.hasFocus) {
  54. amountFocus.unfocus();
  55. }
  56. },
  57. child: SingleChildScrollView(
  58. child: Center(
  59. child: Column(
  60. children: <Widget>[
  61. Container(
  62. padding: EdgeInsets.all(20),
  63. child: Column(
  64. children: <Widget>[
  65. _buildAmountField(),
  66. SizedBox(height: 10),
  67. _buildTitleTextField(),
  68. Padding(
  69. padding: EdgeInsets.symmetric(vertical: 30),
  70. child: Row(
  71. mainAxisAlignment: MainAxisAlignment.center,
  72. children: <Widget>[
  73. Text(
  74. '$curAmount',
  75. textScaleFactor: 1.0,
  76. style: TextStyle(
  77. fontWeight: FontWeight.w500,
  78. fontSize: 30),
  79. ),
  80. //SizedBox(width: 5),
  81. Text(
  82. I18n.of(context).mask_coin,
  83. textScaleFactor: 1.0,
  84. style: TextStyle(fontSize: 18),
  85. ),
  86. //Image.asset(R.assetsImagesCoin, scale: 2),
  87. ],
  88. )),
  89. _buildSendBtn(),
  90. ],
  91. ))
  92. ],
  93. ),
  94. )),
  95. )));
  96. }
  97. _buildAmountField() {
  98. return Container(
  99. padding: const EdgeInsets.fromLTRB(8, 8, 0, 8),
  100. height: 40.0,
  101. width: Screen.width - 60,
  102. alignment: Alignment.center,
  103. decoration: BoxDecoration(
  104. color: Colors.white, borderRadius: BorderRadius.circular(10.0)),
  105. child: TextField(
  106. keyboardAppearance: Brightness.light,
  107. controller: _amountCtrl,
  108. focusNode: amountFocus,
  109. textAlign: TextAlign.right,
  110. textAlignVertical: TextAlignVertical.center,
  111. textInputAction: TextInputAction.done,
  112. style: TextStyle(textBaseline: TextBaseline.alphabetic, fontSize: 16),
  113. keyboardType: TextInputType.number,
  114. inputFormatters: <TextInputFormatter>[
  115. LengthLimitingTextInputFormatter(10) //限制长度
  116. ],
  117. onChanged: (String text) {
  118. if (text.length == 0) {
  119. curAmount = 0;
  120. } else {
  121. curAmount = int.parse(text);
  122. }
  123. setState(() {});
  124. },
  125. decoration: InputDecoration(
  126. contentPadding: EdgeInsets.only(top: 8, bottom: 8),
  127. hintText: curAmount.toString(),
  128. hintStyle:
  129. TextStyle(fontSize: 16, color: Constants.LightGreyTextColor),
  130. prefixIcon: Container(
  131. width: 65,
  132. alignment: Alignment.centerLeft,
  133. child: Text(I18n.of(context).mount),
  134. ),
  135. border: InputBorder.none,
  136. suffixIcon: Container(
  137. width: 50,
  138. child: Image.asset(R.assetsImagesCoin, scale: 3),
  139. alignment: Alignment.center,
  140. ),
  141. ),
  142. ),
  143. );
  144. }
  145. _buildTitleTextField() {
  146. return Container(
  147. padding: const EdgeInsets.fromLTRB(8, 8, 0, 8),
  148. height: 100.0,
  149. width: Screen.width - 60,
  150. alignment: Alignment.center,
  151. decoration: BoxDecoration(
  152. color: Colors.white, borderRadius: BorderRadius.circular(12.0)),
  153. child: TextField(
  154. keyboardAppearance: Brightness.light,
  155. controller: _textCtrl,
  156. focusNode: editFocus,
  157. textAlign: TextAlign.center,
  158. style: TextStyle(textBaseline: TextBaseline.alphabetic, fontSize: 15),
  159. maxLines: 4,
  160. minLines: 1,
  161. textInputAction: TextInputAction.done,
  162. inputFormatters: <TextInputFormatter>[
  163. LengthLimitingTextInputFormatter(30) //限制长度
  164. ],
  165. onChanged: (String text) {
  166. curDesc = text;
  167. },
  168. decoration: InputDecoration.collapsed(
  169. hintText: curDesc,
  170. hintStyle:
  171. TextStyle(fontSize: 15, color: Constants.LightGreyTextColor)),
  172. ),
  173. );
  174. }
  175. _buildSendBtn() {
  176. return SizedBox(
  177. width: Screen.width * 2 / 3,
  178. child: RaisedButton(
  179. child: Text(I18n.of(context).put_money,
  180. textAlign: TextAlign.center,
  181. style: TextStyle(color: Colors.white, fontSize: 20)),
  182. color: Color(0xFFdd643e),
  183. padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 12),
  184. disabledColor: Color(0xFFdd643e).withAlpha(160),
  185. shape: RoundedRectangleBorder(
  186. borderRadius: BorderRadius.all(Radius.circular(8))),
  187. onPressed: curAmount > 0 ? _sendCoin : null,
  188. ));
  189. }
  190. _sendCoin() {
  191. if (curDesc == '') {
  192. curDesc = I18n.of(context).little;
  193. }
  194. if (curAmount <= 0) {
  195. showToast(I18n.of(context).enter_amount);
  196. return;
  197. } else {
  198. if (curAmount < 10) {
  199. showToast(
  200. I18n.of(context).little_min.replaceFirst('/s1', 10.toString()));
  201. return;
  202. }
  203. if (curAmount > 999) {
  204. showToast(
  205. I18n.of(context).more_big.replaceFirst('/s1', 999.toString()));
  206. return;
  207. }
  208. }
  209. int curCoin = Provider.of<MoneyChangeProvider>(context).money;
  210. if (curCoin >= curAmount) {
  211. Provider.of<MoneyChangeProvider>(context).subMoney(curAmount);
  212. MessageMgr()
  213. .emit('Send CoinBag', {'amount': curAmount, 'title': curDesc});
  214. curAmount = 0;
  215. Navigator.pop(context);
  216. } else {
  217. showToast(I18n.of(context).not_enough);
  218. }
  219. }
  220. }