import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'Car.dart';

class List_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ListPageState();
}

EasyRefreshController _controller = EasyRefreshController();

class _ListPageState extends State<List_Page> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("ListView"),
        centerTitle: true,
        //leading 返回按键
        //GestureDetector 用于事件触发
        leading: GestureDetector(
          onTap: () {
            //当前页面出栈
            Navigator.pop(context);
          },
          //左上角返回的小箭头
          child: Icon(Icons.arrow_back_ios),
        ),
      ),
      body: EasyRefresh(
        //下拉刷新头布局、可以自定义
        header: TaurusHeader(),
        //上拉加载尾布局，可以自定义
        footer: TaurusFooter(),
        enableControlFinishRefresh: true,
        enableControlFinishLoad: true,
        //刷新和加载的控制器，用于控制刷新和加载
        controller: _controller,
        child: ListView.builder(
          //返回数据条数
          itemCount: datas.length,
          //itemBuilder 需要返回一个Widget
          itemBuilder: (context,index){
            return _getDatas(index);
          },
        ),
        //刷新动作被触发
        onRefresh: () async {
          refreshData();
        },
        //加载动作被触发
        onLoad: () async {
          loadData();
        },
      ),
    );
  }

  //定义一个全局的变量：模型数组
  List<Car> datas = [
    Car(
      name: '保时捷918 Spyder',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-7d8be6ebc4c7c95b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '兰博基尼Aventador',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-e3bfd824f30afaac?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '法拉利Enzo',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-a1d64cf5da2d9d99?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: 'Zenvo ST1',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-bf883b46690f93ce?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '迈凯伦F1',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-5a7b5550a19b8342?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '萨林S7',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-2e128d18144ad5b8?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '科尼赛克CCR',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-01ced8f6f95219ec?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '布加迪Chiron',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-7fc8359eb61adac0?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '轩尼诗Venom GT',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-d332bf510d61bbc2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    ),
    Car(
      name: '西贝尔Tuatara',
      imageUrl:
      'https://upload-images.jianshu.io/upload_images/2990730-3dd9a70b25ae6bc9?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240',
    )
  ];


  ///获取item视图
  Widget _getDatas(int position) {
    return Container(
        child: GestureDetector(
      child: Card(
        elevation: 6,
        shadowColor: Colors.redAccent,
        margin: EdgeInsets.fromLTRB(16, 20, 16, 20),
        child: Column(
          children: [
            SizedBox(height: 20),
            Text(datas[position].name),
            SizedBox(height: 10),
            Image.network(
              datas[position].imageUrl,
              width: 340,
              height: 200,
            ),
            SizedBox(height: 20),
          ],
        ),
      ),
      onTap: () {
        var data = datas[position];
        Navigator.pushNamed(context, "car_details", arguments: data);
      },
    ));
  }

  ///刷新数据
  Future<void> refreshData() async {
    await Future.delayed(Duration(seconds: 2), () {
      setState(() {
        for (int i = 0; i < datas.length; i++) {
          datas[i].name = "刷新后的数据$i";
        }
        _controller.finishRefresh(success: true);
      });
    });
  }

  ///加载数据
  Future<void> loadData() async {
    await Future.delayed(Duration(seconds: 2), () {
      setState(() {
        for (int i = 0; i < 10; i++) {
          datas.add(Car(
              name: "新增数据$i",
              imageUrl:
                  "https://img2.baidu.com/it/u=2010980676,2375641500&fm=11&fmt=auto&gp=0.jpg"));
        }
        _controller.finishLoad(success: true);
      });
    });
  }
}


