import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutterappfuyou/code/base/YSNetWorking.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'YSAddDiary.dart'; import 'YSPhoto.dart'; class YSDiary extends StatefulWidget { @override _YSDiaryState createState() => _YSDiaryState(); } String timeSwitch(String timeStr){ DateTime time = DateTime.parse(timeStr); return "${time.day.toString().padLeft(2,'0')}${time.month.toString().padLeft(2,'0')}月"; } class _YSDiaryState extends State { List dataArray = []; int _page = 1; ScrollController _scroll = ScrollController(); String _name = ''; String _avatar = ''; @override void initState() { Future.delayed(Duration(seconds: 0)).then((value){ this._refreshData(); _scroll.addListener(() { if(_scroll.position.pixels == _scroll.position.maxScrollExtent){ this._loadMoreData(); } }); }); Future prefer = SharedPreferences.getInstance(); prefer.then((value){ _name = value.getString('name')??''; _avatar = value.getString('avatar')??''; }); super.initState(); } @override Widget build(BuildContext context) { return CupertinoPageScaffold( backgroundColor: Colors.white, child: Stack( children: [ Container( height: 191.5, width: MediaQuery.of(context).size.width, child: Image( image: AssetImage('lib/images/diarybg.png'), fit: BoxFit.fill, ), ), CupertinoButton( padding: EdgeInsets.all(0), child: Container( margin: EdgeInsets.only(left: 15,top: MediaQuery.of(context).padding.top+5), child: Icon(Icons.keyboard_arrow_left,size: 30,color: Colors.white,), ), onPressed: (){ Navigator.pop(context); }, ), Container( margin: EdgeInsets.only(top: 170), height: MediaQuery.of(context).size.height-170, decoration: BoxDecoration( borderRadius: BorderRadius.only(topLeft: Radius.circular(20),topRight: Radius.circular(20)), color: Colors.white, ), child: Column( children: [ Container( height: 70, margin: EdgeInsets.only(top: 80,bottom: 20), padding: EdgeInsets.only(left: 20,right: 20), child: Row( children: [ Text('今天',style: TextStyle(fontSize: 26,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600),), CupertinoButton( padding: EdgeInsets.all(0), child: Container( margin: EdgeInsets.only(left: 20), height: 70, width: 70, color: Color(0xFFEFEFEF), child: Icon(Icons.photo_camera,size: 35,color: Color(0xFF8A8A8A),), ), onPressed: (){ Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return YSAddDiary(); } ) ).then((value){ if(value!=null){ _refreshData(); } }); }, ) ], ), ), Container( height: MediaQuery.of(context).size.height-340, child: RefreshIndicator( onRefresh: _refreshData, child: dataArray.length==0?Container( alignment: Alignment.center, child: Image.asset('lib/images/none.png',height: 200,width: 200,), ):ListView.builder(itemBuilder: (context,index){ return Container( padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ RichText( text: TextSpan( text: timeSwitch(dataArray[index]['created_at']).substring(0,2), style: TextStyle(fontSize: 26,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.w600), children: [ TextSpan( text: ' '+timeSwitch(dataArray[index]['created_at']).substring(2,timeSwitch(dataArray[index]['created_at']).length), style: TextStyle(fontSize: 13), ) ] ), ), Container( width: MediaQuery.of(context).size.width-150, padding: EdgeInsets.only(left: 15,right: 15,top: 5), child: Column( children: [ dataArray[index]['body']!=null?Text(dataArray[index]['body'], style: TextStyle(fontSize: 12,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(), Container( margin: EdgeInsets.only(top: dataArray[index]['body']!=null?5:0), height: dataArray[index]['covers'].length==0?0:dataArray[index]['covers'].length%3!=0?70.0*(((dataArray[index]['covers'].length)~/3)+1):70.0*((dataArray[index]['covers'].length)~/3), width: MediaQuery.of(context).size.width-150, child: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, ), itemBuilder: (context,indexSub){ return GestureDetector( onTap: (){ Navigator.of(context).push( CupertinoPageRoute(builder: (context){ return YSPhotoDetail(url: dataArray[index]['covers'][indexSub],); }) ); }, child: Container( height: 70, width: 70, margin: EdgeInsets.only(right: 5,top: 5,bottom: 5), color: Color(0xFFEFEFEF), child: Image(image: NetworkImage('${dataArray[index]['covers'][indexSub]}'),fit: BoxFit.fill,) ), ); }, itemCount: dataArray[index]['covers'].length, physics: NeverScrollableScrollPhysics(), padding: EdgeInsets.all(0), ), ), Container( child: Row( children: [ dataArray[index]['tag']!=null?Text('#'+dataArray[index]['tag']+'#', style: TextStyle(fontSize: 12,color: Color(0xFFDB5278),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(), dataArray[index]['position']!=null? Text('${dataArray[index]['position']}',style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),):Container(), ], ), ) ], crossAxisAlignment: CrossAxisAlignment.start, ), ), GestureDetector( child: Container( height: 20, width: 20, child: Image.asset('lib/images/off2.png'), ), onTap: (){ showDialog( context:context, builder:(context){ return AlertDialog( title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), content: Text('确认删除此条记录?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), actions: [ CupertinoButton( padding: EdgeInsets.all(0), child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), onPressed: (){ Navigator.pop(context); }, ), CupertinoButton( padding: EdgeInsets.all(0), child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), onPressed: (){ Navigator.pop(context); _deleteDiaryData(index); }, ), ], ); } ); }, ) ], ), ); },itemCount: dataArray.length,padding: EdgeInsets.all(0),controller: _scroll,physics: AlwaysScrollableScrollPhysics(),), ), ) ], ), ), Container( margin: EdgeInsets.only(top: 130,left: MediaQuery.of(context).size.width-80), height: 70, width: 70, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(35)), border: Border.all(color: Colors.white,width: 2), image: DecorationImage(image: _avatar==''?AssetImage('lib/images/avtar.png'):NetworkImage(_avatar),fit: BoxFit.fill) ), ), Container( margin: EdgeInsets.only(top: 205,left: 15,right: 15), width: MediaQuery.of(context).size.width-30, height: 20, alignment: Alignment.centerRight, child: Text(_name,style: TextStyle(fontSize: 14,color: Color(0xFF292929),fontWeight: FontWeight.normal,decoration: TextDecoration.none),overflow: TextOverflow.ellipsis,), ) ], ), ); } Future _refreshData() async{ SharedPreferences prefer = await SharedPreferences.getInstance(); int chapter = prefer.getInt('chapters')??0; _page = 1; Map request = Map(); request['category_id'] = chapter+1; request['page'] = _page; if(chapter>1){ request['child_id'] = prefer.getInt('childId'); } Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'diary/diary', request); if(dict!=null){ setState(() { dataArray = dict['data']['datas']??[]; }); } } Future _loadMoreData() async{ SharedPreferences prefer = await SharedPreferences.getInstance(); int chapter = prefer.getInt('chapters')??0; _page++; Map request = Map(); request['category_id'] = 1; request['page'] = _page; if(chapter>1){ request['child_id'] = prefer.getInt('childId'); } Map dict = await ysRequestHttp(context, requestType.post, 'diary/diary', request); if(dict!=null){ setState(() { dataArray.addAll(dict['data']['datas']??[]); }); } } _deleteDiaryData(int index) async{ Map dict = await ysRequestHttp(context, requestType.delete, 'diary/delete', {'id':dataArray[index]['id']}); if(dict!=null){ setState(() { dataArray.removeAt(index); }); } } }