import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutterappfuyou/code/YSChildBody.dart';
import 'package:flutterappfuyou/code/YSDiary.dart';
import 'package:flutterappfuyou/code/YSWomanBody.dart';
import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
import 'package:flutterappfuyou/code/version2/YSUrlPage.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'base/YSBase.dart';

class YSMore extends StatefulWidget {
  @override
  _YSMoreState createState() => _YSMoreState();
}

class _YSMoreState extends State<YSMore> {
  List _dataArray = [];
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      this._getAllFunctionData();
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '百宝箱',
      yschild: Column(
        children: [
          Container(
            height: 50,
            padding: EdgeInsets.all(15),
            alignment: Alignment.centerLeft,
            child: Text('篇章工具',style: TextStyle(fontSize: 14,color: Color(0xFF7F3344),decoration: TextDecoration.none),),
          ),
          Container(
            height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-100,
            child: GridView.builder(
              padding: EdgeInsets.all(0),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4,mainAxisSpacing: 10,crossAxisSpacing: 10),
              itemBuilder: (context,index){
                return GestureDetector(
                  onTap: (){
                    Navigator.of(context).push(
                      CupertinoPageRoute(
                        builder: (context){
                          if(_dataArray[index]['value']=='PregnancyDiary'){
                            return YSDiary();
                          }else if(_dataArray[index]['value']=='ChildDiary'){
                            return YSDiary();
                          }else if(_dataArray[index]['value']=='MotherhoodDiary'){
                            return YSDiary();
                          }else if(_dataArray[index]['value']=='PregnancyBody'){
                            return YSWomanBody();
                          }else if(_dataArray[index]['value']=='ChildBody'){
                            return YSChildBody();
                          }else if('${_dataArray[index]['value']}'.contains('http')==true){
                            return YSUrlPage(url: _dataArray[index]['value'],title: _dataArray[index]['name']);
                          }else{
                            return YSDiary();
                          }

                        }
                      )
                    );
                  },
                  child: Container(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Image(image:NetworkImage('${_dataArray[index]['img']}'),height: 36,width: 36,),
                        Text('${_dataArray[index]['name']}',style: TextStyle(color: Color(0xFF7F3344),fontSize: 11,decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                      ],
                    ),
                  ),
                );
              },
              itemCount: _dataArray.length,
            ),
          )
        ],
      ),
    );
  }

  _getAllFunctionData() async{
    SharedPreferences prefer = await SharedPreferences.getInstance();
    int chapter = prefer.getInt('chapters')+1;
    Map request = Map();
    request['category_id'] = chapter;
    request['type'] = 'ios';
    if(chapter>2)request['child_id'] = prefer.getInt('childId');
    var dict = await ysRequestHttpNoLoading(context, requestType.post, 'tool/list', request);
    if(dict!=null){
      setState(() {
        _dataArray = dict;
      });
    }

  }
}
