# Refresh  下拉刷新

## 基础用法
```dart
import 'package:flutter/material.dart';
import 'package:millui/millui.dart';
import 'dart:math';

import '../app_provider.dart';

class Refash extends StatefulWidget {
  _Refash createState() => new _Refash();
}

class _Refash extends State<Refash> {
  List _items = [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
  ];
  bool _visibileSel = false;
  _refresh() async {
    await Future.delayed(Duration(milliseconds: 1000), () {
      if (mounted) {
        setState(() {
          _items = [
            1,
            2,
            3,
          ];
        });
      }
    });
  }

  _onLoad() async {
    await Future.delayed(Duration(milliseconds: 200), () {
      if (mounted) {
        _items.add(1);
        _items.add(2);
        _items.add(3);
        _items.add(4);
        _items.add(5);
        setState(() {
          _items = _items;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    bool isCn = getLanage(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('refresh'),
      ),
      body: Stack(
        children: [
          Column(
            children: [
              Container(
                height: 30,
                child: Text('Top'),
              ),
              Expanded(
                flex: 1,
                child: MillRefresh(
                  timeout: 6,   //请求超时时间设置为
                  sliverList: _buildList(_items),
                  dropText: isCn ? '下拉刷新' : 'pulldown ↓',
                  releaseText: isCn ? '释放加载' : 'release ↑',
                  footReleaseText: isCn ? '释放加载' : 'release ↓',
                  noDataText: isCn ? '没有更多数据' : 'NO DATA',
                  refreshing: MillBallScaleLoading(),
                  loading: MillBallRotateLoading(size: 2, paintSize: 16,),
                  onRefresh: _refresh,
                  onLoad: _onLoad,
                ),
              ),
            ],
        ],
      ),
    );
  }
}

Widget _buildList(items) {
  return SliverList(
      delegate: SliverChildBuilderDelegate(
    (context, index) {
      return Container(
        padding: EdgeInsets.all(20),
        child: Text('${items[index]}'),
      );
    },
    childCount: items.length,
  ));
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

```
![refresh](../../images/refresh.gif)

![loading](../../images/loading_more.gif)

## 设置 Adapter
```dart
import 'package:flutter/material.dart';
import 'package:millui/millui.dart';
import 'dart:math';

class Refash extends StatefulWidget {
  _Refash createState() => new _Refash();
}

class _Refash extends State<Refash> {
  List _items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

  _refresh() async {
    await Future.delayed(Duration(seconds: 2), () {
      if (mounted) {
        setState(() {
          _items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
        });
      }
    });
  }

  _onLoad() async {
    await Future.delayed(Duration(seconds: 2), () {
      if (mounted) {
        _items.add(1);
        _items.add(2);
        _items.add(3);
        setState(() {
          _items = _items;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('refresh'),
      ),
      body: MillRefresh(
        sliverList: _buildList(_items),
        onRefresh: _refresh,
        onLoad: _onLoad,
        preAdapter: [
          SliverToBoxAdapter(
            child: Container(
              alignment: Alignment.center,
              height: 60,
              child: Text('nihao'),
            ),
          ),
          SliverPersistentHeader(
            pinned: true, //是否固定在顶部
            floating: true,
            delegate: _SliverAppBarDelegate(
              minHeight: 40, //收起的高度
              maxHeight: 40, //展开的最大高度
              child: Container(
                alignment: Alignment.center,
                child: Text('吸顶导航'),
                color: Colors.red,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Widget _buildList(items) {
  return SliverList(
      delegate: SliverChildBuilderDelegate(
    (context, index) {
      return Container(
        padding: EdgeInsets.all(20),
        child: Text('${items[index]}'),
      );
    },
    childCount: items.length,
  ));
}

class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate({
    @required this.minHeight,
    @required this.maxHeight,
    @required this.child,
  });

  final double minHeight;
  final double maxHeight;
  final Widget child;

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return new SizedBox.expand(child: child);
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}

```
## Attributes
| 字段名称 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| sliverList | 列表 | _SliverList|SliverToBoxAdapter/List_ |  |
| onRefresh | 刷新回调 | Function |  |
| onLoad | 加载回调 | Function |  |
| refreshFinished | 刷新完成的 widget | Widget |  |
| refreshing | 刷新中的widget | Widget |  |
| loading | 加载中展示的 widget | Widget |  |
| error | 加载超时展示的 widget | Widget |  |
| timeout | 请求超时时间 (秒) | int | 10 |
| headerBgColor | 头部下拉时的颜色 | Color |  |
| footerBgColor | 底部刷新时的背景颜色 | Color |  |
| preAdapter |  | List |  |
| sufAdapter |  | _SliverToBoxAdapter_ |  |
| hasMore | 是否有更多数据 | bool  | false |
| dropText | 顶部下拉展示的文案 | String | 下拉刷新 |
| releaseText | 顶部释放展示的文案 | String | 释放加载 |
| footReleaseText | 底部释放展示的文案 | String | 释放加载 |
| noDataText | 无数据展示的文案 | String | 没有更多数据 |



