Skip to content

Commit

Permalink
process #209
Browse files Browse the repository at this point in the history
  • Loading branch information
xdd666t committed Sep 1, 2024
1 parent 836d7ba commit a383fd5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# [4.9.8+x]
* SmartDialog.config.checkExist() adjust to SmartDialog.checkExist()
* Fix [#209](https://github.com/fluttercandies/flutter_smart_dialog/issues/209)

* # [4.9.7+x]
* optimize bindWidget, when bindWidget is not null, bindPage will be automatically set to false.
Expand Down
56 changes: 56 additions & 0 deletions example/lib/demo/issue209.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => _show(),
child: const Text('show'),
),
),
),
navigatorObservers: [FlutterSmartDialog.observer],
builder: FlutterSmartDialog.init(),
);
}

void _show() async {
print("before:${DateTime.now().millisecondsSinceEpoch}");
var result = await SmartDialog.show<bool>(
tag: 'tag',
backDismiss: false,
clickMaskDismiss: false,
builder: (_) {
return ElevatedButton(
onPressed: () {
SmartDialog.dismiss(tag: 'tag', result: true);
},
child: const Text('关闭Dialog,显示Loading'),
);
},
onDismiss: () {
print("onDismiss:${DateTime.now().millisecondsSinceEpoch}");
// showLoading()放在这里能正常显示
// SmartDialog.showLoading();
});

print("aftermiss:${DateTime.now().millisecondsSinceEpoch}");
if (result == true) {
// showLoading() 放在这里需要加一点延时才能显示Loading
// await Future.delayed(const Duration(seconds: 1));
SmartDialog.showLoading();

// 延时关闭Loading
await Future.delayed(const Duration(seconds: 2));
SmartDialog.dismiss();
}
}
}
10 changes: 5 additions & 5 deletions lib/src/custom/main_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,16 @@ class MainDialog {
Navigator.pop(DialogProxy.contextNavigator!);
}

await DialogProxy.instance.smartOverlayController.dismiss();

// safety await
await ViewUtils.awaitPostFrame();

//end waiting
_handleAwaitOver<T>(
awaitOverType: SmartAwaitOverType.dialogDismiss,
result: result,
);

await DialogProxy.instance.smartOverlayController.dismiss();

// safety await
await Future.delayed(const Duration(milliseconds: 20));
}

Widget getWidget() => Offstage(offstage: !visible, child: _widget);
Expand Down
28 changes: 27 additions & 1 deletion lib/src/kit/view_utils.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

Expand All @@ -14,6 +15,31 @@ class ViewUtils {
}
}

static Future<void> awaitSafeUse({VoidCallback? onPostFrame}) async {
final completer = Completer();
var schedulerPhase = schedulerBinding.schedulerPhase;
if (schedulerPhase == SchedulerPhase.persistentCallbacks) {
widgetsBinding.addPostFrameCallback((timeStamp) {
onPostFrame?.call();
if (!completer.isCompleted) completer.complete();
});
} else {
onPostFrame?.call();
if (!completer.isCompleted) completer.complete();
}

await completer.future;
}

static Future<void> awaitPostFrame({VoidCallback? onPostFrame}) async {
final completer = Completer();
widgetsBinding.addPostFrameCallback((timeStamp) {
onPostFrame?.call();
if (!completer.isCompleted) completer.complete();
});
await completer.future;
}

static bool isDarkModel() {
if (DialogProxy.contextNavigator == null) {
return false;
Expand Down
5 changes: 1 addition & 4 deletions lib/src/widget/helper/smart_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ class _SmartOverlayState extends State<SmartOverlay> {
if (showCompleter?.isCompleted == false) showCompleter?.complete();
showCompleter = Completer();

var completer = Completer();
ViewUtils.addSafeUse(() {
await ViewUtils.awaitSafeUse(onPostFrame: () {
setState(() => visible = true);
completer.complete();
});
await completer.future;

// await show isExist
widgetsBinding.addPostFrameCallback((timeStamp) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description:
An elegant Flutter Dialog solution,
Easily implement Toast, Loading and custom Dialog,
Make the use of the dialog easier!
version: 4.9.8
version: 4.9.8+1
homepage: https://github.com/fluttercandies/flutter_smart_dialog
# flutter pub publish --server=https://pub.dartlang.org
# flutter build web --release --base-href="/flutter_smart_dialog/web/"
Expand Down

0 comments on commit a383fd5

Please sign in to comment.