显示图像时在颤振中丢失设备连接

时间:2021-05-06 02:06:18

标签: flutter dart

我有一列小部件,显示应用程序中的顶级产品,当它显示产品的图像时,它会在 2 秒后失去与设备的连接时显示产品的所有详细信息和图像,这是小部件:

class Topproducts extends StatefulWidget {
  final Size size;
  final List<Products> productlist;
  final Users userdata;
  Topproducts(
      {@required this.size, @required this.productlist, @required this.userdata});

  @override
  _TopproductsState createState() => _TopproductsState();
}

class _TopproductsState extends State<Topproducts> {

    List toplist;
  List<Products> gettoplist(List productlist) {
    List<Products> toplist = [];
    for (var i = 0; i < productlist.length; i++) {
      if (productlist[i].getrating() >= 4.9) {
        toplist.add(productlist[i]);
      }
    }
    return toplist;
  }

  @override
  void initState() {
    super.initState();
    toplist = gettoplist(widget.productlist);
    toplist.removeRange(9, toplist.length - 1);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.size.width,
      child: Column(
        children: toplist.map((product) {
          return Productbox(
            product: product,
            size: widget.size,
            productlist: widget.productlist,
            userdata:widget.userdata,
          );
        }).toList(),
      ),
    );
  }
}

这是从 firebase 存储中获取图像的 productbox 代码:

class Productbox extends StatefulWidget {
  final Size size;
  final Users userdata;
  final Products product;
  final List productlist;
  final bool isvertical;
  Productbox({
    @required this.size,
    this.userdata,
    this.productlist,
    @required this.product,
    this.isvertical = false,
  });

  Productbox.vertical({
    @required this.size,
    this.userdata,
    this.productlist,
    @required this.product,
    this.isvertical = true,
  });

  @override
  _ProductboxState createState() => _ProductboxState();
}

class _ProductboxState extends State<Productbox> {
  double paddinghorizontal = 0;
  double paddingvertical = 0;
  dynamic imageurl;
  Size size;
  Products product;

  @override
  void initState() {
    super.initState();

    size = widget.size;
    product = widget.product;
    Storage().getproductimage(product.id).then((value) {
      setState(() {
        imageurl = value;
      });
    });
  }

  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) {
              return Productscreen(
                product: widget.product,
                size: widget.size,
                userdata: widget.userdata,
                productslist: widget.productlist,
                imageurl: imageurl,
              );
            },
          ),
        );
      },
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: paddinghorizontal,
          vertical: paddingvertical,
        ),
        width: widget.isvertical
            ? widget.size.width * 0.6
            : widget.size.width * 0.9,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          border: Border.all(
            color: Colors.grey[350],
            width: 1,
          ),
        ),
        margin: EdgeInsets.only(
          left: widget.size.width * 0.04,
          top: widget.size.height * 0.045,
          bottom: widget.size.height * 0.01,
          right: widget.size.width * 0.04,
        ),
        child: widget.isvertical
            ? Column(
                children: [
                  Container(
                    height: widget.size.height * 0.2,
                    width: size.width * 0.45,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(10),
                        bottomLeft: Radius.circular(10),
                      ),
                    ),
                    alignment: Alignment.center,
                    child: imageurl.runtimeType == String
                        ? Hero(
                            tag: imageurl,
                            child: Container(
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: NetworkImage(imageurl),
                                  fit: BoxFit.fill,
                                ),
                              ),
                            ),
                          )
                        : imageurl.runtimeType == Errormodel
                            ? Text(imageurl.message)
                            : JumpingDotsProgressIndicator(
                                color: Theme.of(context).primaryColor,
                                dotSpacing: 3,
                                fontSize: 20,
                                numberOfDots: 3,
                                milliseconds: 400,
                              ),
                  ),
                  Detailsbox(size: size, product: product),
                ],
              )
            : Row(
                children: [
                  Container(
                    height: size.height * 0.2,
                    width: size.width * 0.45,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(10),
                        bottomLeft: Radius.circular(10),
                      ),
                    ),
                    alignment: Alignment.center,
                    child: imageurl.runtimeType == String
                        ? Hero(
                            tag: imageurl,
                            child: Container(
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: NetworkImage(imageurl),
                                  fit: BoxFit.fill,
                                ),
                              ),
                            ))
                        : imageurl.runtimeType == Errormodel
                            ? Text(imageurl.message)
                            : JumpingDotsProgressIndicator(
                                color: Theme.of(context).primaryColor,
                                dotSpacing: 3,
                                fontSize: 20,
                                numberOfDots: 3,
                                milliseconds: 400,
                              ),
                  ),
                  Detailsbox(
                    size: size,
                    product: product,
                  ),
                ],
              ),
      ),
    );
  }
}

我认为从存储中获取图像时会出现错误,但我不知道我不确定的解决方案

0 个答案:

没有答案