Skip to content

Instantly share code, notes, and snippets.

@dsyrstad
Created May 5, 2022 18:05
Show Gist options
  • Save dsyrstad/ecc79ed05f25a462476ea414812158f2 to your computer and use it in GitHub Desktop.
Save dsyrstad/ecc79ed05f25a462476ea414812158f2 to your computer and use it in GitHub Desktop.
flutter_layout_grid nested grid overflow
import 'package:flutter/material.dart';
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Nested Grid Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Nested Grid Demo'),
),
// Use a column so that outer grid has no pre-determined height.
body: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Divider(),
const Text('OuterGrid with nested InnerGrid:'),
const OuterGrid(),
const SizedBox(height: 30),
// Just to check the height calculation, wrap the inner grid in a colored container.
// Seems to be correct when not nested.
const Divider(),
const Text('InnerGrid not nested:'),
Container(color: Colors.amber, child: const InnerGrid()),
],
),
);
}
}
class OuterGrid extends StatelessWidget {
const OuterGrid({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return LayoutGrid(
areas: '''
r1
r2
''',
columnSizes: [1.fr],
rowSizes: const [auto, auto],
columnGap: 0,
// Note that the InnerGrid's Row 3 overflows into this gap.
rowGap: 30,
children: [
Container(
color: Colors.lightBlue,
child: const InnerGrid(),
).inGridArea('r1'),
Container(
color: Colors.lightGreen,
height: 200,
).inGridArea('r2'),
],
);
}
}
class InnerGrid extends StatelessWidget {
const InnerGrid({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return LayoutGrid(
areas: '''
r1
r2
r3
''',
columnSizes: [1.fr],
rowSizes: const [auto, auto, auto],
columnGap: 0,
rowGap: 10,
children: [
const Text('Row 1').inGridArea('r1'),
const Text('Row 2').inGridArea('r2'),
const Text('Row 3').inGridArea('r3'),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment