Created
May 3, 2020 08:56
-
-
Save sma/a377435ab091d4b3b5b367f20907cd29 to your computer and use it in GitHub Desktop.
A widget to add gaps to Row and Column widgets
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/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: Container( | |
width: 400, | |
color: Colors.amber, | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Text('Here is a message'), | |
Gap.large, | |
Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
Spacer(), | |
RaisedButton( | |
onPressed: () => null, | |
child: Text('Cancel'), | |
), | |
Gap.small, | |
RaisedButton( | |
onPressed: () => null, | |
child: Text('OK'), | |
), | |
], | |
), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class Gap extends SingleChildRenderObjectWidget { | |
const Gap({Key key, this.size = 16}) | |
: assert(size != null), | |
super(key: key); | |
final double size; | |
@override | |
RenderGap createRenderObject(BuildContext context) { | |
return RenderGap(size); | |
} | |
@override | |
void updateRenderObject(BuildContext context, RenderGap renderObject) { | |
renderObject.gapSize = size; | |
} | |
static const small = Gap(size: 8); | |
static const medium = Gap(size: 16); | |
static const large = Gap(size: 32); | |
} | |
class RenderGap extends RenderBox { | |
RenderGap(this._gapSize); | |
double _gapSize; | |
double get gapSize => _gapSize; | |
set gapSize(double gapSize) { | |
if (_gapSize != gapSize) { | |
_gapSize = gapSize; | |
markNeedsLayout(); | |
} | |
} | |
@override | |
void performLayout() { | |
final parent = this.parent; | |
if (parent is RenderFlex) { | |
if (parent.direction == Axis.horizontal) { | |
size = constraints.constrain(Size(_gapSize, 0)); | |
} else { | |
size = constraints.constrain(Size(0, _gapSize)); | |
} | |
} else { | |
throw FlutterError('Gap must be used inside a Flex widget'); | |
} | |
} | |
@override | |
double computeMinIntrinsicWidth(double width) { | |
final parent = this.parent; | |
if (parent is RenderFlex && parent.direction == Axis.horizontal) { | |
return gapSize; | |
} | |
return super.computeMinIntrinsicWidth(width); | |
} | |
@override | |
double computeMaxIntrinsicWidth(double width) { | |
return computeMinIntrinsicWidth(width); | |
} | |
@override | |
double computeMinIntrinsicHeight(double height) { | |
final parent = this.parent; | |
if (parent is RenderFlex && parent.direction == Axis.vertical) { | |
return gapSize; | |
} | |
return super.computeMinIntrinsicHeight(height); | |
} | |
@override | |
double computeMaxIntrinsicHeight(double height) { | |
return computeMaxIntrinsicHeight(height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment