zoukankan      html  css  js  c++  java
  • 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: Http 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,
    child: GridView.count(
    primary: false,
    physics: const NeverScrollableScrollPhysics(),
    //禁止滚动
    mainAxisSpacing: 10,
    crossAxisSpacing: 10,
    crossAxisCount: crossAxisCount == 0 ? 1 : crossAxisCount,

    ///一行数量
    children: imageWidgetList,
    )),
    );
    } else {
    return Container();
    }
    }
  • 相关阅读:
    rsync
    2个网卡流量的变化
    服务器端FIN的条件
    The third column indicates whether subclasses of the class declared outside this package have access to the member.;
    Java反序列化与远程代码执行

    address sizes : 46 bits physical, 48 bits virtual
    intptr_t、uintptr_t数据类型的解析
    Why should we typedef a struct so often in C?
    源码 502 回溯
  • 原文地址:https://www.cnblogs.com/lulushen/p/12834747.html
Copyright © 2011-2022 走看看