Skip to content

Instantly share code, notes, and snippets.

@av
Created September 18, 2019 14:14
Show Gist options
  • Save av/541ccea984a3c033d8085801db41ee6b to your computer and use it in GitHub Desktop.
Save av/541ccea984a3c033d8085801db41ee6b to your computer and use it in GitHub Desktop.
Grid gap rendering
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Grid gaps',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Grid Gaps'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: CustomScrollView(
physics: BouncingScrollPhysics(),
slivers: <Widget>[
_buildListHeader('.5 pixel, white'),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1,
mainAxisSpacing: .5,
crossAxisSpacing: .5,
),
delegate: SliverChildBuilderDelegate(
_buildChild,
addRepaintBoundaries: true,
childCount: 12,
),
),
_buildListHeader('1 pixel, white'),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1,
mainAxisSpacing: 1,
crossAxisSpacing: 1,
),
delegate: SliverChildBuilderDelegate(
_buildChild,
addRepaintBoundaries: true,
childCount: 12,
),
),
_buildListHeader('995‰ transform'),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, childAspectRatio: 1),
delegate: SliverChildBuilderDelegate(
(context, i) => Container(
color: Colors.black,
transform: Matrix4.identity()..scale(.995),
),
addRepaintBoundaries: true,
childCount: 12,
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildListHeader(String text) {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => Container(
margin: const EdgeInsets.all(16.0),
child: Text(
text,
style: Theme.of(context).textTheme.title,
),
),
childCount: 1,
addAutomaticKeepAlives: false,
),
);
}
Widget _buildChild(context, i) {
return Container(
color: Colors.black,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment