getx
getx copied to clipboard
RouterReportManager base on _current to delete Controller is Not a good idae
when pageA pop dialogB use Get.bottomSheet to show a dialog ,RouterReportManager._current will change to dialogB ,if AController generate late than dialogB, AController will bind to dialogB; only dialogB Close , AController will call onClose and onDelete
it should find it really Route by context,then bind Controller key to route
Thank you for your report. Could you please provide a reproducible code snippet?
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp(
getPages: [
GetPage(
participatesInRootNavigator: true,
name: '/first',
page: () => const First()),
],
debugShowCheckedModeBanner: false,
);
}
}
class FirstController extends GetxController {
RxBool isShowedTip = RxBool(false);
@override
void onInit() async {
super.onInit();
}
@override
void onClose() {
print('on close first');
super.onClose();
}
}
class First extends StatefulWidget {
const First({Key? key}) : super(key: key);
@override
State<First> createState() => _FirstState();
}
class _FirstState extends State<First> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return GetBuilder<FirstController>(
init: FirstController(),
tag: "first_controller",
builder: (controller) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
Future.delayed(Duration(milliseconds: 100), () {
controller.isShowedTip.value = true;
});
Get.bottomSheet(
Container(
color: Colors.blue,
height: 100,
),
settings: RouteSettings(name: "test-route"),
);
},
child: Container(
width: 100,
height: 50,
color: Colors.green,
child: Center(
child: Text("show bottom sheet"),
),
),
),
Obx(() {
if (controller.isShowedTip.value) {
return ChildWidget();
} else {
return Center();
}
}),
],
),
);
});
}
}
class ChildWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetBuilder<ChildWidgetController>(
init: ChildWidgetController(),
builder: (controller) {
return Center(
child: Container(
width: 100,
height: 100,
color: Colors.blue,
child: Text("child view"),
),
);
},
);
}
}
class ChildWidgetController extends GetxController {
@override
void onInit() {
super.onInit();
}
@override
void onClose() {
print('on close child');
super.onClose();
}
}
run code in demo., click 'show bottom sheet' ,after bottom sheet show at least 2 second ,close bottoon sheet , console will print "on close child" ; the ChildWidgetController was bind to BottomSheet ,so it was deleted when BottomSheet dismiss. GetController shound not bind to top roure, it should bind to really route get by view context @jonataslaw