flutter_staggered_grid_view

repository·master·Indexed 25 days ago

https://github.com/letsar/flutter_staggered_grid_view

A collection of various grid layout implementations for Flutter, providing specialized layouts including Masonry, Quilted, Woven, Aligned (CSS-style), and Staired grids. It offers both standalone widgets like StaggeredGrid and MasonryGridView, as well as sliver delegates such as SliverQuiltedGridDelegate, SliverWovenGridDelegate, and SliverStairedGridDelegate for use with GridView.

Tokens
6.5K
Snippets
10
Records
42
Agent score
85%

What's inside flutter_staggered_grid_view

  1. Install flutter_staggered_grid_view

    master

    To use this package, add it to your pubspec.yaml dependencies:

    dependencies:
      ...
      flutter_staggered_grid_view: <latest_version>

    Then, import it in your Dart files:

    import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
    dependencies:
      ...
      flutter_staggered_grid_view: <latest_version>
  2. Customize iOS launch screen assets

    master

    To change the launch screen image for the iOS version of the example app, you can either:

    1. Replace the image files directly in the examples/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory.
    2. Open the iOS project in Xcode using open ios/Runner.xcworkspace, navigate to Runner/Assets.xcassets in the Project Navigator, and drag and drop your new images into the asset catalog.
    open ios/Runner.xcworkspace
  3. Use MasonryGridView for scrollable masonry layouts

    master

    The MasonryGridView widget provides a scrollable, 2D array of widgets arranged in a masonry layout. It is a high-level widget that wraps a SliverMasonryGrid inside a CustomScrollView.

    Common usage patterns include:

    • Fixed number of columns: Use MasonryGridView.count with a crossAxisCount.
    • Maximum column width: Use MasonryGridView.extent with a maxCrossAxisExtent.
    • Large/Infinite lists: Use MasonryGridView.builder to create items on demand.
    • Custom layouts: Use MasonryGridView with a custom gridDelegate or MasonryGridView.custom for full control over both the grid delegate and the children delegate.
  4. Use AlignedGridView for scrollable aligned grid layouts

    master

    The AlignedGridView widget provides a scrollable, 2D array of widgets arranged in an aligned layout. It is a high-level widget that wraps a CustomScrollView containing a SliverAlignedGrid.

    There are three primary ways to instantiate it:

    1. AlignedGridView.count: Creates a grid with a fixed number of tiles in the cross axis.
    2. AlignedGridView.extent: Creates a grid where tiles have a maximum cross-axis extent (useful for responsive layouts).
    3. AlignedGridView.custom: Allows you to provide a custom SliverSimpleGridDelegate for arbitrary 2D arrangements.

    Common properties include mainAxisSpacing, crossAxisSpacing, itemCount, and itemBuilder.

  5. Transition from AlignedGridView to CustomScrollView

    master

    If you need to combine the grid with other slivers (like SliverAppBar or SliverList), you can port your AlignedGridView to a CustomScrollView.

    An AlignedGridView is essentially a CustomScrollView with a single SliverAlignedGrid in its slivers property.

    Mapping guide:

    • AlignedGridView.count $\rightarrow$ SliverAlignedGrid.count
    • AlignedGridView.extent $\rightarrow$ SliverAlignedGrid.extent
    • padding $\rightarrow$ Wrap the SliverAlignedGrid in a SliverPadding within the CustomScrollView.
  6. Port MasonryGridView to CustomScrollView

    master

    If you need to combine a masonry grid with other slivers (like a SliverAppBar or SliverList), you can port MasonryGridView to a CustomScrollView.

    Mapping Guide:

    • MasonryGridView $\rightarrow$ CustomScrollView with a single SliverMasonryGrid in the slivers list.
    • gridDelegate $\rightarrow$ SliverMasonryGrid.gridDelegate.
    • childrenDelegate $\rightarrow$ SliverMasonryGrid.delegate.
    • padding $\rightarrow$ Wrap the SliverMasonryGrid in a SliverPadding.
    • itemBuilder/itemCount $\rightarrow$ Use SliverChildBuilderDelegate inside the SliverMasonryGrid delegate.
  7. Use QuiltedGridDelegate with GridView

    master

    The SliverQuiltedGridDelegate is a specific delegate for the built-in GridView (or SliverGrid) widget. It creates hierarchy using varied container sizes and ratios.

    Grid Properties:

    • Evenly divided into n columns.
    • The height of each row is equal to the width of each column.
    • A pattern defines tile sizes and repetition modes.

    Tile Properties:

    • Tiles can occupy 1 to n columns.
    • Tiles must occupy 1 or more entire rows.
    GridView.custom(
      gridDelegate: SliverQuiltedGridDelegate(
        crossAxisCount: 4,
        mainAxisSpacing: 4,
        crossAxisSpacing: 4,
        repeatPattern: QuiltedGridRepeatPattern.inverted,
        pattern: [
          QuiltedGridTile(2, 2),
          QuiltedGridTile(1, 1),
          QuiltedGridTile(1, 1),
          QuiltedGridTile(1, 2),
        ],
      ),
      childrenDelegate: SliverChildBuilderDelegate(
        (context, index) => Tile(index: index),
      ),
    );
  8. Use AlignedGridView (CSS Grid style)

    master

    The AlignedGridView (often called CSS Grid) ensures that each item within a track has the maximum cross-axis extent of its siblings.

    Grid Properties:

    • Evenly divided into n columns.
    • Rows can have different heights.

    Tile Properties:

    • Tiles must occupy exactly 1 column.
    • Each tile has the same height as the tallest one in its row.
    AlignedGridView.count(
      crossAxisCount: 4,
      mainAxisSpacing: 4,
      crossAxisSpacing: 4,
      itemBuilder: (context, index) {
        return Tile(
          index: index,
          extent: (index % 7 + 1) * 30,
        );
      },
    );
  9. Use StaggeredGrid for small, non-scrollable layouts

    master

    The StaggeredGrid is intended for a small number of items and is not a GridView (it is not scrollable and does not support Sliver contexts).

    Grid Properties:

    • Evenly divided into n columns.
    • Tiles can occupy between 1 and n columns.
    • Placement follows a top-most, then left-most algorithm.
    StaggeredGrid.count(
      crossAxisCount: 4,
      mainAxisSpacing: 4,
      crossAxisSpacing: 4,
      children: const [
        StaggeredGridTile.count(
          crossAxisCellCount: 2,
          mainAxisCellCount: 2,
          child: Tile(index: 0),
        ),
        StaggeredGridTile.count(
          crossAxisCellCount: 2,
          mainAxisCellCount: 1,
          child: Tile(index: 1),
        ),
        StaggeredGridTile.count(
          crossAxisCellCount: 1,
          mainAxisCellCount: 1,
          child: Tile(index: 2),
        ),
        StaggeredGridTile.count(
          crossAxisCellCount: 1,
          mainAxisCellCount: 1,
          child: Tile(index: 3),
        ),
        StaggeredGridTile.count(
          crossAxisCellCount: 4,
          mainAxisCellCount: 2,
          child: Tile(index: 4),
        ),
      ],
    );
  10. Use SliverWovenGridDelegate with GridView

    master

    The SliverWovenGridDelegate is a specific delegate for the built-in GridView (or SliverGrid) widget. It creates a rhythmic layout using containers of varying ratios.

    Grid Properties:

    • Evenly divided into n columns.
    • Row height is the maximum height of the tiles.
    • Tile sizes follow a pattern in a 'z' sequence.

    Tile Properties:

    • Height is defined by an aspectRatio (width/height).
    • Width is defined by a crossAxisRatio (width/column's width) between 0 (exclusive) and 1 (inclusive).
    • Tiles can define alignment within available space.
    GridView.custom(
      gridDelegate: SliverWovenGridDelegate.count(
        crossAxisCount: 2,
        mainAxisSpacing: 8,
        crossAxisSpacing: 8,
        pattern: [
          WovenGridTile(1),
          WovenGridTile(
            5 / 7,
            crossAxisRatio: 0.9,
            alignment: AlignmentDirectional.centerEnd,
          ),
        ],
      ),
      childrenDelegate: SliverChildBuilderDelegate(
        (context, index) => Tile(index: index),
      ),
    );
  11. Use SliverStairedGridDelegate with GridView

    master

    The SliverStairedGridDelegate is a specific delegate for the built-in GridView (or SliverGrid) widget. It uses alternating container sizes and ratios to create a rhythmic effect.

    Grid Properties:

    • A pattern defines tile sizes.
    • Each tile is shifted from the previous one by a margin in both axes.
    • Placement follows a 'z' sequence.

    Tile Properties:

    • Height is defined by an aspectRatio (width/height).
    • Width is defined by a crossAxisRatio (width/available horizontal space) between 0 (exclusive) and 1 (inclusive).
    GridView.custom(
      gridDelegate: SliverStairedGridDelegate(
        crossAxisSpacing: 48,
        mainAxisSpacing: 24,
        startCrossAxisDirectionReversed: true,
        pattern: [
          StairedGridTile(0.5, 1),
          StairedGridTile(0.5, 3 / 4),
          StairedGridTile(1.0, 10 / 4),
        ],
      ),
      childrenDelegate: SliverChildBuilderDelegate(
        (context, index) => Tile(index: index),
      ),
    );
  12. Use MasonryGridView for uncropped peer content

    master

    The MasonryGridView is designed for browsing content where container heights are determined by the widget size. It is a separate grid (not a SliverGridDelegate) for performance reasons.

    Grid Properties:

    • Evenly divided into n columns.
    • Tiles must occupy exactly 1 column.
    • Placement follows a top-most, then left-most algorithm.
    MasonryGridView.count(
      crossAxisCount: 4,
      mainAxisSpacing: 4,
      crossAxisSpacing: 4,
      itemBuilder: (context, index) {
        return Tile(
          index: index,
          extent: (index % 5 + 1) * 100,
        );
      },
    );