recharge.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import 'package:flutter/material.dart';
  2. import '../styles/colors.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'dart:ui';
  5. class Rechrage extends StatefulWidget {
  6. @override
  7. RechrageState createState() => RechrageState();
  8. }
  9. class RechrageState extends State<Rechrage> {
  10. List<Map> moenyList = [
  11. {
  12. 'title': '10金币',
  13. 'money': 10,
  14. },
  15. {
  16. 'title': '50金币',
  17. 'money': 50,
  18. },
  19. {
  20. 'title': '100金币',
  21. 'money': 100,
  22. },
  23. {
  24. 'title': '300金币',
  25. 'money': 300,
  26. },
  27. {
  28. 'title': '500金币',
  29. 'money': 500,
  30. }
  31. ];
  32. bool isInput = false;
  33. int choosemoeny = 0;
  34. bool autoChoose = false;
  35. FocusNode _focusNode = FocusNode();
  36. @override
  37. void initState() {
  38. super.initState();
  39. _focusNode.addListener(_focusNodeListener);
  40. }
  41. Future<Null> _focusNodeListener() async {
  42. if (_focusNode.hasFocus) {
  43. setState(() {
  44. if (!autoChoose) {
  45. autoChoose = true;
  46. choosemoeny = 0;
  47. }
  48. isInput = true;
  49. });
  50. } else {
  51. setState(() {
  52. isInput = false;
  53. });
  54. }
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
  59. return Scaffold(
  60. appBar: AppBar(
  61. backgroundColor: PRIMARY_COLOR,
  62. title: Text('充值'),
  63. centerTitle: true,
  64. elevation: 0,
  65. ),
  66. body: GestureDetector(
  67. ///透明也响应处理
  68. behavior: HitTestBehavior.translucent,
  69. onTap: () {
  70. ///触摸手气键盘
  71. FocusScope.of(context).requestFocus(FocusNode());
  72. },
  73. child: RefreshIndicator(
  74. color: PRIMARY_COLOR,
  75. backgroundColor: Colors.white,
  76. onRefresh: () async {
  77. await Future.delayed(const Duration(seconds: 1));
  78. },
  79. child: Container(
  80. color: BG_COLOR,
  81. child: Column(
  82. children: <Widget>[_useGride(context)],
  83. ),
  84. ),
  85. )));
  86. }
  87. Widget _useGride(BuildContext context) {
  88. return Container(
  89. padding: EdgeInsets.all(ScreenUtil().setWidth(15)),
  90. width: double.infinity,
  91. child: Wrap(
  92. alignment: WrapAlignment.spaceBetween,
  93. children: _allChooseBtn(context),
  94. ),
  95. );
  96. }
  97. List<Widget> _allChooseBtn(BuildContext context) {
  98. List<Widget> allWidget =
  99. List<Widget>.generate(moenyList.length, (int index) {
  100. return _chooseBtn(context, index);
  101. });
  102. Widget _input() {
  103. return Container(
  104. padding: EdgeInsets.only(left: 30, right: 5),
  105. color: autoChoose ? PRIMARY_COLOR : BG_SUB_COLOR,
  106. margin: EdgeInsets.only(bottom: ScreenUtil().setWidth(5)),
  107. width: ScreenUtil().setWidth(170),
  108. height: ScreenUtil().setWidth(60),
  109. child: TextField(
  110. focusNode: _focusNode,
  111. textAlign: TextAlign.center,
  112. keyboardType: TextInputType.number,
  113. style: TextStyle(
  114. fontSize: 16,
  115. fontWeight: FontWeight.w500,
  116. color: Colors.white,
  117. ),
  118. decoration: InputDecoration(
  119. hintText: isInput ? '请输入金额' : '其他金额',
  120. hintStyle: TextStyle(
  121. color: Colors.white,
  122. fontSize: 14,
  123. fontWeight: isInput?FontWeight.w500:FontWeight.w400),
  124. suffixText: '金币',
  125. suffixStyle: TextStyle(
  126. fontSize: 13,
  127. fontWeight: FontWeight.w400,
  128. color: Colors.white),
  129. border: OutlineInputBorder(borderSide: BorderSide.none),
  130. ),
  131. onChanged: (String str) {
  132. //输入监听
  133. choosemoeny = int.parse(str);
  134. }));
  135. }
  136. allWidget.add(_input());
  137. return allWidget;
  138. }
  139. Widget _chooseBtn(BuildContext context, int index) {
  140. bool isChoose = false;
  141. if (!autoChoose && moenyList[index]['money'] == choosemoeny) {
  142. isChoose = true;
  143. }
  144. return Container(
  145. margin: EdgeInsets.only(bottom: ScreenUtil().setWidth(5)),
  146. width: ScreenUtil().setWidth(170),
  147. height: ScreenUtil().setWidth(60),
  148. child: FlatButton(
  149. color: isChoose ? PRIMARY_COLOR : BG_SUB_COLOR,
  150. highlightColor: isChoose
  151. ? PRIMARY_COLOR.withOpacity(0.3)
  152. : BG_COLOR.withOpacity(0.3),
  153. child: Column(
  154. mainAxisAlignment: MainAxisAlignment.center,
  155. children: <Widget>[
  156. Text(moenyList[index]['title'],
  157. style: TextStyle(
  158. color: Colors.white,
  159. fontSize: 14,
  160. fontWeight: FontWeight.w500,
  161. height: 1)),
  162. Text('¥' + moenyList[index]['money'].toString(),
  163. style: TextStyle(
  164. color: isChoose ? Colors.white : Color(0xFF727785),
  165. fontSize: 13,
  166. fontWeight: FontWeight.w400,
  167. height: 18 / 13)),
  168. ],
  169. ),
  170. onPressed: () {
  171. setState(() {
  172. autoChoose = false;
  173. choosemoeny = moenyList[index]['money'];
  174. FocusScope.of(context).requestFocus(FocusNode());
  175. });
  176. },
  177. ),
  178. );
  179. }
  180. }