Skip to content

Instantly share code, notes, and snippets.

@Andrious
Created December 4, 2019 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andrious/e0939b045f41db39fe1adc3688784652 to your computer and use it in GitHub Desktop.
Save Andrious/e0939b045f41db39fe1adc3688784652 to your computer and use it in GitHub Desktop.
GridView.builder constructor example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GridView.builder(
primary: false,
padding: const EdgeInsets.all(20),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 10, mainAxisSpacing: 10, crossAxisCount: 2),
itemCount: 6,
itemBuilder: (BuildContext context, int index) {
Widget widget;
switch (index) {
case 0:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('He\'d have you all unravel at the'),
color: Colors.teal[100],
);
break;
case 1:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('Heed not the rabble'),
color: Colors.teal[200],
);
break;
case 2:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('Sound of screams but the'),
color: Colors.teal[300],
);
break;
case 3:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('Who scream'),
color: Colors.teal[400],
);
break;
case 4:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('Revolution is coming...'),
color: Colors.teal[500],
);
break;
default:
widget = Container(
padding: const EdgeInsets.all(8),
child: const Text('Revolution, they...'),
color: Colors.teal[600],
);
}
return widget;
},
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment