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();
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM