Flutter Future 异步 FutureBuilder 获取数据后 加载Widget

// 不使用FutureBuilder,直接在then或whenComplete中加载渲染Widget会报错

future几个函数:
then:异步操作逻辑在这里写。
whenComplete:异步完成时的回调。
catchError:捕获异常或者异步出错时的回调。
///图片
FutureBuilder(
future: getImages(
"${Global.baseUrl}${API.readCommentImageURL}?commentPkid=${parentItem.comment_pkid}&isThumbnail=true",
context),
builder:
(BuildContext context, AsyncSnapshot<List> s) {
LogUtils.log("ssss", s.data);
return reportCommentImageWidget(s.data);
},
),

//获取一条评论的相关图片及widget
Future<List> getImages(String url, BuildContext context) async {
List<Widget> imageWidgetList = [];
//commentPkid 图片文件夹标识
var httpClient = new HttpClient();
String result;
try {
var request = await httpClient.getUrl(Uri.parse(url));
request.headers.add("Semf-Token", Global.token);

var response = await request.close();

if (response.statusCode == 200) {
var json = await response.transform(utf8.decoder).join();
List imageBase64List = jsonDecode(json);

if (imageBase64List != null) {
for (var imageBase64Str in imageBase64List) {
Uint8List bytes = Base64Decoder().convert(imageBase64Str);
LogUtils.log("bytes", bytes);

Widget imageWidget;
if (bytes != null) {
imageWidget = Container(
child: InkWell(
child: Container(
child:
Image.memory(bytes != null ? bytes : "", fit: BoxFit.fill),
),
onTap: () {
Navigator.of(context).push(
CustomRoute(ReportCommentImagePage(imageByte: bytes)));
},
));
} else {
imageWidget = Container(
child: Image.asset('images/report_imagePlaceholder.png'),
);
}
imageWidgetList.add(imageWidget);
}
LogUtils.log("imageWidgetList-0-", imageWidgetList);
return await imageWidgetList;
} else {
return imageWidgetList;
}
} else {
result = 'Error getting IP address:\nHttp status ${response.statusCode}';
}
} catch (exception) {
result = 'Failed getting IP address';
}
}

///评论图片
Widget reportCommentImageWidget(List<Widget> imageWidgetList) {
var size = 0.0;

if (imageWidgetList != null && imageWidgetList.length > 0) {
LogUtils.log("图片控件数组", imageWidgetList);
if (imageWidgetList.length == 1) {
size = ScreenUtil.getInstance().width / 3.0;
} else if (imageWidgetList.length == 2) {
size = ScreenUtil.getInstance().width / 5.0;
} else if (imageWidgetList.length >= 3) {
size = ScreenUtil.getInstance().width / 6.5;
}
var crossAxisCount = 0;
var height = 0.0;
var width = 0.0;
if (imageWidgetList.length >= 3) {
crossAxisCount = 3;

///向上取整ceil 20 边距
height = size * ((imageWidgetList.length / 3.0).ceil()) +
10 * (imageWidgetList.length / 3.0).ceil() +
20;
width = (size + 10) * 3.0 + 20;
} else if (imageWidgetList.length > 0 && imageWidgetList.length < 3) {
crossAxisCount = imageWidgetList.length;
height = size + 20;
width = (size + 10) * imageWidgetList.length + 20;
}
return Center(
child: Container(
padding: EdgeInsets.all(10),
height: height,
width: width,
child: GridView.count(
primary: false,
physics: const NeverScrollableScrollPhysics(),
//禁止滚动
mainAxisSpacing: 10,
crossAxisSpacing: 10,
crossAxisCount: crossAxisCount == 0 ? 1 : crossAxisCount,

///一行数量
children: imageWidgetList,
)),
);
} else {
return Container();
}
}
posted @ 2020-05-06 10:17  Belinda_sl  阅读(1411)  评论(0编辑  收藏  举报