Flutter uilder:(BuildContext上下文,int索引)=>Card( 子:列( 儿童:[ Image.network(图像[索引]), 文本(“某些文本”), ], ), ), 交错文件生成器:(int索引)=> 新的交错文件安装(2), 主轴间距:

Flutter uilder:(BuildContext上下文,int索引)=>Card( 子:列( 儿童:[ Image.network(图像[索引]), 文本(“某些文本”), ], ), ), 交错文件生成器:(int索引)=> 新的交错文件安装(2), 主轴间距:,flutter,Flutter,uilder:(BuildContext上下文,int索引)=>Card( 子:列( 儿童:[ Image.network(图像[索引]), 文本(“某些文本”), ], ), ), 交错文件生成器:(int索引)=> 新的交错文件安装(2), 主轴间距:4.0, 交叉轴间距:4.0, ), ); } } 很抱歉,这并不能回答问题,因为该组件正是问题中提到的颤振交错网格视图,没有任何用处。非常感谢!这行代码真的帮我省去了很多麻烦。现在我的照片看起来很棒,上/下/右/左部分没有任何损失。酷!这是否

uilder:(BuildContext上下文,int索引)=>Card( 子:列( 儿童:[ Image.network(图像[索引]), 文本(“某些文本”), ], ), ), 交错文件生成器:(int索引)=> 新的交错文件安装(2), 主轴间距:4.0, 交叉轴间距:4.0, ), ); } }
很抱歉,这并不能回答问题,因为该组件正是问题中提到的颤振交错网格视图,没有任何用处。非常感谢!这行代码真的帮我省去了很多麻烦。现在我的照片看起来很棒,上/下/右/左部分没有任何损失。酷!这是否也适用于无限列表?或者它的性能会因为内容太多而变差吗?你知道是否有可能根据图像使其动态化吗?例如,如果是纵向模式,则将4个图像放在一行中,如果是横向模式,则将2个图像放在一行中?我发现的一切都是手动定义每个图像,但我找不到“动态”解决方案。@Thomas要确定设备方向是否为potrait,可以使用
OrientionBuilder
。阅读此处:我指的是图像,而不是设备方向。@Thomas我没有得到它当我使用
fit(2)
时,视图总是将两个图像放在一起。但我真正想要的是这样的:,但在所有示例中,瓷砖的大小都是预定义的。我希望根据图像的宽高比,它们能达到所需的大小。
return new StreamBuilder<QuerySnapshot>(
      stream: Firestore.instance.collection(PRODUCTS_COLLECTION).snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (!snapshot.hasData) return new Center(child: new CircularProgressIndicator());
        return new StaggeredGridView.count(
          physics: new BouncingScrollPhysics(),
          crossAxisCount: 2,
          children: buildGrid(snapshot.data.documents), 
          staggeredTiles: generateRandomTiles(snapshot.data.documents.length),
        );
      },
    );

List<Widget> buildGrid(List<DocumentSnapshot> documents) {
  List<Widget> _gridItems = [];
  _products.clear();

  for (DocumentSnapshot document in documents) {
    _products.add(Product.fromDocument(document));
  }

  for (Product product in _products) {
    _gridItems.add(buildGridItem(product));
  }

  return _gridItems;
}

Widget buildGridItem(Product product) {
  return new GestureDetector(
    child: new Card(
      elevation: 2.0,
      margin: const EdgeInsets.all(5.0),
      child: new Stack(
        alignment: Alignment.center,
        children: <Widget>[
          new Hero(
            tag: product.name,
            child: new Image.network(product.imageUrl, fit: BoxFit.cover),
          ),
          new Align(
            child: new Container(
              padding: const EdgeInsets.all(6.0),
              child: new Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  new Text('\u20B9 ${product.price}',
                      style: new TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 16.0)),
                  new Text(product.name,
                      style: new TextStyle(color: Colors.white)),
                ],
              ),
              color: Colors.black.withOpacity(0.4),
              width: double.infinity,
            ),
            alignment: Alignment.bottomCenter,
          ),
        ],
      ),
    ),
    onTap: () async {
      // TODO
    },
  );
}

List<StaggeredTile> generateRandomTiles(int count) {
  Random rnd = new Random();
  List<StaggeredTile> _staggeredTiles = [];
  for (int i=0; i<count; i++) {
    num mainAxisCellCount = 0;
    double temp = rnd.nextDouble();

    if (temp > 0.6) {
      mainAxisCellCount = temp + 0.5;
    } else if (temp < 0.3) {
      mainAxisCellCount = temp + 0.9;
    } else {
      mainAxisCellCount = temp + 0.7;
    }
    _staggeredTiles.add(new StaggeredTile.count(rnd.nextInt(1) + 1, mainAxisCellCount));
  }
  return _staggeredTiles;
}
import 'package:flutter/material.dart';

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

class StaggeredGridExample extends StatefulWidget {
  @override
  _StaggeredGridExampleState createState() => _StaggeredGridExampleState();
}

class _StaggeredGridExampleState extends State<StaggeredGridExample> {
  final List<String> images = [
    "https://uae.microless.com/cdn/no_image.jpg",
    "https://images-na.ssl-images-amazon.com/images/I/81aF3Ob-2KL._UX679_.jpg",
    "https://www.boostmobile.com/content/dam/boostmobile/en/products/phones/apple/iphone-7/silver/device-front.png.transform/pdpCarousel/image.jpg",
    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgUgs8_kmuhScsx-J01d8fA1mhlCR5-1jyvMYxqCB8h3LCqcgl9Q",
    "https://ae01.alicdn.com/kf/HTB11tA5aiAKL1JjSZFoq6ygCFXaw/Unlocked-Samsung-GALAXY-S2-I9100-Mobile-Phone-Android-Wi-Fi-GPS-8-0MP-camera-Core-4.jpg_640x640.jpg",
    "https://media.ed.edmunds-media.com/gmc/sierra-3500hd/2018/td/2018_gmc_sierra-3500hd_f34_td_411183_1600.jpg",
    "https://hips.hearstapps.com/amv-prod-cad-assets.s3.amazonaws.com/images/16q1/665019/2016-chevrolet-silverado-2500hd-high-country-diesel-test-review-car-and-driver-photo-665520-s-original.jpg",
    "https://www.galeanasvandykedodge.net/assets/stock/ColorMatched_01/White/640/cc_2018DOV170002_01_640/cc_2018DOV170002_01_640_PSC.jpg",
    "https://media.onthemarket.com/properties/6191869/797156548/composite.jpg",
    "https://media.onthemarket.com/properties/6191840/797152761/composite.jpg",
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StaggeredGridView.countBuilder(
        crossAxisCount: 4,
        itemCount: images.length,
        itemBuilder: (BuildContext context, int index) => Card(
          child: Column(
            children: <Widget>[
              Image.network(images[index]),
              Text("Some text"),
            ],
          ),
        ),
        staggeredTileBuilder: (int index) =>
        new StaggeredTile.fit(2),
        mainAxisSpacing: 4.0,
        crossAxisSpacing: 4.0,
      ),
    );
  }
}