Skip to content

Commit c6bb7cb

Browse files
committed
add SmartDialogController
1 parent e09f8e9 commit c6bb7cb

File tree

7 files changed

+105
-7
lines changed

7 files changed

+105
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Add SmartAwaitOverType
55
- Fix [#56](https://github.com/fluttercandies/flutter_smart_dialog/issues/56)
66
- Fix [#60](https://github.com/fluttercandies/flutter_smart_dialog/issues/60)
7+
- Add SmartDialogController
78

89
# [4.0.3]
910

docs/Attach Chapter.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ SmartDialog.showAttach(
314314

315315
The above two business scenarios are very common. Sometimes, we need to be above or below the target widget or a specific area without being covered by a mask.
316316

317-
If you do it yourself, you can do it, but it will be cumbersome; now you can easily achieve this using the `highlight` parameter in `showAttach`
317+
If you do it yourself, you can do it, but it will be cumbersome; now you can easily achieve this using the `highlightBuilder` parameter in `showAttach`
318318

319319
> **Guide Actions**
320320
321321
Guidance operations are still very common in apps, and you need to specify the area to highlight, and then introduce its functions
322322

323-
- Using the `highlight` parameter in `showAttach` can also easily achieve this requirement, see the effect
323+
- Using the `highlightBuilder` parameter in `showAttach` can also easily achieve this requirement, see the effect
324324
- The code is also a little bit more, if you are interested, please check: [flutter_use](https://github.com/xdd666t/flutter_use)
325325

326326
![attachGuide](https://cdn.jsdelivr.net/gh/xdd666t/MyData@master/pic/flutter/blog/20220103224816.gif)

lib/flutter_smart_dialog.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export 'src/config/smart_config_toast.dart';
1010
/// dialog
1111
export 'src/init_dialog.dart';
1212
export 'src/smart_dialog.dart';
13+
export 'src/widget/dialog_scope.dart' show SmartDialogController;

lib/src/smart_dialog.dart

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'config/smart_config.dart';
77
import 'config/enum_config.dart';
88
import 'helper/dialog_proxy.dart';
99
import 'widget/attach_dialog_widget.dart';
10+
import 'widget/dialog_scope.dart';
1011

1112
class SmartDialog {
1213
/// Compatible with older versions
@@ -23,6 +24,8 @@ class SmartDialog {
2324
///
2425
/// [builder]:the custom dialog
2526
///
27+
/// [controller]:this controller can be used to refresh the layout of the custom dialog
28+
///
2629
/// [alignment]:control the location of the dialog
2730
///
2831
/// [clickMaskDismiss]:true(the dialog will be closed after click mask),false(not close)
@@ -71,6 +74,8 @@ class SmartDialog {
7174
///
7275
/// [builder]:自定义 dialog
7376
///
77+
/// [controller]:可使用该控制器来刷新自定义的dialog的布局
78+
///
7479
/// [alignment]:控制弹窗的位置
7580
///
7681
/// [clickMaskDismiss]:true(点击遮罩后,将关闭dialog),false(不关闭)
@@ -111,6 +116,7 @@ class SmartDialog {
111116
/// 绑定页面被关闭,被绑定在该页面上的dialog也将被移除
112117
static Future<T?> show<T>({
113118
required WidgetBuilder builder,
119+
SmartDialogController? controller,
114120
AlignmentGeometry? alignment,
115121
bool? clickMaskDismiss,
116122
bool? usePenetrate,
@@ -143,7 +149,10 @@ class SmartDialog {
143149
);
144150

145151
return DialogProxy.instance.show<T>(
146-
widget: Builder(builder: (context) => builder(context)),
152+
widget: DialogScope(
153+
controller: controller,
154+
builder: (context) => builder(context),
155+
),
147156
alignment: alignment ?? config.custom.alignment,
148157
clickMaskDismiss: clickMaskDismiss ?? config.custom.clickMaskDismiss,
149158
animationType: animationType ?? config.custom.animationType,
@@ -170,6 +179,8 @@ class SmartDialog {
170179
///
171180
/// [builder]:the custom dialog
172181
///
182+
/// [controller]:this controller can be used to refresh the layout of the custom dialog
183+
///
173184
/// [target]:target offset,when the target is set to value,
174185
/// the targetContext param will be invalid
175186
///
@@ -226,6 +237,8 @@ class SmartDialog {
226237
///
227238
/// [builder]:自定义 dialog
228239
///
240+
/// [controller]:可使用该控制器来刷新自定义的dialog的布局
241+
///
229242
/// [target]:target offset,当target被设置数据,targetContext参数将失效
230243
///
231244
/// [alignment]:控制弹窗的位置
@@ -271,6 +284,7 @@ class SmartDialog {
271284
static Future<T?> showAttach<T>({
272285
required BuildContext? targetContext,
273286
required WidgetBuilder builder,
287+
SmartDialogController? controller,
274288
Offset? target,
275289
AlignmentGeometry? alignment,
276290
bool? clickMaskDismiss,
@@ -311,7 +325,10 @@ class SmartDialog {
311325
var highlight = highlightBuilder;
312326
return DialogProxy.instance.showAttach<T>(
313327
targetContext: targetContext,
314-
widget: Builder(builder: (context) => builder(context)),
328+
widget: DialogScope(
329+
controller: controller,
330+
builder: (context) => builder(context),
331+
),
315332
target: target,
316333
alignment: alignment ?? config.attach.alignment,
317334
clickMaskDismiss: clickMaskDismiss ?? config.attach.clickMaskDismiss,
@@ -338,6 +355,8 @@ class SmartDialog {
338355
///
339356
/// [msg]:loading msg (Use the [builder] param, this param will be invalid)
340357
///
358+
/// [controller]:this controller can be used to refresh the layout of the custom loading
359+
///
341360
/// [clickMaskDismiss]:true(loading will be closed after click mask),false(not close)
342361
///
343362
/// [animationType]:For details, please refer to the [SmartAnimationType] comment
@@ -364,6 +383,8 @@ class SmartDialog {
364383
///
365384
/// [msg]:loading 的信息(使用[builder]参数,该参数将失效)
366385
///
386+
/// [controller]:可使用该控制器来刷新自定义的loading的布局
387+
///
367388
/// [clickMaskDismiss]:true(点击遮罩后,将关闭loading),false(不关闭)
368389
///
369390
/// [animationType]:具体可参照[SmartAnimationType]注释
@@ -384,6 +405,7 @@ class SmartDialog {
384405
/// [builder]:自定义loading
385406
static Future<T?> showLoading<T>({
386407
String msg = 'loading...',
408+
SmartDialogController? controller,
387409
bool? clickMaskDismiss,
388410
SmartAnimationType? animationType,
389411
bool? usePenetrate,
@@ -404,7 +426,10 @@ class SmartDialog {
404426
maskWidget: maskWidget ?? config.loading.maskWidget,
405427
backDismiss: backDismiss ?? config.loading.backDismiss,
406428
widget: builder != null
407-
? Builder(builder: (context) => builder(context))
429+
? DialogScope(
430+
controller: controller,
431+
builder: (context) => builder(context),
432+
)
408433
: DialogProxy.instance.loadingBuilder(msg),
409434
);
410435
}
@@ -413,6 +438,8 @@ class SmartDialog {
413438
///
414439
/// [msg]:msg presented to users (Use the [builder] param to customize the toast, this param will be invalid)
415440
///
441+
/// [controller]:this controller can be used to refresh the layout of the custom toast
442+
///
416443
/// [displayTime]:toast display time on the screen
417444
///
418445
/// [alignment]:control the location of the dialog
@@ -447,6 +474,8 @@ class SmartDialog {
447474
///
448475
/// [msg]:呈现给用户的信息(使用[builder]参数自定义toast,该参数将失效)
449476
///
477+
/// [controller]:可使用该控制器来刷新自定义的toast的布局
478+
///
450479
/// [displayTime]:toast在屏幕上的显示时间
451480
///
452481
/// [alignment]:控制弹窗的位置
@@ -474,6 +503,7 @@ class SmartDialog {
474503
/// [builder]:自定义toast
475504
static Future<void> showToast(
476505
String msg, {
506+
SmartDialogController? controller,
477507
Duration? displayTime,
478508
AlignmentGeometry? alignment,
479509
bool? clickMaskDismiss,
@@ -502,7 +532,10 @@ class SmartDialog {
502532
displayType: displayType ?? config.toast.displayType,
503533
consumeEvent: consumeEvent ?? config.toast.consumeEvent,
504534
widget: builder != null
505-
? Builder(builder: (context) => builder(context))
535+
? DialogScope(
536+
controller: controller,
537+
builder: (context) => builder(context),
538+
)
506539
: DialogProxy.instance.toastBuilder(msg),
507540
);
508541
}

lib/src/widget/dialog_scope.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_smart_dialog/src/util/view_utils.dart';
3+
4+
part 'smart_dialog_controller.dart';
5+
6+
class DialogScope extends StatefulWidget {
7+
const DialogScope({
8+
Key? key,
9+
required this.controller,
10+
required this.builder,
11+
}) : super(key: key);
12+
13+
final SmartDialogController? controller;
14+
15+
final WidgetBuilder builder;
16+
17+
@override
18+
State<DialogScope> createState() => _DialogScopeState();
19+
}
20+
21+
class _DialogScopeState extends State<DialogScope> {
22+
@override
23+
void initState() {
24+
widget.controller?._setListener(() {
25+
ViewUtils.addSafeUse(() => setState(() {}));
26+
});
27+
28+
super.initState();
29+
}
30+
31+
@override
32+
Widget build(BuildContext context) {
33+
return widget.builder(context);
34+
}
35+
36+
@override
37+
void dispose() {
38+
widget.controller?._dismiss();
39+
40+
super.dispose();
41+
}
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
part of 'dialog_scope.dart';
2+
3+
/// SmartDialog Controller
4+
class SmartDialogController {
5+
VoidCallback? _callback;
6+
7+
/// refresh dialog
8+
///
9+
/// 刷新dialog
10+
void refresh() {
11+
_callback?.call();
12+
}
13+
14+
void _setListener(VoidCallback? voidCallback) {
15+
_callback = voidCallback;
16+
}
17+
18+
void _dismiss() {
19+
_callback = null;
20+
}
21+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description:
33
An elegant Flutter Dialog solution,
44
Easily implement Toast, Loading and custom Dialog,
55
Make the use of the dialog easier!
6-
version: 4.3.2+1
6+
version: 4.3.5
77
homepage: https://github.com/fluttercandies/flutter_smart_dialog
88
#flutter pub publish --server=https://pub.dartlang.org
99

0 commit comments

Comments
 (0)