FutureBuilder 笔记
image.png
1.FutureBuilder
FutureBuilder(
future: getHomePageContent(), //异步网络请求
builder: (BuildContext context,AsyncSnapshot snapshot){
if(snapshot.hasData){//如何返回数据
Map newMap = snapshot.data; //网络返回的数据源
List<Map> rst = (newMap['rst'] as List).cast();
List<Map> swiper = rst;
// (_data['data']['slides'] as List).cast();
return Column(
children: [
SwiperDiy(swiper) //录播图显示
],
);
}else{ //如果没有返回
return Center(
child: Text('加载中'),
);
}
},
),
2.轮播图
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
class SwiperDiy extends StatelessWidget {
// const SwiperDiy({Key? key}) : super(key: key);
//
final List wiperDateList;
SwiperDiy(this.wiperDateList);
@override
Widget build(BuildContext context) {
return Container(
height: 250,
child: Swiper(
itemBuilder: (BuildContext context, int index) {
return Image.network("${wiperDateList[index]['extData']['images']}",fit: BoxFit.fill);
},
itemCount:wiperDateList.length,
pagination: new SwiperPagination(),
autoplay: true,
// control: new SwiperControl(),
),
);
}
}
网友评论