From fc65ba807ada392d1e57dfddd5eb2f5cf6dd9744 Mon Sep 17 00:00:00 2001 From: Kevin Gamboa Date: Mon, 27 Dec 2021 11:01:05 -0600 Subject: [PATCH] feat(null-safety): Null safety migration applied --- example/lib/main.dart | 16 ++++---- lib/src/spinning_wheel.dart | 74 ++++++++++++++++++------------------- lib/src/utils.dart | 6 +-- pubspec.lock | 69 ++++++++++++++++++---------------- pubspec.yaml | 2 +- 5 files changed, 87 insertions(+), 80 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index f3c8582..b5b2ae6 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -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), @@ -105,7 +105,7 @@ class Basic extends StatelessWidget { StreamBuilder( stream: _dividerController.stream, builder: (context, snapshot) => - snapshot.hasData ? BasicScore(snapshot.data) : Container(), + snapshot.hasData ? BasicScore(snapshot.data as int) : Container(), ) ], ), @@ -117,7 +117,7 @@ class Basic extends StatelessWidget { } class BasicScore extends StatelessWidget { - final int selected; + final int? selected; final Map labels = { 1: 'Purple', @@ -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)); } } @@ -176,7 +176,7 @@ class Roulette extends StatelessWidget { StreamBuilder( stream: _dividerController.stream, builder: (context, snapshot) => - snapshot.hasData ? RouletteScore(snapshot.data) : Container(), + snapshot.hasData ? RouletteScore(snapshot.data as int) : Container(), ), SizedBox(height: 30), new RaisedButton( @@ -196,7 +196,7 @@ class Roulette extends StatelessWidget { } class RouletteScore extends StatelessWidget { - final int selected; + final int? selected; final Map labels = { 1: '1000\$', @@ -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)); } } diff --git a/lib/src/spinning_wheel.dart b/lib/src/spinning_wheel.dart index 750226f..28344b2 100644 --- a/lib/src/spinning_wheel.dart +++ b/lib/src/spinning_wheel.dart @@ -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 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, @@ -83,7 +83,7 @@ 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(); @@ -91,19 +91,19 @@ class SpinningWheel extends StatefulWidget { class _SpinningWheelState extends State with SingleTickerProviderStateMixin { - AnimationController _animationController; - Animation _animation; + late AnimationController _animationController; + late Animation _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; @@ -112,28 +112,28 @@ class _SpinningWheelState extends State 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() { @@ -158,7 +158,7 @@ class _SpinningWheelState extends State }); if (widget.shouldStartOrStop != null) { - _subscription = widget.shouldStartOrStop.listen(_startOrStop); + _subscription = widget.shouldStartOrStop!.listen(_startOrStop); } } @@ -176,11 +176,11 @@ class _SpinningWheelState extends State 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; @@ -203,7 +203,7 @@ class _SpinningWheelState extends State child: Container(child: widget.image), builder: (context, child) { _updateAnimationValues(); - widget.onUpdate(_currentDivider); + widget.onUpdate?.call(_currentDivider); return Transform.rotate( angle: _initialSpinAngle + _currentDistance, child: child, @@ -232,9 +232,9 @@ class _SpinningWheelState extends State // 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 @@ -253,7 +253,7 @@ class _SpinningWheelState extends State } // 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; @@ -268,10 +268,10 @@ class _SpinningWheelState extends State _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; @@ -290,14 +290,14 @@ class _SpinningWheelState extends State _animationController.stop(); _animationController.reset(); - widget.onEnd(_currentDivider); + widget.onEnd?.call(_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; @@ -311,7 +311,7 @@ class _SpinningWheelState extends State void _startAnimation(Offset pixelsPerSecond) { var velocity = - _spinVelocity.getVelocity(_localPositionOnPanUpdate, pixelsPerSecond); + _spinVelocity.getVelocity(_localPositionOnPanUpdate!, pixelsPerSecond); _localPositionOnPanUpdate = null; _isBackwards = velocity < 0; @@ -328,7 +328,7 @@ class _SpinningWheelState extends State dispose() { _animationController.dispose(); if (_subscription != null) { - _subscription.cancel(); + _subscription!.cancel(); } super.dispose(); } diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 7296da1..ea87cf1 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -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); } @@ -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; diff --git a/pubspec.lock b/pubspec.lock index ec26bfa..c138f56 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: async: dependency: transitive @@ -7,28 +7,49 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.8.2" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.1.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" + version: "1.3.1" + clock: + dependency: transitive + description: + name: clock + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" + version: "1.15.0" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -45,35 +66,21 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.3+1" + version: "0.12.11" meta: dependency: "direct main" description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.7.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.2" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.dartlang.org" - source: hosted - version: "1.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.1" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -85,55 +92,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.4" + version: "1.8.1" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "1.6.8" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.2" + version: "0.4.3" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.1" sdks: - dart: ">=2.1.0 <3.0.0" + dart: ">=2.14.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index dc19d3e..515ad10 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,7 +7,7 @@ repository: https://github.com/davidanaya/flutter-spinning-wheel issue_tracker: https://github.com/davidanaya/flutter-spinning-wheel/issues environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: