Skip to content

Instantly share code, notes, and snippets.

@gliheng
Created August 7, 2018 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gliheng/2cc5f97922d456052713fd0803a7efdc to your computer and use it in GitHub Desktop.
Save gliheng/2cc5f97922d456052713fd0803a7efdc to your computer and use it in GitHub Desktop.
Absolute positioning with CustomSingleChildLayout
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