Created
January 24, 2022 06:14
-
-
Save jogboms/eeb1e0643dce3349692a77a0a3a7ec8f to your computer and use it in GitHub Desktop.
A low-overhead Space widget for all Flex needs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/rendering.dart'; | |
import 'package:flutter/widgets.dart'; | |
class Space extends LeafRenderObjectWidget { | |
const Space(this.space, {Key? key}) : super(key: key); | |
final double space; | |
@override | |
RenderObject createRenderObject(BuildContext context) => RenderSpace(space: space); | |
@override | |
void updateRenderObject(BuildContext context, covariant RenderSpace renderObject) => renderObject.space = space; | |
} | |
class RenderSpace extends RenderBox { | |
RenderSpace({required double space}) : _space = space; | |
double get space => _space; | |
double _space; | |
set space(double space) { | |
if (space == _space) { | |
return; | |
} | |
_space = space; | |
markNeedsLayout(); | |
} | |
@override | |
void performLayout() { | |
final Axis? direction = (parent as RenderFlex?)?.direction; | |
assert(direction != null, 'Space should not be used outside of a Flex (Row, Column)'); | |
if (direction == null) { | |
size = Size.zero; | |
} else { | |
size = Size(direction == Axis.horizontal ? space : 0, direction == Axis.vertical ? space : 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment