Skip to content

Instantly share code, notes, and snippets.

@letsar
Created May 6, 2020 13:13
Show Gist options
  • Save letsar/3a50b2eab1af33123e9b76e78087fcb7 to your computer and use it in GitHub Desktop.
Save letsar/3a50b2eab1af33123e9b76e78087fcb7 to your computer and use it in GitHub Desktop.
Simple code for a Gap Widget
class Gap extends LeafRenderObjectWidget {
const Gap(
this.mainAxisExtent, {
Key key,
}) : assert(mainAxisExtent != null &&
mainAxisExtent >= 0 &&
mainAxisExtent < double.infinity),
super(key: key);
final double mainAxisExtent;
@override
RenderObject createRenderObject(BuildContext context) {
return _RenderGap(mainAxisExtent: mainAxisExtent);
}
@override
void updateRenderObject(BuildContext context, _RenderGap renderObject) {
renderObject.mainAxisExtent = mainAxisExtent;
}
}
class _RenderGap extends RenderBox {
_RenderGap({
double mainAxisExtent,
}) : _mainAxisExtent = mainAxisExtent;
double get mainAxisExtent => _mainAxisExtent;
double _mainAxisExtent;
set mainAxisExtent(double value) {
if (_mainAxisExtent != value) {
_mainAxisExtent = value;
markNeedsLayout();
}
}
@override
void performLayout() {
final AbstractNode flex = parent;
if (flex is RenderFlex) {
if (flex.direction == Axis.horizontal) {
size = constraints.constrain(Size(mainAxisExtent, 0));
} else {
size = constraints.constrain(Size(0, mainAxisExtent));
}
} else {
throw FlutterError(
'A Gap widget must be placed directly inside a Flex widget',
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment