Flutter - Layout a Grid

前端 未结 5 1072
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 01:46

I am trying to layout a 4x4 grid of tiles in flutter. I managed to do it with columns and rows. But now I found the GridView component. Could anyone provide an

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 01:52

    There are few named constructors in GridView for different scenarios,

    Constructors

    1. GridView
    2. GridView.builder
    3. GridView.count
    4. GridView.custom
    5. GridView.extent

    Below is a example of GridView constructor:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
      MaterialApp(
        home: ExampleGrid(),
      ),
    );
    
    class ExampleGrid extends StatelessWidget {
      List 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: GridView(
            physics: BouncingScrollPhysics(), // if you want IOS bouncing effect, otherwise remove this line
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),//change the number as you want
            children: images.map((url) {
              return Card(child: Image.network(url));
            }).toList(),
          ),
        );
      }
    }
    

    If you want your GridView items to be dynamic according to the content, you can few lines to do that but the simplest way to use StaggeredGridView package. I have provided an answer with example here.

    Below is an example for a GridView.count:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: ExampleGrid(),
          ),
        );
    
    class ExampleGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GridView.count(
            crossAxisCount: 4,
            children: List.generate(40, (index) {
              return Card(
                child: Image.network("https://robohash.org/$index"),
              ); //robohash.org api provide you different images for any number you are giving
            }),
          ),
        );
      }
    }
    

    Screenshot for above snippet:

    Example for a SliverGridView:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: ExampleGrid(),
          ),
        );
    
    class ExampleGrid extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomScrollView(
            primary: false,
            slivers: [
              SliverPadding(
                padding: const EdgeInsets.all(20.0),
                sliver: SliverGrid.count(
                  crossAxisSpacing: 10.0,
                  crossAxisCount: 2,
                  children: List.generate(20, (index) {
                    return Card(child: Image.network("https://robohash.org/$index"));
                  }),
                ),
              ),
            ],
          )
        );
      }
    }
    

提交回复
热议问题