flutter下拉刷新上拉加载 简单使用

2020/10/13 16:05
阅读数 705

使用flutter_easyrefresh库
本实例:flutter_easyrefresh版本: ^1.2.7
使用

import 'dart:convert' as JSON;
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:meituanwaimai/wangluoqingqiu/shuaxinmoban.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
   
   
  @override
  Widget build(BuildContext context) {
   
   
    return new MaterialApp(
      // App名字
      title: 'EasyRefresh',
      // App主题
      theme: new ThemeData(
        primarySwatch: Colors.orange,
      ),
      // 主页
      home: BasicPage(),
    );
  }
}

class BasicPage extends StatefulWidget {
   
   
  @override
  State<StatefulWidget> createState() {
   
   
    return _BasicPageState();
  }
}

class _BasicPageState extends State<BasicPage> {
   
   

  int kaishi = 0;
  int number = 5;
  @override
  void initState() {
   
   
    // TODO: implement initState
    super.initState();
    qingqiu();
  }

  qingqiu() async {
   
   
    var resalt = await shuaxinmoban.resquest(
      '/xzj?start=$kaishi&count=$number',
      method: shuaxinmoban.GET,
    );

    var resalt_json = JSON.jsonDecode(resalt);

    setState(() {
   
   
      List<dynamic> one = resalt_json['shuju'];
      str.addAll(one);
    });
  }

  List<String> addStr = [];
  List<dynamic> str = [];


  @override
  Widget build(BuildContext context) {
   
   
    return Scaffold(
      appBar: AppBar(
        title: Text("EasyRefresh"),
      ),
      body: Center(
          child: new EasyRefresh(
        child: new ListView.builder(
            //ListView的Item
            itemCount: str.length,
            itemBuilder: (BuildContext context, int index) {
   
   
              return new Container(
                  height: 70.0,
                  child: Card(
                    child: new Center(
                      child: new Text(
                        str[index],
                        style: new TextStyle(fontSize: 18.0, color: Colors.red),
                      ),
                    ),
                  ));
            }),
        onRefresh: () async {
   
   
          // //下拉请求新数据
          await new Future.delayed(const Duration(seconds: 1), () async {
   
   
            //this.kaishi++;
            //int offset=(kaishi-1)*number;
            var resalt = await shuaxinmoban.resquest(
              '/xzj1?start=$kaishi&count=$number',
              method: shuaxinmoban.GET,
            );

            var resalt_json = JSON.jsonDecode(resalt);

            setState(() {
   
   
              List<dynamic> one = resalt_json['shuju'];
              str.clear();
              str.addAll(one);
            });
          });

        },
        loadMore: () async {
   
   
          await new Future.delayed(const Duration(seconds: 1), () async {
   
   
            this.kaishi++;
            int offset=(kaishi-1)*number;
            var resalt = await shuaxinmoban.resquest(
              '/xzj1?start=$offset&count=$number',
              method: shuaxinmoban.GET,
            );
            var resalt_json = JSON.jsonDecode(resalt);
            setState(() {
   
   
              List<dynamic> one = resalt_json['shuju'];
              str.addAll(one);
            });
          });
        },
      )), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部
返回顶部
顶部