From 42b666997e45eadac094e47d81089197eb14c1e0 Mon Sep 17 00:00:00 2001 From: Yan Wong Date: Wed, 31 Mar 2021 18:47:11 +0800 Subject: [PATCH] Migrate to null safety --- CHANGELOG.md | 4 + example/.gitignore | 3 + example/pubspec.yaml | 5 +- lib/config.dart | 24 ++-- lib/wave.dart | 101 ++++++++-------- pubspec.lock | 269 ++++++++++++++++++++++++++++++++++++++++--- pubspec.yaml | 7 +- test/wave_test.dart | 2 +- 8 files changed, 329 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba67184..e8d9335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.2.0 + +Migrate to null safety. + ## 0.1.0 Add backgroundImage param. diff --git a/example/.gitignore b/example/.gitignore index 47e0b4d..ccf55e5 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -69,3 +69,6 @@ build/ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages + +# Web related +web \ No newline at end of file diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 7a6f591..c08d7a6 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,7 +1,8 @@ name: wave_example description: An example application for Wave. author: Yan Wong -homepage: https://github.com/TheProtoss/wave/example +homepage: https://github.com/i-protoss/wave/tree/master/example +publish_to: none # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 @@ -22,7 +23,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^1.0.1+1 + cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: diff --git a/lib/config.dart b/lib/config.dart index 502bb49..4b160ca 100644 --- a/lib/config.dart +++ b/lib/config.dart @@ -14,7 +14,7 @@ enum ColorMode { } abstract class Config { - final ColorMode colorMode; + final ColorMode? colorMode; Config({this.colorMode}); @@ -25,21 +25,21 @@ abstract class Config { } class CustomConfig extends Config { - final List colors; - final List> gradients; - final Alignment gradientBegin; - final Alignment gradientEnd; - final List durations; - final List heightPercentages; - final MaskFilter blur; + final List? colors; + final List>? gradients; + final Alignment? gradientBegin; + final Alignment? gradientEnd; + final List? durations; + final List? heightPercentages; + final MaskFilter? blur; CustomConfig({ this.colors, this.gradients, this.gradientBegin, this.gradientEnd, - @required this.durations, - @required this.heightPercentages, + required this.durations, + required this.heightPercentages, this.blur, }) : assert(() { if (colors == null && gradients == null) { @@ -68,7 +68,9 @@ class CustomConfig extends Config { return true; }()), assert(() { - if (colors != null) { + if (colors != null && + durations != null && + heightPercentages != null) { if (colors.length != durations.length || colors.length != heightPercentages.length) { throw FlutterError( diff --git a/lib/wave.dart b/lib/wave.dart index 7dd3e19..51d97f7 100644 --- a/lib/wave.dart +++ b/lib/wave.dart @@ -214,14 +214,14 @@ class WaveWidget extends StatefulWidget { final double wavePhase; final double waveFrequency; final double heightPercentange; - final int duration; - final Color backgroundColor; - final DecorationImage backgroundImage; + final int? duration; + final Color? backgroundColor; + final DecorationImage? backgroundImage; final bool isLoop; WaveWidget({ - @required this.config, - @required this.size, + required this.config, + required this.size, this.waveAmplitude = 20.0, this.wavePhase = 10.0, this.waveFrequency = 1.6, @@ -237,17 +237,17 @@ class WaveWidget extends StatefulWidget { } class _WaveWidgetState extends State with TickerProviderStateMixin { - List _waveControllers; - List> _wavePhaseValues; + late List _waveControllers; + late List> _wavePhaseValues; List _waveAmplitudes = []; - Map, AnimationController> valueList; - Timer _endAnimationTimer; + Map, AnimationController>? valueList; + Timer? _endAnimationTimer; _initAnimations() { if (widget.config.colorMode == ColorMode.custom) { _waveControllers = - (widget.config as CustomConfig).durations.map((duration) { + (widget.config as CustomConfig).durations!.map((duration) { _waveAmplitudes.add(widget.waveAmplitude + 10); return AnimationController( vsync: this, duration: Duration(milliseconds: duration)); @@ -280,7 +280,8 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { // If isLoop is false, stop the animation after the specified duration. if (!widget.isLoop) { - _endAnimationTimer = Timer(Duration(milliseconds: widget.duration), () { + _endAnimationTimer = + Timer(Duration(milliseconds: widget.duration!), () { for (AnimationController waveController in _waveControllers) { waveController.stop(); } @@ -292,10 +293,10 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { _buildPaints() { List paints = []; if (widget.config.colorMode == ColorMode.custom) { - List _colors = (widget.config as CustomConfig).colors; - List> _gradients = (widget.config as CustomConfig).gradients; - Alignment begin = (widget.config as CustomConfig).gradientBegin; - Alignment end = (widget.config as CustomConfig).gradientEnd; + List? _colors = (widget.config as CustomConfig).colors; + List>? _gradients = (widget.config as CustomConfig).gradients; + Alignment? begin = (widget.config as CustomConfig).gradientBegin; + Alignment? end = (widget.config as CustomConfig).gradientEnd; for (int i = 0; i < _wavePhaseValues.length; i++) { paints.add( Container( @@ -306,7 +307,7 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { gradientBegin: begin, gradientEnd: end, heightPercentange: - (widget.config as CustomConfig).heightPercentages[i], + (widget.config as CustomConfig).heightPercentages![i], repaint: _waveControllers[i], waveFrequency: widget.waveFrequency, wavePhaseValue: _wavePhaseValues[i], @@ -357,12 +358,12 @@ class _WaveWidgetState extends State with TickerProviderStateMixin { /// Meta data of layer class Layer { - final Color color; - final List gradient; - final MaskFilter blur; - final Path path; - final double amplitude; - final double phase; + final Color? color; + final List? gradient; + final MaskFilter? blur; + final Path? path; + final double? amplitude; + final double? phase; Layer({ this.color, @@ -375,20 +376,20 @@ class Layer { } class _CustomWavePainter extends CustomPainter { - final ColorMode colorMode; - final Color color; - final List gradient; - final Alignment gradientBegin; - final Alignment gradientEnd; - final MaskFilter blur; + final ColorMode? colorMode; + final Color? color; + final List? gradient; + final Alignment? gradientBegin; + final Alignment? gradientEnd; + final MaskFilter? blur; - double waveAmplitude; + double? waveAmplitude; - Animation wavePhaseValue; + Animation? wavePhaseValue; - double waveFrequency; + double? waveFrequency; - double heightPercentange; + double? heightPercentange; double _tempA = 0.0; double _tempB = 0.0; @@ -406,7 +407,7 @@ class _CustomWavePainter extends CustomPainter { this.waveFrequency, this.wavePhaseValue, this.waveAmplitude, - Listenable repaint}) + Listenable? repaint}) : super(repaint: repaint); _setPaths(double viewCenterY, Size size, Canvas canvas) { @@ -415,37 +416,37 @@ class _CustomWavePainter extends CustomPainter { color: color, gradient: gradient, blur: blur, - amplitude: (-1.6 + 0.8) * waveAmplitude, - phase: wavePhaseValue.value * 2 + 30, + amplitude: (-1.6 + 0.8) * waveAmplitude!, + phase: wavePhaseValue!.value * 2 + 30, ); - _layer.path.reset(); - _layer.path.moveTo( + _layer.path!.reset(); + _layer.path!.moveTo( 0.0, viewCenterY + - _layer.amplitude * _getSinY(_layer.phase, waveFrequency, -1)); + _layer.amplitude! * _getSinY(_layer.phase!, waveFrequency!, -1)); for (int i = 1; i < size.width + 1; i++) { - _layer.path.lineTo( + _layer.path!.lineTo( i.toDouble(), viewCenterY + - _layer.amplitude * _getSinY(_layer.phase, waveFrequency, i)); + _layer.amplitude! * _getSinY(_layer.phase!, waveFrequency!, i)); } - _layer.path.lineTo(size.width, size.height); - _layer.path.lineTo(0.0, size.height); - _layer.path.close(); + _layer.path!.lineTo(size.width, size.height); + _layer.path!.lineTo(0.0, size.height); + _layer.path!.close(); if (_layer.color != null) { - _paint.color = _layer.color; + _paint.color = _layer.color!; } if (_layer.gradient != null) { var rect = Offset.zero & - Size(size.width, size.height - viewCenterY * heightPercentange); + Size(size.width, size.height - viewCenterY * heightPercentange!); _paint.shader = LinearGradient( begin: gradientBegin == null ? Alignment.bottomCenter - : gradientBegin, - end: gradientEnd == null ? Alignment.topCenter : gradientEnd, - colors: _layer.gradient) + : gradientBegin!, + end: gradientEnd == null ? Alignment.topCenter : gradientEnd!, + colors: _layer.gradient!) .createShader(rect); } if (_layer.blur != null) { @@ -453,12 +454,12 @@ class _CustomWavePainter extends CustomPainter { } _paint.style = PaintingStyle.fill; - canvas.drawPath(_layer.path, _paint); + canvas.drawPath(_layer.path!, _paint); } @override void paint(Canvas canvas, Size size) { - double viewCenterY = size.height * (heightPercentange + 0.1); + double viewCenterY = size.height * (heightPercentange! + 0.1); viewWidth = size.width; _setPaths(viewCenterY, size, canvas); } diff --git a/pubspec.lock b/pubspec.lock index 8426fcf..910c6f9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,55 +1,111 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _fe_analyzer_shared: + dependency: transitive + description: + name: _fe_analyzer_shared + url: "https://pub.dartlang.org" + source: hosted + version: "19.0.0" + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0-nullsafety.3" + version: "2.5.0" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" characters: dependency: transitive description: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.5" + version: "1.1.0" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" + cli_util: + dependency: transitive + description: + name: cli_util + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.0" clock: dependency: transitive description: name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" collection: dependency: transitive description: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0-nullsafety.5" + version: "1.15.0" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + coverage: + dependency: transitive + description: + name: coverage + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.0" flutter: dependency: "direct main" description: flutter @@ -60,87 +116,262 @@ packages: description: flutter source: sdk version: "0.0.0" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + http_multi_server: + dependency: transitive + description: + name: http_multi_server + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" + io: + dependency: transitive + description: + name: io + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10-nullsafety.3" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.6" + version: "1.3.0" + mime: + dependency: transitive + description: + name: mime + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + node_preamble: + dependency: transitive + description: + name: node_preamble + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.13" + package_config: + dependency: transitive + description: + name: package_config + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.3" + version: "1.8.0" + pedantic: + dependency: transitive + description: + name: pedantic + url: "https://pub.dartlang.org" + source: hosted + version: "1.11.0" + pool: + dependency: transitive + description: + name: pool + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.0" + pub_semver: + dependency: transitive + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + shelf: + dependency: transitive + description: + name: shelf + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + shelf_packages_handler: + dependency: transitive + description: + name: shelf_packages_handler + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + shelf_static: + dependency: transitive + description: + name: shelf_static + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + shelf_web_socket: + dependency: transitive + description: + name: shelf_web_socket + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" + source_map_stack_trace: + dependency: transitive + description: + name: source_map_stack_trace + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + source_maps: + dependency: transitive + description: + name: source_maps + url: "https://pub.dartlang.org" + source: hosted + version: "0.10.10" source_span: dependency: transitive description: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.0-nullsafety.4" + version: "1.8.0" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.10.0-nullsafety.6" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.3" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.1.0-nullsafety.3" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.2.0-nullsafety.3" + version: "1.2.0" + test: + dependency: "direct dev" + description: + name: test + url: "https://pub.dartlang.org" + source: hosted + version: "1.16.5" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.19-nullsafety.6" + version: "0.2.19" + test_core: + dependency: transitive + description: + name: test_core + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.15" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.0-nullsafety.5" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0-nullsafety.5" + version: "2.1.0" + vm_service: + dependency: transitive + description: + name: vm_service + url: "https://pub.dartlang.org" + source: hosted + version: "6.2.0" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + web_socket_channel: + dependency: transitive + description: + name: web_socket_channel + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + webkit_inspection_protocol: + dependency: transitive + description: + name: webkit_inspection_protocol + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" sdks: - dart: ">=2.12.0-0.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 68d5356..8a3adf3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ name: wave description: Widget for displaying waves with custom color, duration, floating and blur effects. -version: 0.1.0 +version: 0.2.0 homepage: https://github.com/i-protoss/wave environment: - sdk: ">=2.0.0-dev.68.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' dependencies: flutter: @@ -12,4 +12,5 @@ dependencies: dev_dependencies: flutter_test: - sdk: flutter \ No newline at end of file + sdk: flutter + test: ^1.16.5 diff --git a/test/wave_test.dart b/test/wave_test.dart index 44618db..c62ab85 100644 --- a/test/wave_test.dart +++ b/test/wave_test.dart @@ -28,7 +28,7 @@ void main() { }); } -Widget getWaveWidget({int duration, bool isLoop = true}) { +Widget getWaveWidget({int? duration, bool isLoop = true}) { return MaterialApp( home: Container( child: WaveWidget(