如何让SongDetailsBottomsheetController释放并在每次打开BottomSheet时重新初始化
class SongDetailsBottomsheet extends StatelessWidget {
final SongDetailsBottomsheetController controller;
SongDetailsBottomsheet({super.key, required MediaItem track})
: controller = Get.put(SongDetailsBottomsheetController(track: track));
@override
Widget build(BuildContext context) {
return const SizedBox(
height: 220,
child: Column(
children: [
SizedBox(width: 100,height: 100,)
],
),
);
}
}
class SongDetailsBottomsheetController extends GetxController {
// bool isLoading = true;
// late String avatarUrl;
final MediaItem track;
SongDetailsBottomsheetController({required this.track});
@override
void onInit() {
super.onInit();
debugPrint('start');
}
@override
void onReady() {
super.onReady();
debugPrint('ready');
// getArtistAvatar();
}
@override
onClose(){
super.onClose();
debugPrint('closing');
}
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) {
return SongDetailsBottomsheet(track: track);
});
}
当按下按钮时,弹出BottomSheet,但关闭时不会打印"closing",当再次打开时,也不会重新调用OnInit()和OnReady(),每次传入的MediaItem都不同,但SongDetailsBottomsheetController中的MediaItem不变
调试信息:
I/flutter (25437): start
I/flutter (25437): ready
W/WindowOnBackDispatcher(25437): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher(25437): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
W/WindowOnBackDispatcher(25437): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher(25437): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
W/WindowOnBackDispatcher(25437): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher(25437): Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
D/SystemUtils(25437): [isSystemApp] begin android.app.Application@da53904
D/ScreenUtils(25437): isMultiWindow:0,userId:0
D/ScreenUtils(25437): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(25437): isLittleVExist:0,userId:0
D/SystemUtils(25437): userId:0
尝试使用Get.bottomsheet(),但问题依然存在
方法1:
这是基于你的代码进行了一些修改
解决方法:
比如:我有一个HomepPage 而 HomepPage 是通过GetPage(name:'/homePage',page:HomePage)方法声在Getx的route中;当我在HomepPage下面的某个子组件中调用 showModalBottomSheet方法;其实 SongDetailsBottomsheetController 是被关联到 /homePage 这个路由下的;所以在HomePage这个界面释放的时候,SongDetailsBottomsheetController才被释放这个时候onClose 方法才会调用;所以你代码中onClose是不会执行的
方法2 使用StatefulWidget组件与GetController组合
方法3 使用 StatefulWidget 与 ValueNotifier