YSChapters.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutterappfuyou/YSPage.dart';
  4. import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import 'YSAddChapter.dart';
  7. import 'YSDueDate.dart';
  8. import 'base/YSBase.dart';
  9. import 'base/YSTools.dart';
  10. class YSChapters extends StatefulWidget {
  11. final bool isFirst;
  12. const YSChapters({Key key, this.isFirst = false}) : super(key: key);
  13. @override
  14. _YSChaptersState createState() => _YSChaptersState();
  15. }
  16. class _YSChaptersState extends State<YSChapters> {
  17. List _dataArray = [];
  18. String _expectedTime = '';
  19. @override
  20. void initState() {
  21. Future.delayed(Duration(seconds: 0)).then((e){
  22. _getChapterData();
  23. getVersionData(context);
  24. });
  25. super.initState();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return YSBase(
  30. ystitle: '选择篇章',
  31. isBack: widget.isFirst==false,
  32. yschild: ListView.separated(itemBuilder: (context,index){
  33. return GestureDetector(
  34. behavior: HitTestBehavior.opaque,
  35. onTap: (){
  36. if(index<_dataArray.length+2){
  37. int chapters = index==0?1:index==1?2:3;
  38. bool sex = false;
  39. int childId = 0;
  40. if(index==1&&_expectedTime.isEmpty){
  41. Navigator.of(context).push(
  42. CupertinoPageRoute(
  43. builder: (context){
  44. return YSDueDate(dueDate: _expectedTime,);
  45. }
  46. )
  47. ).then((value) {
  48. if(value!=null){
  49. Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  50. _prefs.then((value) => {
  51. value.setInt('chapters', chapters-1),
  52. value.setInt('childId', childId),
  53. });
  54. if(widget.isFirst){
  55. Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
  56. builder: (context){
  57. return YSPage();
  58. }
  59. ), (route) => false);
  60. }else{
  61. Navigator.of(context).pop({'chapters':chapters,'sex':sex,'child':childId});
  62. }
  63. }
  64. });
  65. return;
  66. }
  67. if(index>1){
  68. sex = _dataArray[index-2]['gender']=='男'?true:false;
  69. childId = _dataArray[index-2]['id'];
  70. }
  71. Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  72. _prefs.then((value) => {
  73. value.setInt('chapters', chapters-1),
  74. value.setInt('childId', childId),
  75. });
  76. if(widget.isFirst){
  77. Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
  78. builder: (context){
  79. return YSPage();
  80. }
  81. ), (route) => false);
  82. }else{
  83. Navigator.of(context).pop({'chapters':chapters,'sex':sex,'child':childId});
  84. }
  85. }else{
  86. Navigator.of(context).push(
  87. CupertinoPageRoute(
  88. builder: (context){
  89. return YSAddChapter();
  90. }
  91. )
  92. ).then((value){
  93. if(value=='完成'){
  94. _getChapterData();
  95. }
  96. });
  97. }
  98. },
  99. onLongPress: (){
  100. if(index>1 && index<_dataArray.length+2){
  101. showDialog(
  102. context:context,
  103. builder:(context){
  104. return AlertDialog(
  105. title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  106. content: Text('确认删除此篇章?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  107. actions: [
  108. CupertinoButton(
  109. padding: EdgeInsets.all(0),
  110. child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  111. onPressed: (){
  112. Navigator.pop(context);
  113. },
  114. ),
  115. CupertinoButton(
  116. padding: EdgeInsets.all(0),
  117. child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  118. onPressed: (){
  119. Navigator.pop(context);
  120. _deleteChapter(index);
  121. },
  122. ),
  123. ],
  124. );
  125. }
  126. );
  127. }
  128. },
  129. child: Container(
  130. height: 123,
  131. margin: EdgeInsets.only(left: 75,right: 75),
  132. width: MediaQuery.of(context).size.width-150,
  133. decoration: BoxDecoration(
  134. color: Color(0xFFFAF8F5),
  135. borderRadius: BorderRadius.all(Radius.circular(20))
  136. ),
  137. child: index<_dataArray.length+2?Stack(
  138. children: [
  139. Image.asset(index==0?'lib/images/beiyunchapter.png':index==1?'lib/images/yunchanchapter.png':
  140. _dataArray[index-2]['gender']==1?'lib/images/childchapter.png':'lib/images/childchapter2.png'),
  141. Container(
  142. height: 123,
  143. width: MediaQuery.of(context).size.width-150,
  144. padding: EdgeInsets.only(left: 15,right: 15,top: 20,bottom: 20),
  145. child: index>1?Column(
  146. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  147. crossAxisAlignment: CrossAxisAlignment.start,
  148. children: [
  149. Text('宝宝姓名:${_dataArray[index-2]['name']}',style: TextStyle(fontSize: 14,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  150. Text('性别:${_dataArray[index-2]['gender']==1?'男':'女'}',style: TextStyle(fontSize: 12,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  151. maxLines: 2,overflow: TextOverflow.ellipsis,),
  152. Text('出生日期:${_dataArray[index-2]['birthday']}',style: TextStyle(fontSize: 10,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  153. maxLines: 2,overflow: TextOverflow.ellipsis,),
  154. ],
  155. ):Container(
  156. padding: EdgeInsets.only(top: 10),
  157. child: Text(index==0?'备孕篇':'孕产篇',style: TextStyle(fontSize: 14,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
  158. ),
  159. )
  160. ],
  161. ):Container(
  162. height: 123,
  163. width: MediaQuery.of(context).size.width-150,
  164. padding: EdgeInsets.only(left: 15,right: 15,top: 20,bottom: 20),
  165. decoration: BoxDecoration(
  166. color: Colors.white,
  167. borderRadius: BorderRadius.all(Radius.circular(20)),
  168. border: Border.all(color: Color(0xFFE6E4E1),width: 1)
  169. ),
  170. child: Column(
  171. mainAxisAlignment: MainAxisAlignment.spaceAround,
  172. children: [
  173. Image(
  174. width: 63,
  175. height: 43,
  176. image: AssetImage('lib/images/addchapter.png'),
  177. ),
  178. Text('新增儿童',style: TextStyle(fontSize: 13,color: Color(0xFFBCBCBC),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  179. ],
  180. ),
  181. ),
  182. ),
  183. );
  184. }, separatorBuilder: (context,index){
  185. return Divider(color: Colors.transparent,height: 15,thickness: 15,);
  186. },
  187. itemCount: _dataArray.length+3,
  188. padding: EdgeInsets.only(top: 15,bottom: 15),
  189. ),
  190. );
  191. }
  192. _getChapterData() async{
  193. Map dict = await ysRequestHttp(context,requestType.get, 'chapter/list', {});
  194. if(dict!=null){
  195. _expectedTime = dict['data']['pregnancy']['expected_time']??'';
  196. setState(() {
  197. _dataArray = dict['data']['child'];
  198. });
  199. }
  200. }
  201. _deleteChapter(int index) async{
  202. Map dict = await ysRequestHttp(context,requestType.delete, 'child/chapter/delete', {'child_id':_dataArray[index-2]['id']});
  203. if(dict!=null){
  204. setState(() {
  205. _dataArray.removeAt(index-2);
  206. });
  207. }
  208. }
  209. }