Skip to content

Commit

Permalink
feat(null-safety): Null safety migration applied
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingamboa17 committed Dec 27, 2021
1 parent fa28203 commit 083412d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 78 deletions.
12 changes: 6 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class MyHomePage extends StatelessWidget {
);
}

Widget buildNavigationButton({String text, Function onPressedFn}) {
Widget buildNavigationButton({required String text, Function? onPressedFn}) {
return FlatButton(
color: Color.fromRGBO(255, 255, 255, 0.3),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
),
onPressed: onPressedFn,
onPressed: onPressedFn as void Function()?,
child: Text(
text,
style: TextStyle(color: Colors.white, fontSize: 18.0),
Expand Down Expand Up @@ -117,7 +117,7 @@ class Basic extends StatelessWidget {
}

class BasicScore extends StatelessWidget {
final int selected;
final int? selected;

final Map<int, String> labels = {
1: 'Purple',
Expand All @@ -132,7 +132,7 @@ class BasicScore extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Text('${labels[selected]}',
return Text('${labels[selected!]}',
style: TextStyle(fontStyle: FontStyle.italic));
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ class Roulette extends StatelessWidget {
}

class RouletteScore extends StatelessWidget {
final int selected;
final int? selected;

final Map<int, String> labels = {
1: '1000\$',
Expand All @@ -213,7 +213,7 @@ class RouletteScore extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Text('${labels[selected]}',
return Text('${labels[selected!]}',
style: TextStyle(fontStyle: FontStyle.italic, fontSize: 24.0));
}
}
74 changes: 37 additions & 37 deletions lib/src/spinning_wheel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,38 @@ class SpinningWheel extends StatefulWidget {
final bool canInteractWhileSpinning;

/// will be rendered on top of the wheel and can be used to show a selector
final Image secondaryImage;
final Image? secondaryImage;

/// x dimension for the secondaty image, if provided
/// if provided, has to be smaller than widget height
final double secondaryImageHeight;
final double? secondaryImageHeight;

/// y dimension for the secondary image, if provided
/// if provided, has to be smaller than widget width
final double secondaryImageWidth;
final double? secondaryImageWidth;

/// can be used to fine tune the position for the secondary image, otherwise it will be centered
final double secondaryImageTop;
final double? secondaryImageTop;

/// can be used to fine tune the position for the secondary image, otherwise it will be centered
final double secondaryImageLeft;
final double? secondaryImageLeft;

/// callback function to be executed when the wheel selection changes
final Function onUpdate;
final Function? onUpdate;

/// callback function to be executed when the animation stops
final Function onEnd;
final Function? onEnd;

/// Stream<double> used to trigger an animation
/// if triggered in an animation it will stop it, unless canInteractWhileSpinning is false
/// the parameter is a double for pixelsPerSecond in axis Y, which defaults to 8000.0 as a medium-high velocity
final Stream shouldStartOrStop;
final Stream? shouldStartOrStop;

SpinningWheel(
this.image, {
@required this.width,
@required this.height,
@required this.dividers,
required this.width,
required this.height,
required this.dividers,
this.initialSpinAngle: 0.0,
this.spinResistance: 0.5,
this.canInteractWhileSpinning: true,
Expand All @@ -83,27 +83,27 @@ class SpinningWheel extends StatefulWidget {
assert(spinResistance > 0.0 && spinResistance <= 1.0),
assert(initialSpinAngle >= 0.0 && initialSpinAngle <= (2 * pi)),
assert(secondaryImage == null ||
(secondaryImageHeight <= height && secondaryImageWidth <= width));
(secondaryImageHeight! <= height && secondaryImageWidth! <= width));

@override
_SpinningWheelState createState() => _SpinningWheelState();
}

class _SpinningWheelState extends State<SpinningWheel>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
late AnimationController _animationController;
late Animation<double> _animation;

// we need to store if has the widget behaves differently depending on the status
// AnimationStatus _animationStatus = AnimationStatus.dismissed;

// it helps calculating the velocity based on position and pixels per second velocity and angle
SpinVelocity _spinVelocity;
NonUniformCircularMotion _motion;
late SpinVelocity _spinVelocity;
late NonUniformCircularMotion _motion;

// keeps the last local position on pan update
// we need it onPanEnd to calculate in which cuadrant the user was when last dragged
Offset _localPositionOnPanUpdate;
Offset? _localPositionOnPanUpdate;

// duration of the animation based on the initial velocity
double _totalDuration = 0;
Expand All @@ -112,28 +112,28 @@ class _SpinningWheelState extends State<SpinningWheel>
double _initialCircularVelocity = 0;

// angle for each divider: 2*pi / numberOfDividers
double _dividerAngle;
double? _dividerAngle;

// current (circular) distance (angle) covered during the animation
double _currentDistance = 0;

// initial spin angle when the wheels starts the animation
double _initialSpinAngle;
late double _initialSpinAngle;

// dividider which is selected (positive y-coord)
int _currentDivider;
int? _currentDivider;

// spining backwards
bool _isBackwards;
late bool _isBackwards;

// if the user drags outside the wheel, won't be able to get back in
DateTime _offsetOutsideTimestamp;
DateTime? _offsetOutsideTimestamp;

// will be used to do transformations between global and local
RenderBox _renderBox;
RenderBox? _renderBox;

// subscription to the stream used to trigger an animation
StreamSubscription _subscription;
StreamSubscription? _subscription;

@override
void initState() {
Expand All @@ -158,7 +158,7 @@ class _SpinningWheelState extends State<SpinningWheel>
});

if (widget.shouldStartOrStop != null) {
_subscription = widget.shouldStartOrStop.listen(_startOrStop);
_subscription = widget.shouldStartOrStop!.listen(_startOrStop);
}
}

Expand All @@ -176,11 +176,11 @@ class _SpinningWheelState extends State<SpinningWheel>

double get topSecondaryImage =>
widget.secondaryImageTop ??
(widget.height / 2) - (widget.secondaryImageHeight / 2);
(widget.height / 2) - (widget.secondaryImageHeight! / 2);

double get leftSecondaryImage =>
widget.secondaryImageLeft ??
(widget.width / 2) - (widget.secondaryImageWidth / 2);
(widget.width / 2) - (widget.secondaryImageWidth! / 2);

double get widthSecondaryImage => widget.secondaryImageWidth ?? widget.width;

Expand All @@ -203,7 +203,7 @@ class _SpinningWheelState extends State<SpinningWheel>
child: Container(child: widget.image),
builder: (context, child) {
_updateAnimationValues();
widget.onUpdate(_currentDivider);
widget.onUpdate!(_currentDivider);
return Transform.rotate(
angle: _initialSpinAngle + _currentDistance,
child: child,
Expand Down Expand Up @@ -232,9 +232,9 @@ class _SpinningWheelState extends State<SpinningWheel>
// transforms from global coordinates to local and store the value
void _updateLocalPosition(Offset position) {
if (_renderBox == null) {
_renderBox = context.findRenderObject();
_renderBox = context.findRenderObject() as RenderBox?;
}
_localPositionOnPanUpdate = _renderBox.globalToLocal(position);
_localPositionOnPanUpdate = _renderBox!.globalToLocal(position);
}

/// returns true if (x,y) is outside the boundaries from size
Expand All @@ -253,7 +253,7 @@ class _SpinningWheelState extends State<SpinningWheel>
}
// calculate current divider selected
var modulo = _motion.modulo(_currentDistance + _initialSpinAngle);
_currentDivider = widget.dividers - (modulo ~/ _dividerAngle);
_currentDivider = widget.dividers - (modulo ~/ _dividerAngle) as int?;
if (_animationController.isCompleted) {
_initialSpinAngle = modulo;
_currentDistance = 0;
Expand All @@ -268,10 +268,10 @@ class _SpinningWheelState extends State<SpinningWheel>

_updateLocalPosition(details.globalPosition);

if (_contains(_localPositionOnPanUpdate)) {
if (_contains(_localPositionOnPanUpdate!)) {
// we need to update the rotation
// so, calculate the new rotation angle and rebuild the widget
var angle = _spinVelocity.offsetToRadians(_localPositionOnPanUpdate);
var angle = _spinVelocity.offsetToRadians(_localPositionOnPanUpdate!);
setState(() {
// initialSpinAngle will be added later on build
_currentDistance = angle - _initialSpinAngle;
Expand All @@ -290,14 +290,14 @@ class _SpinningWheelState extends State<SpinningWheel>
_animationController.stop();
_animationController.reset();

widget.onEnd(_currentDivider);
widget.onEnd!(_currentDivider);
}

void _startAnimationOnPanEnd(DragEndDetails details) {
if (!_userCanInteract) return;

if (_offsetOutsideTimestamp != null) {
var difference = DateTime.now().difference(_offsetOutsideTimestamp);
var difference = DateTime.now().difference(_offsetOutsideTimestamp!);
_offsetOutsideTimestamp = null;
// if more than 50 seconds passed since user dragged outside the boundaries, dont start animation
if (difference.inMilliseconds > 50) return;
Expand All @@ -311,7 +311,7 @@ class _SpinningWheelState extends State<SpinningWheel>

void _startAnimation(Offset pixelsPerSecond) {
var velocity =
_spinVelocity.getVelocity(_localPositionOnPanUpdate, pixelsPerSecond);
_spinVelocity.getVelocity(_localPositionOnPanUpdate!, pixelsPerSecond);

_localPositionOnPanUpdate = null;
_isBackwards = velocity < 0;
Expand All @@ -328,7 +328,7 @@ class _SpinningWheelState extends State<SpinningWheel>
dispose() {
_animationController.dispose();
if (_subscription != null) {
_subscription.cancel();
_subscription!.cancel();
}
super.dispose();
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class SpinVelocity {
double get width_0_5 => width / 2;
double get height_0_5 => height / 2;

SpinVelocity({@required this.height, @required this.width});
SpinVelocity({required this.height, required this.width});

double getVelocity(Offset position, Offset pps) {
var cuadrantIndex = _getCuadrantFromOffset(position);
var cuadrant = cuadrants[cuadrantIndex];
var cuadrant = cuadrants[cuadrantIndex]!;
return (cuadrant.dx * pps.dx) + (cuadrant.dy * pps.dy);
}

Expand Down Expand Up @@ -53,7 +53,7 @@ class SpinVelocity {
class NonUniformCircularMotion {
final double resistance;

NonUniformCircularMotion({@required this.resistance});
NonUniformCircularMotion({required this.resistance});

/// returns the acceleration based on the resistance provided in the constructor
double get acceleration => resistance * -7 * pi;
Expand Down
Loading

0 comments on commit 083412d

Please sign in to comment.