Skip to content

Commit

Permalink
null-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed May 5, 2021
1 parent 4e9b49b commit 8367077
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 92 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
sky_engine:
dependency: transitive
description: flutter
Expand Down
26 changes: 13 additions & 13 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'dart:async';
import 'package:flutter/material.dart';

abstract class DialogController<T>{
ValueNotifier<T> notifier;
Future<dynamic> result;
ValueNotifier<T>? notifier;
Future<dynamic>? result;

BuildContext context;
DialogController.of(this.context, this.notifier);
Expand All @@ -16,31 +16,31 @@ abstract class DialogController<T>{
}

class ProgressController extends DialogController<int> {
OverlayEntry entry;
OverlayEntry? entry;
ProgressController(BuildContext context, ValueNotifier<int> notifier, [this.entry]) : super.of(context, notifier);

open() {
Overlay.of(context).insert(entry);
Overlay.of(context)!.insert(entry!);
}

update(int value) {
notifier.value = value;
notifier!.value = value;
}

close() {
notifier.value = 101;
notifier!.value = 101;
}

remove() {
if(entry != null) {
entry.remove();
entry!.remove();
}
}
}

class ModalController extends DialogController<int> {

ModalController(BuildContext context, [ValueNotifier<int> notifier]) : super.of(context, notifier);
ModalController(BuildContext context, [ValueNotifier<int>? notifier]) : super.of(context, notifier);

open() {

Expand All @@ -58,25 +58,25 @@ class ModalController extends DialogController<int> {
}

class EntryController extends DialogController<int> {
OverlayEntry entry;
OverlayEntry? entry;

EntryController(BuildContext context, [ValueNotifier<int> notifier, this.entry])
EntryController(BuildContext context, [ValueNotifier<int>? notifier, this.entry])
: super.of(context, notifier);

open() {
Overlay.of(context).insert(entry);
Overlay.of(context)!.insert(entry!);
}

update(int value) {}

close() {
if(notifier != null)notifier.value = 101;
if(notifier != null)notifier!.value = 101;
else{
remove();
}
}

remove() {
entry.remove();
entry!.remove();
}
}
62 changes: 35 additions & 27 deletions lib/src/my_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,33 @@ class MyDialog {
static const alignTop = const Alignment(0.0, -0.7);
static const alignBottom = const Alignment(0.0, 0.7);

static const iconSuccess = const Icon(CupertinoIcons.checkmark_circle_fill, color: Colors.green,);
static const iconError = const Icon(CupertinoIcons.multiply_circle_fill, color: Colors.red);
static const iconWarning = const Icon(CupertinoIcons.exclamationmark_triangle_fill, color: Colors.deepOrangeAccent);
static const iconInfo = const Icon(CupertinoIcons.exclamationmark_circle_fill, color: Colors.blue);

Future<bool> confirm(message,
static const iconSuccess = const Icon(
CupertinoIcons.checkmark_circle_fill,
color: Colors.green,
);
static const iconError =
const Icon(CupertinoIcons.multiply_circle_fill, color: Colors.red);
static const iconWarning = const Icon(
CupertinoIcons.exclamationmark_triangle_fill,
color: Colors.deepOrangeAccent);
static const iconInfo = const Icon(CupertinoIcons.exclamationmark_circle_fill,
color: Colors.blue);

Future<bool?>? confirm(message,
{String buttonText = 'OK',
String title = '',
String cancelText = 'Cancel'}) {
ModalController controller;
late ModalController controller;
Completer completer = Completer<bool>();
controller = modal(
message is Widget
? message
: ListBody(children: message
.toString()
.split('\n')
.map<Widget>((item) => Text(item))
.toList()),
: ListBody(
children: message
.toString()
.split('\n')
.map<Widget>((item) => Text(item))
.toList()),
[
TextButton(
onPressed: () {
Expand All @@ -51,24 +59,24 @@ class MyDialog {
child: Text(buttonText)),
],
title: title,
);
) as ModalController;
controller.result = completer.future;

return controller.result;
return controller.result as Future<bool?>?;
}

Future<void> alert(message,
{String buttonText = 'OK', String title = ''}) {
ModalController controller;
Future<void> alert(message, {String buttonText = 'OK', String title = ''}) {
late ModalController controller;
Completer completer = Completer<bool>();
controller = modal(
message is Widget
? message
: ListBody(children: message
.toString()
.split('\n')
.map<Widget>((item) => Text(item))
.toList()),
: ListBody(
children: message
.toString()
.split('\n')
.map<Widget>((item) => Text(item))
.toList()),
[
ElevatedButton(
onPressed: () {
Expand All @@ -79,7 +87,7 @@ class MyDialog {
),
],
title: title,
);
) as ModalController;

return completer.future;
}
Expand Down Expand Up @@ -145,7 +153,7 @@ class MyDialog {
}

void toast(String message,
{int duration = 2, AlignmentGeometry align = alignTop, Icon icon}) {
{int duration = 2, Alignment align = alignTop, Icon? icon}) {
OverlayEntry entry = OverlayEntry(builder: (context) {
return ToastWidget(
message,
Expand All @@ -155,17 +163,17 @@ class MyDialog {
);
});

Overlay.of(context).insert(entry);
Overlay.of(context)!.insert(entry);
Future.delayed(Duration(seconds: duration ?? 2)).then((value) {
// 移除层可以通过调用OverlayEntry的remove方法。
entry.remove();
});
}

EntryController snack(String message,
{Widget action,
{Widget? action,
int duration = 3,
AlignmentGeometry align = alignBottom,
Alignment align = alignBottom,
double width = 0.7}) {
ValueNotifier<int> progressNotify = ValueNotifier<int>(0);
EntryController controller = EntryController(context, progressNotify);
Expand Down
38 changes: 19 additions & 19 deletions lib/src/popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'package:flutter/material.dart';
import 'controller.dart';

class PopupWidget extends StatefulWidget {
final Widget child;
final double height;
final Widget? child;
final double? height;
final double alpha;
final DialogController controller;
final DialogController? controller;

const PopupWidget(
{Key key, this.child, this.height, this.controller, this.alpha = 0.6})
{Key? key, this.child, this.height, this.controller, this.alpha = 0.6})
: super(key: key);

@override
Expand All @@ -18,13 +18,13 @@ class PopupWidget extends StatefulWidget {

class _PopupWidgetState extends State<PopupWidget>
with SingleTickerProviderStateMixin {
AnimationController _aniController;
double height = 0;
late AnimationController _aniController;
double? height = 0;
double curAlpha = 0;
Matrix4 transform = Matrix4.translationValues(0, 0, 0);

bool isShown = false;
DragStartDetails dragStart;
DragStartDetails? dragStart;

@override
void initState() {
Expand All @@ -35,8 +35,8 @@ class _PopupWidgetState extends State<PopupWidget>
_aniController.addListener(_onAnimation);
if (widget.height != null) height = widget.height;

widget.controller.notifier.addListener(_onController);
if (height > 0) {
widget.controller!.notifier!.addListener(_onController);
if (height! > 0) {
startShow();
}
}
Expand All @@ -53,39 +53,39 @@ class _PopupWidgetState extends State<PopupWidget>
if (isShown) return;
isShown = true;
setState(() {
_aniController.value = height;
transform = Matrix4.translationValues(0, height, 0);
_aniController.value = height!;
transform = Matrix4.translationValues(0, height!, 0);
});

_aniController.animateTo(0, curve: Curves.easeOutQuart);
}

void _onController() {
if (widget.controller.notifier.value == 101) {
if (widget.controller!.notifier!.value == 101) {
print('will close');
_close();
}
}

void _close() {
_aniController.animateTo(height, curve: Curves.easeOutQuart)
_aniController.animateTo(height!, curve: Curves.easeOutQuart)
..whenComplete(() {
widget.controller.notifier.removeListener(_onController);
widget.controller.remove();
widget.controller!.notifier!.removeListener(_onController);
widget.controller!.remove();
});
}

void _onAnimation() {
setState(() {
curAlpha = (1 - _aniController.value / height) * widget.alpha;
curAlpha = (1 - _aniController.value / height!) * widget.alpha;
transform = Matrix4.translationValues(0, _aniController.value, 0);
});
}

@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
if (height == null || height <= 0) {
if (height == null || height! <= 0) {
height = size.height * 0.8;
startShow();
}
Expand All @@ -107,9 +107,9 @@ class _PopupWidgetState extends State<PopupWidget>
//print(detail);
if (dragStart == null) return;
var offset =
detail.globalPosition.dy - dragStart.globalPosition.dy;
detail.globalPosition.dy - dragStart!.globalPosition.dy;
if (offset < 0) offset = 0;
_aniController.value = offset > height ? height : offset;
_aniController.value = offset > height! ? height! : offset;
_onAnimation();
},
onHorizontalDragStart: (detail) {
Expand Down
Loading

0 comments on commit 8367077

Please sign in to comment.