Skip to content

Instantly share code, notes, and snippets.

@roipeker
Last active December 28, 2023 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roipeker/460cff051156bc913c84a17af278a660 to your computer and use it in GitHub Desktop.
Save roipeker/460cff051156bc913c84a17af278a660 to your computer and use it in GitHub Desktop.
test for removing GetX dialogs with removeRoute + extension methods.
/// Copyright roipeker 2020.
// Web Sample:
// https://roi-getx-kill-dialog.surge.sh/
import 'package:flutter/material.dart';
import 'package:get/get.dart';
/// proof of concept
/// execute in main.dart:
/// main() => OverlayDemo.main();
class OverlayDemo {
static void main() {
Get.put(MyController());
runApp(
GetMaterialApp(
debugShowCheckedModeBanner: false,
home: _OverlayDemo(),
),
);
}
}
class MyDialog extends StatelessWidget {
final int id;
final int total;
MyDialog({
this.id,
this.total,
});
@override
Widget build(BuildContext context) {
context.addRouteKey(id);
return Padding(
padding: EdgeInsets.only(top: 64.0 * id),
child: Dialog(
backgroundColor: Colors.redAccent,
elevation: 2,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Dialog $id',
style: Get.textTheme.headline4,
),
...List.generate(total, _button),
],
),
),
);
}
MyController get myController => Get.find();
void _killRoute(int routeId) {
final removed = Get.killRoute(routeId);
if (removed) {
myController.removedRouteId(routeId);
}
}
Widget _button(int routeId) {
return Obx(
() {
final exists = myController.routeExists(routeId);
final textStyle =
!exists ? TextStyle(decoration: TextDecoration.lineThrough) : null;
return FlatButton(
onPressed: exists ? () => _killRoute(routeId) : null,
child: Text(
'dialog $routeId',
style: textStyle,
),
);
},
);
}
}
class MyController extends GetxController {
final reactiveList = <bool>[].obs;
@override
void onReady() {
print(';ready');
}
Future<void> press() async {
final numDialogs = 8;
reactiveList.clear();
reactiveList.addAll(List.generate(numDialogs, (index) => true));
for (var i = 0; i < numDialogs; ++i) {
Get.dialog(
MyDialog(id: i, total: numDialogs),
barrierDismissible: false,
barrierColor: Colors.black12,
transitionCurve: Curves.easeOutExpo,
transitionDuration: .3.seconds,
);
await .35.seconds.delay();
}
// await .5.seconds.delay();
// _killRoute(1);
// await .5.seconds.delay();
// _killRoute(4);
// await .5.seconds.delay();
// _killRoute(3);
}
void _killRoute(int i) {
Get.killRoute(i);
removedRouteId(i);
}
void removedRouteId(int routeId) {
reactiveList[routeId] = false;
}
bool routeExists(int routeId) {
return reactiveList[routeId];
}
}
class _OverlayDemo extends GetView<MyController> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FlatButton(
onPressed: controller.press,
child: Text('open'),
),
));
}
}
/// EXTENSION
///
extension MyContextExt on BuildContext {
void addRouteKey(int key) => DialogManager.add(key, ModalRoute.of(this));
}
extension MyDialogExt on GetInterface {
void addRouteKey(int key, Route route) => DialogManager.add(key, route);
bool killRoute(int key) => DialogManager.remove(key);
}
class DialogManager {
static var _maps = <int, Route>{};
static void add(int key, Route route) => _maps[key] = route;
static bool remove(int key) {
if (!_maps.containsKey(key)) return false;
navigator.removeRoute(_maps[key]);
_maps.remove(key);
return true;
}
}
@roipeker
Copy link
Author

roipeker commented Oct 8, 2020

@manojeeva
Copy link

This is another level.
Soooo cool.
This is Prefect.
This is what Looking for.
This should fix almost all dialog issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment