Created
August 7, 2018 05:46
-
-
Save gliheng/2cc5f97922d456052713fd0803a7efdc to your computer and use it in GitHub Desktop.
Absolute positioning with CustomSingleChildLayout
This file contains 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 'dart:math'; | |
import 'package:flutter/material.dart'; | |
class RadialSlider extends StatefulWidget { | |
double radius; | |
RadialSlider({ | |
this.radius, | |
}); | |
@override | |
_RadialSliderState createState() => _RadialSliderState(); | |
} | |
class _RadialSliderState extends State<RadialSlider> { | |
double angle = 60.0 / 180 * pi; | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
alignment: Alignment(0.0, 0.0), | |
children: <Widget>[ | |
Container( | |
width: widget.radius * 2 + 10, | |
height: widget.radius * 2 + 10, | |
decoration: BoxDecoration( | |
border: Border.all(), | |
shape: BoxShape.circle, | |
), | |
), | |
CustomSingleChildLayout( | |
delegate: _CicleLayoutDelegate( | |
angle: angle, | |
radius: widget.radius, | |
), | |
child: RaisedButton( | |
onPressed: () {}, | |
shape: CircleBorder(), | |
), | |
), | |
], | |
); | |
} | |
} | |
class _CicleLayoutDelegate extends SingleChildLayoutDelegate { | |
double radius; | |
double angle; | |
_CicleLayoutDelegate({this.radius, this.angle}); | |
@override | |
Offset getPositionForChild(Size size, Size childSize) { | |
var center = Offset( | |
(size.width - childSize.width) / 2, | |
(size.height - childSize.height) / 2, | |
); | |
return center.translate(radius * cos(angle), radius * sin(angle)); | |
} | |
@override | |
bool shouldRelayout(_CicleLayoutDelegate oldDelegate) { | |
return oldDelegate.angle != angle | |
|| oldDelegate.radius != radius; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment