import 'dart:ffi';

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

/**
 * @author xuehao
 * on 2021/7/13
 */
class GridPage extends StatefulWidget {
  GridPage({Key key}) : super(key: key);

  @override
  _GridPageState createState() => _GridPageState();
}

class _GridPageState extends State<GridPage> {
  EasyRefreshController _controller = EasyRefreshController();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "grid_page",
      home: Scaffold(
        appBar: AppBar(
          title: Text("grid_page"),
        ),
        body: EasyRefresh(
          controller: _controller,
          header: MaterialHeader(),
          footer: PhoenixFooter(),
          onRefresh: () async {
            _refresh();
          },
          onLoad: () async {
            _loadData();
          },
          child: GridView.builder(
            ///GridView配置项
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              //设置每行最大显示数量
              crossAxisCount: 3,
              //横轴间距
              crossAxisSpacing: 2.0,
              //主轴间距
              mainAxisSpacing: 2.0,
              //宽高比
              childAspectRatio: 0.7,
            ),
            itemCount: _datas.length,
            itemBuilder: (context, index) {
              return _itemWidget(_datas[index]);
            },
          ),
        ),
      ),
    );
  }

  List<String> _datas = [
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
    "https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg",
  ];

  Future<void> _refresh() async {
    Future.delayed(Duration(seconds: 2), () async {
      setState(() {
        for (int i = 0; i < _datas.length; i++) {
          _datas[i] =
              "https://img2.baidu.com/it/u=1852546487,3301595200&fm=11&fmt=auto&gp=0.jpg";
        }
        _controller.finishRefresh(success: true);
      });
    });
  }

  Future<void> _loadData() async {
    Future.delayed(Duration(seconds: 2), () async {
      setState(() {
        for (int i = 0; i < 10; i++) {
          _datas.add("https://img1.baidu.com/it/u=4241162085,3588743387&fm=26&fmt=auto&gp=0.jpg");
        }
        _controller.finishLoad(success: true);
      });
    });
  }

  Widget _itemWidget(String src) {
    return new Image.network(src, fit: BoxFit.cover);
  }
}
