Flutter-ListView+Pulltorefresh

//https://github.com/peng8350/flutter_pulltorefresh

import 'package:flutter/material.dart';

import 'package:dio/dio.dart';

import 'movie.dart';

import 'dart:convert';

import 'package:flutter/cupertino.dart';

import "package:pull_to_refresh/pull_to_refresh.dart";

class ListPulltorefreshWidgetextends StatefulWidget {

String_title;

ListPulltorefreshWidget(this._title);

@override

  _ListPulltorefreshWidgetState createState() => _ListPulltorefreshWidgetState();

}

class _ListPulltorefreshWidgetStateextends State {

ListmovieList = [];

RefreshController_refreshController;

@override

  void initState() {

super.initState();

getContent();

_refreshController = RefreshController();

//    getContent().then((List list){

//      setState(() {

//        movieList = list;

//      });

//    });

  }

void enterRefresh() {

_refreshController.requestRefresh(true);

}

getBody() {

if (movieList.length !=0) {

return Container(

child:new SmartRefresher(

enablePullDown:true,

enablePullUp:true,

controller:_refreshController,

onRefresh: (up) {

if (up)

new Future.delayed(const Duration(milliseconds:2009))

.then((val) {

//                    _refreshController.scrollTo(_refreshController.scrollController.offset + 100.0);

                    _refreshController.sendBack(true, RefreshStatus.idle);

setState(() {

});

_refreshController.sendBack(true, RefreshStatus.completed);

});

else {

new Future.delayed(const Duration(milliseconds:2009))

.then((val) {

//                    _refreshController.scrollTo(_refreshController.scrollController.offset + 100.0);

                    _refreshController.sendBack(false, RefreshStatus.idle);

//                    setState(() {

//

//                    });

//                    _refreshController.sendBack(false, RefreshStatus.completed);

                  });

}

},

onOffsetChange: _onOffsetCallback,

child: ListView.builder(

reverse:true,

//                itemExtent: 150.0,

                itemCount:movieList.length,

itemBuilder: (BuildContext context, int position) {

return getItem(movieList[position]);

},

)

));

//      return ListView.builder(

//          itemCount: movieList.length,

//          itemBuilder: (BuildContext context, int position) {

//            return getItem(movieList[position]);

//          });

    }else {

// 加载菊花

      return CupertinoActivityIndicator();

}

}

void _onOffsetCallback(bool isUp, double offset) {

// if you want change some widgets state ,you should rewrite the callback

  }

@override

  Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(title: Text(widget._title),backgroundColor: Colors.lightBlue,),

body: getBody(),

);

}

getItem(var subject) {

//    演员列表

    var avatars = List.generate(subject.casts.length, (int index) =>

Container(

margin: EdgeInsets.only(left: index.toDouble() ==0.0 ?0.0 :16.0),

child: CircleAvatar(

backgroundColor: Colors.white10,

backgroundImage: NetworkImage(

subject.casts[index].avatars.small,

)

),

),

);

var row = Container(

margin: EdgeInsets.all(4.0),

child: Row(

children: [

ClipRRect(

borderRadius: BorderRadius.circular(4.0),

child: Image.network(

subject.images.large,

width:100.0, height:150.0,

fit: BoxFit.fill,

),

),

Expanded(

child: Container(

margin: EdgeInsets.only(left:8.0),

height:150.0,

child: Column(

crossAxisAlignment: CrossAxisAlignment.start,

children: [

//                    电影名称

                    Text(

subject.title,

style: TextStyle(

fontWeight: FontWeight.bold,

fontSize:20.0,

),

maxLines:1,

),

//                    豆瓣评分

                    Text(

'豆瓣评分:${subject.rating.average}',

style: TextStyle(

fontSize:16.0

                      ),

),

//                    类型

                    Text(

"类型:${subject.genres.join("、")}"

                    ),

//                    导演

                    Text(

'导演:${subject.directors[0].name}'

                    ),

//                    演员

                    Container(

margin: EdgeInsets.only(top:8.0),

child: Row(

children: [

Text('主演:'),

Row(

children: avatars,

)

],

),

)

],

),

)

)

],

),

);

return Card(

child: row,

);

}

//  Future> getContent() async {

//    BaseResp baseResp = await DioUtil().request(Method.get, 'http://api01.idataapi.cn:8000/news/qihoo?apikey=fhtmiuCEpY5wMir5tkPNSk5FE9tHWbjH96nn30n3PNiK4HH411huikTtQxGSu2tQ&kw=白&site=qq.com');

//    List bannerList;

//    print('lcj0000'+ String.fromCharCode(baseResp.code));

//    if (baseResp.status != 000000) {

//      print('lcj'+baseResp.msg);

//      return new Future.error(baseResp.msg);

//    }

//    if (baseResp.data != null) {

//      print('lcj111');

////      bannerList = baseResp.data.map((value) {

////        print('lcj33333');

////        return Data.fromJson(value);

////      }).toList();

//    }

//    return bannerList;

//  }

//  getContent() async {

//    BaseResp baseResp = await DioUtil().request(Method.get, 'http://api01.idataapi.cn:8000/news/qihoo?apikey=fhtmiuCEpY5wMir5tkPNSk5FE9tHWbjH96nn30n3PNiK4HH411huikTtQxGSu2tQ&kw=白&site=qq.com');

//    Map userMap = json.decode(baseResp.data);

//    var movie = new Movie.fromJson(userMap);

//    setState(() {

//        movieList = movie.data;

//    });

//  }

  getContent()async {

Response response =await Dio().get("https://api.douban.com/v2/movie/in_theaters");

Map userMap = json.decode(response.toString());

var movie1 =new movie.fromJson(userMap);

setState(() {

movieList = movie1.subjects;

});

}

}

你可能感兴趣的:(Flutter-ListView+Pulltorefresh)