Skip to content

Instantly share code, notes, and snippets.

@shyndman
Last active February 24, 2021 03:02
Show Gist options
  • Select an option

  • Save shyndman/4b52b4f5bcb2e4ba340adef6e69c8bc3 to your computer and use it in GitHub Desktop.

Select an option

Save shyndman/4b52b4f5bcb2e4ba340adef6e69c8bc3 to your computer and use it in GitHub Desktop.
Example of looking up a LayoutGrid's associated RenderLayoutGrid
import 'package:flutter/material.dart';
import 'package:flutter_layout_grid/flutter_layout_grid.dart';
import 'package:flutter_layout_grid/src/rendering/layout_grid.dart';
void main() {
runApp(FindRenderLayoutGridExample());
}
class FindRenderLayoutGridExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return WidgetsApp(
title: 'RenderLayoutGrid example',
debugShowCheckedModeBanner: false,
color: Colors.white,
builder: (context, child) => FindRenderLayoutGrid(),
);
}
}
const rowCount = 5;
const colCount = 5;
// This doesn't have to be a StatefulWidget, but in your case, you're likely
// going to want one so that you can store the sizes that you retrieve from the
// render objects.
class FindRenderLayoutGrid extends StatefulWidget {
@override
_FindRenderLayoutGridState createState() => _FindRenderLayoutGridState();
}
class _FindRenderLayoutGridState extends State<FindRenderLayoutGrid> {
// This key is supplied to a Widget, and allows us to look up its associated
// Element and RenderObject (which is where the real magic happens).
//
// In our case, we're supplying it to the LayoutGrid widget.
final _gridKey = GlobalKey();
@override
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((_) {
// Grab the render object associated with the widget we supplied the key
// to.
final renderGrid =
_gridKey.currentContext.findRenderObject() as RenderLayoutGrid;
for (int i = 0; i < rowCount; i++) {
final rowArea = GridArea(
rowStart: i,
rowEnd: i + 1,
columnStart: 0,
columnEnd: colCount,
);
final rowSize = renderGrid.lastGridSizing.sizeForArea(rowArea);
print('row $i height is ${rowSize.height}');
}
});
final style = TextStyle(fontFamily: 'Roboto', color: Colors.black87);
return LayoutGrid(
// Supplied here!
key: _gridKey,
columnSizes: repeat(colCount, [auto]),
rowSizes: repeat(rowCount, [auto]),
children: [
for (int i = 0; i < rowCount * colCount; i++) Text('$i', style: style),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment