use bottom sheet controller

This commit is contained in:
Fabian Freund
2025-08-24 07:51:29 +02:00
parent 079f8d02f0
commit 2a6f3c8ba8
@@ -57,9 +57,13 @@ class BrowserScreen extends HookConsumerWidget {
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final scaffoldKey = useMemoized(() => GlobalKey<ScaffoldState>());
final eventService = ref.watch(eventServiceProvider); final eventService = ref.watch(eventServiceProvider);
final displayedSheet = ref.watch(bottomSheetControllerProvider); final sheetDisplayed = ref.watch(
bottomSheetControllerProvider.select((value) => value != null),
);
final overlayBuilder = ref.watch(overlayControllerProvider); final overlayBuilder = ref.watch(overlayControllerProvider);
final tabInFullScreen = ref.watch( final tabInFullScreen = ref.watch(
@@ -101,18 +105,57 @@ class BrowserScreen extends HookConsumerWidget {
[], [],
); );
bool dismissOnThreshold(DraggableScrollableNotification notification) { final sheetController = useRef<PersistentBottomSheetController?>(null);
if (!context.mounted) return false; ref.listen(bottomSheetControllerProvider, (previous, next) {
final state = scaffoldKey.currentState;
if (state != null) {
if (sheetController.value != null) {
sheetController.value!.close();
sheetController.value = null;
}
if (notification.extent <= 0.1) { if (next != null) {
ref.read(bottomSheetControllerProvider.notifier).dismiss(); bool dismissOnThreshold(
return true; DraggableScrollableNotification notification,
} else { ) {
ref.read(bottomSheetExtendProvider.notifier).add(notification.extent); if (!context.mounted) return false;
if (notification.extent <= 0.1) {
ref.read(bottomSheetControllerProvider.notifier).dismiss();
return true;
} else {
ref
.read(bottomSheetExtendProvider.notifier)
.add(notification.extent);
}
return false;
}
final sheet = switch (next) {
ViewTabsSheet() =>
NotificationListener<DraggableScrollableNotification>(
key: ValueKey(next),
onNotification: dismissOnThreshold,
child: _ViewTabsSheet(
maxChildSize: MediaQuery.of(context).relativeSafeArea(),
),
),
final EditUrlSheet parameter =>
NotificationListener<DraggableScrollableNotification>(
key: ValueKey(parameter),
onNotification: dismissOnThreshold,
child: _ViewUrlSheet(
initialTabState: parameter.tabState,
maxChildSize: MediaQuery.of(context).relativeSafeArea(),
),
),
};
sheetController.value = state.showBottomSheet((context) => sheet);
}
} }
});
return false;
}
return PopScope( return PopScope(
//We need this for BackButtonListener to work downstream //We need this for BackButtonListener to work downstream
@@ -121,6 +164,7 @@ class BrowserScreen extends HookConsumerWidget {
child: Theme( child: Theme(
data: themeData, data: themeData,
child: Scaffold( child: Scaffold(
key: scaffoldKey,
extendBodyBehindAppBar: tabInFullScreen, extendBodyBehindAppBar: tabInFullScreen,
bottomNavigationBar: HookConsumer( bottomNavigationBar: HookConsumer(
builder: (context, ref, child) { builder: (context, ref, child) {
@@ -236,7 +280,7 @@ class BrowserScreen extends HookConsumerWidget {
return overlayBuilder!.call(context); return overlayBuilder!.call(context);
}, },
child: Listener( child: Listener(
onPointerDown: (displayedSheet != null) onPointerDown: sheetDisplayed
? (_) { ? (_) {
ref ref
.read(bottomSheetControllerProvider.notifier) .read(bottomSheetControllerProvider.notifier)
@@ -257,7 +301,7 @@ class BrowserScreen extends HookConsumerWidget {
return false; return false;
} }
if (displayedSheet != null) { if (sheetDisplayed) {
ref ref
.read(bottomSheetControllerProvider.notifier) .read(bottomSheetControllerProvider.notifier)
.dismiss(); .dismiss();
@@ -346,7 +390,7 @@ class BrowserScreen extends HookConsumerWidget {
} }
}, },
child: _BrowserView( child: _BrowserView(
sheetDisplayed: displayedSheet != null, sheetDisplayed: sheetDisplayed,
isFullscreen: tabInFullScreen, isFullscreen: tabInFullScreen,
), ),
), ),
@@ -355,27 +399,6 @@ class BrowserScreen extends HookConsumerWidget {
}, },
), ),
floatingActionButton: ReaderAppearanceButton(), floatingActionButton: ReaderAppearanceButton(),
bottomSheet: (displayedSheet != null)
? switch (displayedSheet) {
ViewTabsSheet() =>
NotificationListener<DraggableScrollableNotification>(
key: ValueKey(displayedSheet),
onNotification: dismissOnThreshold,
child: _ViewTabsSheet(
maxChildSize: MediaQuery.of(context).relativeSafeArea(),
),
),
final EditUrlSheet parameter =>
NotificationListener<DraggableScrollableNotification>(
key: ValueKey(parameter),
onNotification: dismissOnThreshold,
child: _ViewUrlSheet(
initialTabState: parameter.tabState,
maxChildSize: MediaQuery.of(context).relativeSafeArea(),
),
),
}
: null,
), ),
), ),
); );