flutter下拉刷新上拉加載 簡單使用

使用flutter_easyrefresh庫
本實例:flutter_easyrefresh版本: ^1.2.7
使用

java

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.
    );
  }
}
相關文章
相關標籤/搜索