Flutter - Mesh Layout

I'm trying to arrange a grid of 4x4 tiles in a flutter. I managed to do this with columns and rows. But now I found the GridView component. Can anyone give an example of how to do this using it?

I can’t wrap my head around documents. It seems I am not getting the desired results.

Thanks :)

+15
source share
5 answers

A simple example of loading images into tiles.

 import 'package:flutter/material.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new Container( color: Colors.white30, child: new GridView.count( crossAxisCount: 4, childAspectRatio: 1.0, padding: const EdgeInsets.all(4.0), mainAxisSpacing: 4.0, crossAxisSpacing: 4.0, children: <String>[ 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', 'http://www.for-example.org/img/main/forexamplelogo.png', ].map((String url) { return new GridTile( child: new Image.network(url, fit: BoxFit.cover)); }).toList()), ); } } 

Flutter GridView Example

The Flutter Gallery app contains an example of the real world, which can be found here .

+21
source

GridView used to implement a list of grid materials. If you know that you have a fixed number of items, and this is not very much (16 in order), you can use GridView.count . However, you should notice that the GridView scrolls, and if that is not what you want, you might be better off with just rows and columns.

screenshot

 import 'dart:collection'; import 'package:flutter/scheduler.dart'; import 'package:flutter/material.dart'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter/foundation.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.orange, ), home: new MyHomePage(), ); } } class MyHomePage extends StatelessWidget{ @override Widget build(BuildContext context){ return new Scaffold( appBar: new AppBar( title: new Text('Grid Demo'), ), body: new GridView.count( crossAxisCount: 4, children: new List<Widget>.generate(16, (index) { return new GridTile( child: new Card( color: Colors.blue.shade200, child: new Center( child: new Text('tile $index'), ) ), ); }), ), ); } } 
+21
source

Please visit this repo .

 Widget _gridView() { return GridView.count( crossAxisCount: 4, padding: EdgeInsets.all(4.0), childAspectRatio: 8.0 / 9.0, children: itemList .map( (Item) => ItemList(item: Item), ) .toList(), ); } 

The screenshot below contains crossAxisCount: 2 this screenshot is for grid count 2

+10
source

GridView has several named constructors for different scenarios,

Constructors

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

The following is an example of a GridView constructor:

 import 'package:flutter/material.dart'; void main() => runApp( MaterialApp( home: ExampleGrid(), ), ); class ExampleGrid extends StatelessWidget { 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: 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 elements to be dynamic according to the content, you can do this in a few lines, but this is the easiest way to use the StaggeredGridView package. I provided an answer with an example here .

The following is an example for 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 the snippet above:

Flutter gridview example by blasanka using card widget and robohash api

Example for 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: <Widget>[ 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")); }), ), ), ], ) ); } } 
+4
source

Using rows and columns, you can create a layout of any type: http://www.yourowncodes.com/flutter-understanding-layouts-rows-columns-design/

0
source

Source: https://habr.com/ru/post/1268236/


All Articles