import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
import 'package:flutterappfuyou/code/base/YSTools.dart';
import 'package:flutterappfuyou/code/version2/view/YSWikiListView.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'base/YSBase.dart';

class YSInformation extends StatefulWidget {
  @override
  _YSInformationState createState() => _YSInformationState();
}

class _YSInformationState extends State<YSInformation> {
  ScrollController _scroll = ScrollController();
  TextEditingController _search = TextEditingController();
  int _index = 1;
  List _dataArray = [];
  @override
  void initState() {
    Future.delayed(Duration(seconds: 0)).then((value){
      this._refreshData();
      _scroll.addListener(() {
        if(_scroll.position.pixels == _scroll.position.maxScrollExtent){
          this._loadMoreData();
        }
      });
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ystitle: '育儿百科',
      yschild: SingleChildScrollView(
        child: Column(
          children: [
            // Container(
            //   height: 50,
            //   decoration: BoxDecoration(
            //     color: Colors.white,
            //     borderRadius: BorderRadius.only(topRight: Radius.circular(20),topLeft: Radius.circular(20)),
            //   ),
            //   padding: EdgeInsets.only(left: 20,right: 20,top: 10,bottom: 10),
            //   child: CupertinoTextField(
            //     placeholder: '搜索',
            //     textAlign: TextAlign.center,
            //     style: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none),
            //     placeholderStyle: TextStyle(fontSize: 14,color: Color(0xFF808080),decoration: TextDecoration.none),
            //     decoration: BoxDecoration(
            //         borderRadius: BorderRadius.all(Radius.circular(15)),
            //         color: Color(0xFFF5F3F0)
            //     ),
            //     controller: _search,
            //     textInputAction: TextInputAction.search,
            //     onSubmitted: (value){
            //       _refreshData();
            //     },
            //   ),
            // ),
            Container(
              color: Colors.white,
              height: MediaQuery.of(context).size.height-75,
              child: RefreshIndicator(
                onRefresh: _refreshData,
                child: _dataArray.length==0?Container(
                  alignment: Alignment.center,
                  child: Image.asset('lib/images/none.png',height: 200,width: 200,),
                ):ListView.separated(
                  shrinkWrap: true,
                  itemBuilder: (context,index){
                    return GestureDetector(
                      onTap: (){},
                      behavior: HitTestBehavior.opaque,
                      child: YSWikiListView(
                        wikiItem: _dataArray[index],
                        callback: (){
                          _refreshData();
                        },
                      ),
                    );

                  },
                  separatorBuilder: (context,index){
                    return Divider(height: 0.5,thickness: 0.5,color: Color(0xFFE6E1E1),);
                  },
                  itemCount: _dataArray.length,
                  physics: AlwaysScrollableScrollPhysics(),
                  padding: EdgeInsets.only(left: 15,right: 15),
                  controller: _scroll,
                ),
              ),
            ),
          ],
        ),
        physics: NeverScrollableScrollPhysics(),
      ),
    );
  }

  Future<void> _refreshData() async{
    SharedPreferences prefer = await SharedPreferences.getInstance();
    Map request = Map();
    _index = 1;
    request['page'] = _index;
    request['title'] = _search.text??'';
    request['category_id'] = prefer.getInt('chapters')+1;
    Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'wike/list', request);
    if(dict!=null){
      setState(() {
        _dataArray = dict['data'];
      });
    }
  }

  Future<void> _loadMoreData() async{
    SharedPreferences prefer = await SharedPreferences.getInstance();
    Map request = Map();
    _index++;
    request['page'] = _index;
    request['title'] = _search.text??'';
    request['category_id'] = prefer.getInt('chapters')+1;
    Map dict = await ysRequestHttp(context, requestType.get, 'wike/list', request);
    if(dict!=null){
      setState(() {
        _dataArray.addAll(dict['data']);
      });
    }
  }

}
