YSAddChapter.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'base/YSBase.dart';
  5. import 'base/YSTools.dart';
  6. import 'base/YSNetWorking.dart';
  7. class YSAddChapter extends StatefulWidget {
  8. final bool isEdit;
  9. const YSAddChapter({Key key, this.isEdit}) : super(key: key);
  10. @override
  11. _YSAddChapterState createState() => _YSAddChapterState();
  12. }
  13. class _YSAddChapterState extends State<YSAddChapter> {
  14. int childId = 0;
  15. TextEditingController _name = TextEditingController();
  16. TextEditingController _number = TextEditingController();
  17. String birthday='';
  18. String sex='';
  19. @override
  20. void initState() {
  21. Future.delayed(Duration(seconds: 0)).then((value){
  22. if(widget.isEdit==true){
  23. _getChildData();
  24. }
  25. });
  26. super.initState();
  27. }
  28. @override
  29. void dispose() {
  30. _number.dispose();
  31. _name.dispose();
  32. super.dispose();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return YSBase(
  37. ystitle: '宝宝出生了',
  38. yschild: SingleChildScrollView(
  39. child: Column(
  40. crossAxisAlignment: CrossAxisAlignment.start,
  41. children: [
  42. Container(
  43. height: 400,
  44. child: ListView.separated(
  45. itemBuilder: (context,index){
  46. return Container(
  47. padding: EdgeInsets.only(left: 15,right: 15),
  48. height: 65,
  49. child: Column(
  50. crossAxisAlignment: CrossAxisAlignment.start,
  51. children: [
  52. Text(index==0?' 宝宝姓名':index==1?' 宝宝生日':index==2?' 宝宝性别':' 保健册号',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  53. Container(
  54. height: 40,
  55. margin: EdgeInsets.only(top: 8),
  56. padding: EdgeInsets.only(left: 15,right: 15),
  57. decoration: BoxDecoration(
  58. borderRadius: BorderRadius.all(Radius.circular(20)),
  59. border: Border.all(color: Color(0xFFD7D7D7),width: 0.5)
  60. ),
  61. child: index==0||index==3?CupertinoTextField(
  62. placeholder: '请输入${index==0?'宝宝姓名':'保健册号'}',
  63. style: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  64. decoration: BoxDecoration(),
  65. placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  66. controller: index==0?_name:_number,
  67. ):CupertinoButton(
  68. padding: EdgeInsets.all(0),
  69. child: Container(
  70. width: MediaQuery.of(context).size.width-30,
  71. child: Text(index==1?birthday.isEmpty?'请选择宝宝生日':birthday:sex.isEmpty?'请选择宝宝性别':sex,
  72. style: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  73. ),
  74. onPressed: (){
  75. if(index==1){
  76. ysDatePicker(context, (value){
  77. setState(() {
  78. birthday = value;
  79. });
  80. });
  81. // showModalBottomSheet(context: context,builder: (context){
  82. // return YSDatePicker(
  83. // choose: (value){
  84. // setState(() {
  85. // birthday = value;
  86. // });
  87. // },
  88. // );
  89. // },backgroundColor: Colors.transparent);
  90. }else{
  91. showDialog(context: context, builder: (context){
  92. return Dialog(
  93. backgroundColor: Colors.transparent,
  94. child: Container(
  95. height: 150,
  96. decoration: BoxDecoration(
  97. borderRadius: BorderRadius.all(Radius.circular(10)),
  98. color: Colors.white,
  99. ),
  100. child: Column(
  101. children: [
  102. Container(
  103. height: 50,
  104. alignment: Alignment.centerLeft,
  105. padding: EdgeInsets.only(left: 20,right: 20),
  106. child: Text('选择性别',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600)),
  107. ),
  108. CupertinoButton(
  109. padding: EdgeInsets.all(0),
  110. child: Container(
  111. height: 50,
  112. alignment: Alignment.centerLeft,
  113. padding: EdgeInsets.only(left: 20,right: 20),
  114. child: Text('男',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  115. ),
  116. onPressed: (){
  117. Navigator.pop(context);
  118. setState(() {
  119. sex = '男';
  120. });
  121. },
  122. ),
  123. CupertinoButton(
  124. padding: EdgeInsets.all(0),
  125. child: Container(
  126. height: 50,
  127. alignment: Alignment.centerLeft,
  128. padding: EdgeInsets.only(left: 20,right: 20),
  129. child: Text('女',style: TextStyle(fontSize: 14,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  130. ),
  131. onPressed: (){
  132. Navigator.pop(context);
  133. setState(() {
  134. sex = '女';
  135. });
  136. },
  137. )
  138. ],
  139. ),
  140. ),
  141. );
  142. });
  143. }
  144. },
  145. ),
  146. )
  147. ],
  148. ),
  149. );
  150. },
  151. separatorBuilder: (context,index){
  152. return Divider(height: 20,thickness: 20,color: Colors.transparent,);
  153. },
  154. itemCount: 4,
  155. padding: EdgeInsets.only(top: 20),
  156. physics: NeverScrollableScrollPhysics(),
  157. ),
  158. ),
  159. CupertinoButton(
  160. padding: EdgeInsets.all(0),
  161. child: Container(
  162. height: 40,
  163. margin: EdgeInsets.only(left: 75),
  164. width: MediaQuery.of(context).size.width-150,
  165. decoration: BoxDecoration(
  166. color: Color(0xFFDB5278),
  167. borderRadius: BorderRadius.all(Radius.circular(20))
  168. ),
  169. child: Center(child: Text('保存',style: TextStyle(fontSize: 16,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),)),
  170. ),
  171. onPressed: (){
  172. _addChapterData();
  173. },
  174. )
  175. ],
  176. ),
  177. ),
  178. );
  179. }
  180. _addChapterData() async{
  181. if(_name.text.isEmpty){
  182. ysFlutterToast(context, '请输入宝宝姓名');
  183. return;
  184. }
  185. if(birthday.isEmpty){
  186. ysFlutterToast(context, '请选择宝宝生日');
  187. return;
  188. }
  189. if(sex.isEmpty){
  190. ysFlutterToast(context, '请选择宝宝性别');
  191. return;
  192. }
  193. if(_number.text.isEmpty){
  194. ysFlutterToast(context, '请输入保健册号');
  195. return;
  196. }
  197. DateTime now = DateTime.now();
  198. DateTime expected = DateTime.parse('$birthday ${now.hour.toString().padLeft(2,'0')}:${now.minute.toString().padLeft(2,'0')}:${now.second.toString().padLeft(2,'0')}');
  199. bool isBefore = now.isBefore(expected);
  200. if(isBefore){
  201. ysFlutterToast(context, '宝宝生日不能大于当前日期');
  202. return;
  203. }
  204. Map pragma = Map();
  205. if(childId!=0){
  206. pragma['child_id'] = childId;
  207. }
  208. pragma['name'] = _name.text;
  209. pragma['health_book'] = _number.text;
  210. pragma['birthday'] = birthday;
  211. if(sex=='男'){
  212. pragma['gender'] = 1;
  213. }else{
  214. pragma['gender'] = 2;
  215. }
  216. Map dict = await ysRequestHttp(context,requestType.post, 'child/chapter/addOrUp', pragma);
  217. if(dict!=null){
  218. Navigator.of(context).pop('完成');
  219. }
  220. }
  221. _getChildData() async{
  222. SharedPreferences prefer = await SharedPreferences.getInstance();
  223. Map request = Map();
  224. request['category_id'] = prefer.getInt('chapters')+1;
  225. request['child_id'] = prefer.getInt('childId');
  226. Map dict = await ysRequestHttp(context, requestType.get, 'chapter/info', request);
  227. if(dict!=null){
  228. childId = dict['data']['id'];
  229. setState(() {
  230. _name.text = dict['data']['name'];
  231. _number.text = dict['data']['health_book'];
  232. birthday = dict['data']['birthday'];
  233. sex = dict['data']['gender']==2?'女':'男';
  234. });
  235. }
  236. }
  237. }