Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Jun 25, 2021
1 parent 2b12d9b commit 865532b
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 133 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.2.2]
* add MyDialog.getAlignment for get Alignment from string
* add comments

## [1.2.1]
* package & comments

Expand Down
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.2.0"
version: "1.2.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion lib/shirne_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library shirne_dialog;

export 'package:shirne_dialog/src/my_dialog.dart';
export 'package:shirne_dialog/src/controller.dart';
export 'package:shirne_dialog/src/controller.dart';
25 changes: 14 additions & 11 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'dart:async';

import 'package:flutter/material.dart';

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

Expand All @@ -20,7 +21,9 @@ abstract class DialogController<T>{
/// controller of [ProgressWidget]
class ProgressController extends DialogController<int> {
OverlayEntry? entry;
ProgressController(BuildContext context, ValueNotifier<int> notifier, [this.entry]) : super.of(context, notifier);
ProgressController(BuildContext context, ValueNotifier<int> notifier,
[this.entry])
: super.of(context, notifier);

open() {
Overlay.of(context)!.insert(entry!);
Expand All @@ -35,20 +38,18 @@ class ProgressController extends DialogController<int> {
}

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

/// controller of [MyDialog.modal]
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() {

}
open() {}

update(int value) {}

Expand All @@ -65,7 +66,8 @@ class ModalController extends DialogController<int> {
class EntryController extends DialogController<int> {
OverlayEntry? entry;

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

open() {
Expand All @@ -75,8 +77,9 @@ class EntryController extends DialogController<int> {
update(int value) {}

close() {
if(notifier != null)notifier!.value = 101;
else{
if (notifier != null)
notifier!.value = 101;
else {
remove();
}
}
Expand Down
63 changes: 45 additions & 18 deletions lib/src/my_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,54 @@ import 'progress.dart';
import 'snack.dart';
import 'toast.dart';

/// static class to call alert, confirm, toast etc.
class MyDialog {
BuildContext context;

/// construct with a [BuildContext]
MyDialog.of(this.context);

/// detault top position for [toast]
static const alignTop = const Alignment(0.0, -0.7);

/// default bottom position for [toast]
static const alignBottom = const Alignment(0.0, 0.7);

/// transform string to [Alignment] that use for [toast]
static Alignment getAlignment(String align) {
switch (align.toLowerCase()) {
case "top":
case "topcenter":
return alignTop;
case "center":
return Alignment.center;
default:
return alignBottom;
}
}

/// success icon for [toast]
static const iconSuccess = const Icon(
CupertinoIcons.checkmark_circle_fill,
color: Colors.green,
);

/// error icon for [toast]
static const iconError =
const Icon(CupertinoIcons.multiply_circle_fill, color: Colors.red);

/// warning icon for [toast]
static const iconWarning = const Icon(
CupertinoIcons.exclamationmark_triangle_fill,
color: Colors.deepOrangeAccent);

/// info icon for [toast]
static const iconInfo = const Icon(CupertinoIcons.exclamationmark_circle_fill,
color: Colors.blue);

/// show a confirm Modal box.
/// the `message` may be a [Widget] or [String]
Future<bool?>? confirm(message,
/// the [message] may be a [Widget] or [String]
Future<bool?>? confirm(dynamic message,
{String buttonText = 'OK',
String title = '',
String cancelText = 'Cancel'}) {
Expand Down Expand Up @@ -120,19 +145,21 @@ class MyDialog {
}

/// show a modal popup with `body` witch width will fill the screen
DialogController popup(Widget body, {
barrierDismissible = false,
double height = 0,
double borderRound = 10,
EdgeInsetsGeometry padding = const EdgeInsets.all(10),
Color barrierColor: const Color.fromRGBO(0, 0, 0, .6),
Color backgroundColor: Colors.white,
bool isDismissible: true,
bool isScrollControlled: false,
double? elevation,
bool showClose = true,
Widget closeButton = const Icon(Icons.cancel, color: Colors.black38,)
}) {
DialogController popup(Widget body,
{barrierDismissible = false,
double height = 0,
double borderRound = 10,
EdgeInsetsGeometry padding = const EdgeInsets.all(10),
Color barrierColor: const Color.fromRGBO(0, 0, 0, .6),
Color backgroundColor: Colors.white,
bool isDismissible: true,
bool isScrollControlled: false,
double? elevation,
bool showClose = true,
Widget closeButton = const Icon(
Icons.cancel,
color: Colors.black38,
)}) {
ModalController controller = ModalController(context);
controller.result = showModalBottomSheet<dynamic>(
backgroundColor: Colors.transparent,
Expand All @@ -144,9 +171,9 @@ class MyDialog {
builder: (BuildContext context) {
return PopupWidget(
child: body,
height:height,
borderRound:borderRound,
backgroundColor:backgroundColor,
height: height,
borderRound: borderRound,
backgroundColor: backgroundColor,
padding: padding,
controller: controller,
showClose: showClose,
Expand Down
53 changes: 26 additions & 27 deletions lib/src/popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,35 @@ class PopupWidget extends StatefulWidget {
final Widget closeButton;
final Color backgroundColor;

const PopupWidget(
{Key? key,
this.child,
this.height,
this.controller,
this.borderRound = 10,
this.padding = const EdgeInsets.all(10),
this.backgroundColor = Colors.white,
this.showClose = true,
this.closeButton = const Icon(Icons.cancel),
})
: super(key: key);
const PopupWidget({
Key? key,
this.child,
this.height,
this.controller,
this.borderRound = 10,
this.padding = const EdgeInsets.all(10),
this.backgroundColor = Colors.white,
this.showClose = true,
this.closeButton = const Icon(Icons.cancel),
}) : super(key: key);

@override
State<StatefulWidget> createState() => _PopupWidgetState();
}

class _PopupWidgetState extends State<PopupWidget>{
class _PopupWidgetState extends State<PopupWidget> {
double height = 0;

@override
void initState() {
super.initState();
if (widget.height != null) height = widget.height!;

if(widget.controller!.notifier != null) {
if (widget.controller!.notifier != null) {
widget.controller!.notifier!.addListener(_onController);
}
}



void _onController() {
if (widget.controller!.notifier!.value == 101) {
print('will close');
Expand All @@ -55,7 +52,7 @@ class _PopupWidgetState extends State<PopupWidget>{
}

void _close() {
if(widget.controller!.notifier != null) {
if (widget.controller!.notifier != null) {
widget.controller!.notifier!.removeListener(_onController);
}
widget.controller!.remove();
Expand All @@ -68,7 +65,7 @@ class _PopupWidgetState extends State<PopupWidget>{
if (height <= 0) {
// 按具体高度
height = size.height * 0.8;
}else if(height < 1){
} else if (height < 1) {
// 按屏高百分比
height = size.height * height;
}
Expand All @@ -91,15 +88,17 @@ class _PopupWidgetState extends State<PopupWidget>{
),
Align(
alignment: Alignment.topRight,
child: widget.showClose ? GestureDetector(
onTap: () {
_close();
},
child: Padding(
padding: EdgeInsets.all(8),
child: widget.closeButton,
),
) : null,
child: widget.showClose
? GestureDetector(
onTap: () {
_close();
},
child: Padding(
padding: EdgeInsets.all(8),
child: widget.closeButton,
),
)
: null,
)
],
),
Expand Down
32 changes: 17 additions & 15 deletions lib/src/progress.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:flutter/material.dart';

import 'controller.dart';


/// a progress Widget
class ProgressWidget extends StatefulWidget {
final ValueNotifier<int>? notifier;
Expand All @@ -16,39 +15,44 @@ class ProgressWidget extends StatefulWidget {
final DialogController? controller;

const ProgressWidget(
{Key? key, this.notifier, this.showProgress = false, this.message, this.controller, this.showOverlay = true})
{Key? key,
this.notifier,
this.showProgress = false,
this.message,
this.controller,
this.showOverlay = true})
: super(key: key);

@override
State<StatefulWidget> createState() => _ProgressWidgetState();
}

class _ProgressWidgetState extends State<ProgressWidget> with SingleTickerProviderStateMixin {
class _ProgressWidgetState extends State<ProgressWidget>
with SingleTickerProviderStateMixin {
int progress = 0;
AnimationController? _aniController;

@override
void initState() {
super.initState();


if (widget.notifier != null) {
widget.notifier!.addListener(_onValueChange);

if(widget.showProgress){
if (widget.showProgress) {
_aniController = AnimationController.unbounded(
vsync: this, duration: Duration(milliseconds: 400));
_aniController!.value = progress/100.0;
_aniController!.value = progress / 100.0;
_aniController!.addListener(_onAnimation);
}
}
}

@override
void dispose() {
if (widget.notifier != null ) {
if (widget.notifier != null) {
widget.notifier!.removeListener(_onValueChange);
if(_aniController != null){
if (_aniController != null) {
_aniController!.removeListener(_onAnimation);
_aniController!.dispose();
}
Expand All @@ -57,9 +61,7 @@ class _ProgressWidgetState extends State<ProgressWidget> with SingleTickerProvid
}

void _onAnimation() {
setState(() {

});
setState(() {});
}

@override
Expand Down Expand Up @@ -93,18 +95,18 @@ class _ProgressWidgetState extends State<ProgressWidget> with SingleTickerProvid
setState(() {
progress = widget.notifier!.value;
});
if(_aniController != null) {
if (_aniController != null) {
_aniController!.animateTo(progress / 100.0, curve: Curves.easeOutQuart)
..whenComplete(() {
if (progress >= 100) {
widget.controller!.remove();
}
});
}else{
} else {
if (progress >= 100) {
Future.delayed(Duration(milliseconds: 200)).then((v){
Future.delayed(Duration(milliseconds: 200)).then((v) {
widget.controller!.remove();
});
});
}
}
}
Expand Down
Loading

0 comments on commit 865532b

Please sign in to comment.