Skip to content

Commit

Permalink
Reconstruct the implementation of initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
xdd666t committed May 19, 2024
1 parent e6ca0f8 commit 5c2dac7
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 77 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* break change: "AlignmentGeometry" adjust to "Alignment"
* fix [#185](https://github.com/fluttercandies/flutter_smart_dialog/issues/185)
* optimize bindWidget, when bindWidget is not null, bindPage will be automatically set to false.
* Reconstruct the implementation of initialization (fix [#109](https://github.com/fluttercandies/flutter_smart_dialog/issues/109), [#183](https://github.com/fluttercandies/flutter_smart_dialog/issues/183))



# [4.9.0]
Expand Down
4 changes: 2 additions & 2 deletions example/lib/demo/issue181_overstep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class _TestingDialogState extends State<_TestingDialog>
InkWell(
onTap: () {
SmartDialog.dismiss();
print("111111111111");
debugPrint("111111111111");
},
child: const Row(children: [
Icon(Icons.close),
Expand All @@ -111,7 +111,7 @@ class _TestingDialogState extends State<_TestingDialog>
InkWell(
onTap: () {
SmartDialog.dismiss();
print("2222222222222");
debugPrint("2222222222222");
},
child: const Row(children: [
Icon(Icons.close),
Expand Down
144 changes: 144 additions & 0 deletions example/lib/demo/issue183_visibility.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:visibility_detector/visibility_detector.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
navigatorObservers: [FlutterSmartDialog.observer],
builder: FlutterSmartDialog.init(), //这里注释掉就不会有问题
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
final int _counter = 0;

void _incrementCounter() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Page1()),
);
}

@override
Widget build(BuildContext context) {
debugPrint("rebuild");
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Column(
children: [
Expanded(
child: VisibilityDetector(
key: const Key("rec"),
onVisibilityChanged: (VisibilityInfo visibilityInfo) {
var visiblePercentage = visibilityInfo.visibleFraction * 100;
if (visiblePercentage == 100.0) {
debugPrint("推荐页面111");
}
},
child: PageView(
physics: const NeverScrollableScrollPhysics(),
children: <Widget>[
VisibilityDetector(
key: const Key("reca"),
onVisibilityChanged: (VisibilityInfo visibilityInfo) {
var visiblePercentage =
visibilityInfo.visibleFraction * 100;
if (visiblePercentage == 100.0) {
debugPrint("首页首页");
}
},
child: SizedBox(
width: 300,
// color: Colors.red,
child: Column(
children: [
const Text(
'You have pushed the button this many times:',
),
InkWell(
onTap: () {
_incrementCounter();
},
child: const Text("dfdfd"),
)
],
),
),
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
),
Container(width: 200, height: 40, color: Colors.red)
],
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

class Page1 extends StatelessWidget {
const Page1({super.key});

@override
Widget build(BuildContext context) {
// TODO: implement build
return PageView(
children: const [
Center(
child: Text(
"page1",
style: TextStyle(color: Colors.red),
),
)
],
);
}
}
2 changes: 2 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
# 用于处理widget是否可见检测 https://pub.flutter-io.cn/packages/visibility_detector
visibility_detector:
# mac
flutter_smart_dialog:
path: ../
Expand Down
2 changes: 2 additions & 0 deletions lib/src/custom/main_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class MainDialog {
awaitOverType: SmartAwaitOverType.dialogDismiss,
result: result,
);

DialogProxy.instance.smartOverlayController.dismiss();
}

Widget getWidget() => Offstage(offstage: !visible, child: _widget);
Expand Down
20 changes: 12 additions & 8 deletions lib/src/custom/toast/toast_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ class ToastTool {
return;
}

if (closeAll) {
clearAllToast();
SmartDialog.config.toast.isExist = false;
return;
}

var curToast = toastQueue.first;
toastQueue.remove(curToast);
if (toastQueue.isEmpty) {
SmartDialog.config.toast.isExist = false;
}

await curToast.mainDialog.dismiss();
await Future.delayed(SmartDialog.config.toast.intervalTime);
if (curToast.mainDialog.overlayEntry.mounted) {
curToast.mainDialog.overlayEntry.remove();
}

toastQueue.remove(curToast);
if (closeAll) {
clearAllToast();
}
if (toastQueue.length > 1) return;
SmartDialog.config.toast.isExist = false;
await Future.delayed(SmartDialog.config.toast.intervalTime);
}

void clearAllToast() {
Expand Down
41 changes: 25 additions & 16 deletions lib/src/helper/dialog_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import '../config/smart_config.dart';
import '../data/animation_param.dart';
import '../data/notify_info.dart';
import '../init_dialog.dart';
import '../widget/helper/smart_overlay.dart';
import '../widget/helper/smart_overlay_entry.dart';

enum CloseType {
Expand Down Expand Up @@ -45,10 +46,10 @@ enum DialogType {
class DialogProxy {
late SmartConfig config;
late SmartOverlayEntry entryLoading;
late SmartOverlayEntry entryNotify;
late Queue<DialogInfo> dialogQueue;
late Queue<NotifyInfo> notifyQueue;
late CustomLoading _loading;
late CustomLoading loadingWidget;
late SmartOverlayController smartOverlayController;

static DialogProxy? _instance;

Expand All @@ -71,18 +72,17 @@ class DialogProxy {

DialogProxy._internal() {
config = SmartConfig();
smartOverlayController = SmartOverlayController();
dialogQueue = ListQueue();
notifyQueue = ListQueue();
}

void initialize(Set<SmartInitType> initType) {
if (initType.contains(SmartInitType.loading)) {
entryLoading = SmartOverlayEntry(builder: (_) => _loading.getWidget());
_loading = CustomLoading(overlayEntry: entryLoading);
}

if (initType.contains(SmartInitType.notify)) {
entryNotify = SmartOverlayEntry(builder: (_) => const SizedBox.shrink());
entryLoading = SmartOverlayEntry(builder: (_) {
return loadingWidget.getWidget();
});
loadingWidget = CustomLoading(overlayEntry: entryLoading);
}
}

Expand Down Expand Up @@ -110,7 +110,8 @@ class DialogProxy {
required bool bindPage,
required BuildContext? bindWidget,
required Rect? ignoreArea,
}) {
}) async {
await beforeShow();
CustomDialog? dialog;
var entry = SmartOverlayEntry(
builder: (BuildContext context) => dialog!.getWidget(),
Expand Down Expand Up @@ -162,7 +163,8 @@ class DialogProxy {
required String? tag,
required bool keepSingle,
required SmartBackType backType,
}) {
}) async {
await beforeShow();
CustomNotify? dialog;
var entry = SmartOverlayEntry(
builder: (BuildContext context) => dialog!.getWidget(),
Expand Down Expand Up @@ -219,7 +221,8 @@ class DialogProxy {
required bool useSystem,
required bool bindPage,
required BuildContext? bindWidget,
}) {
}) async {
await beforeShow();
CustomDialog? dialog;
var entry = SmartOverlayEntry(
builder: (BuildContext context) => dialog!.getWidget(),
Expand Down Expand Up @@ -273,8 +276,9 @@ class DialogProxy {
required Duration? displayTime,
required bool backDismiss,
required Widget widget,
}) {
return _loading.showLoading<T>(
}) async {
await beforeShow();
return loadingWidget.showLoading<T>(
alignment: alignment,
clickMaskDismiss: clickMaskDismiss,
animationType: animationType,
Expand Down Expand Up @@ -311,7 +315,8 @@ class DialogProxy {
required bool debounce,
required SmartToastType displayType,
required Widget widget,
}) {
}) async {
await beforeShow();
CustomToast? toast;
var entry = SmartOverlayEntry(
builder: (BuildContext context) => toast!.getWidget(),
Expand All @@ -337,6 +342,10 @@ class DialogProxy {
);
}

Future<void> beforeShow() async {
await smartOverlayController.show();
}

Future<void>? dismiss<T>({
required SmartStatus status,
String? tag,
Expand All @@ -349,7 +358,7 @@ class DialogProxy {

if (loading &&
(tag == null || (dialogQueue.isEmpty && notifyQueue.isEmpty))) {
return _loading.dismiss(closeType: closeType);
return loadingWidget.dismiss(closeType: closeType);
}

if (notifyQueue.isNotEmpty) {
Expand Down Expand Up @@ -386,7 +395,7 @@ class DialogProxy {
} else if (status == SmartStatus.allToast) {
return CustomToast.dismiss(closeAll: true);
} else if (status == SmartStatus.loading) {
return _loading.dismiss(closeType: closeType);
return loadingWidget.dismiss(closeType: closeType);
} else if (status == SmartStatus.notify ||
status == SmartStatus.allNotify) {
return CustomNotify.dismiss<T>(
Expand Down
Loading

0 comments on commit 5c2dac7

Please sign in to comment.