feat: enhance routing and state management in Dashbot components

This commit is contained in:
Udhay-Adithya
2025-09-21 02:14:38 +05:30
parent 671e4218cc
commit f8d54da074
6 changed files with 36 additions and 30 deletions

View File

@@ -9,17 +9,27 @@ import 'package:flutter/material.dart';
Route<dynamic>? generateRoute(RouteSettings settings) {
switch (settings.name) {
case (DashbotRoutes.dashbotHome):
return MaterialPageRoute(builder: (context) => DashbotHomePage());
return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotHome),
builder: (context) => DashbotHomePage(),
);
case (DashbotRoutes.dashbotDefault):
return MaterialPageRoute(builder: (context) => DashbotDefaultPage());
return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotDefault),
builder: (context) => DashbotDefaultPage(),
);
case (DashbotRoutes.dashbotChat):
final arg = settings.arguments;
ChatMessageType? initialTask;
if (arg is ChatMessageType) initialTask = arg;
return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotChat),
builder: (context) => ChatScreen(initialTask: initialTask),
);
default:
return MaterialPageRoute(builder: (context) => DashbotDefaultPage());
return MaterialPageRoute(
settings: const RouteSettings(name: DashbotRoutes.dashbotDefault),
builder: (context) => DashbotDefaultPage(),
);
}
}

View File

@@ -6,11 +6,7 @@ import '../providers/dashbot_window_notifier.dart';
/// Optionally pass provider overrides (e.g., dashbotRequestContextProvider)
/// so the host app can feed live context into Dashbot.
void showDashbotWindow(
BuildContext context,
WidgetRef ref, {
List<Override>? overrides,
}) {
void showDashbotWindow(BuildContext context, WidgetRef ref) {
final isDashbotActive = ref.read(dashbotWindowNotifierProvider).isActive;
final isDashbotPopped = ref.read(dashbotWindowNotifierProvider).isPopped;
final windowNotifier = ref.read(dashbotWindowNotifierProvider.notifier);
@@ -21,7 +17,6 @@ void showDashbotWindow(
entry = OverlayEntry(
builder: (context) => ProviderScope(
overrides: overrides ?? const [],
child: DashbotWindow(
onClose: () {
entry?.remove();

View File

@@ -37,11 +37,14 @@ class DashbotWindow extends ConsumerWidget {
ref.listen(
selectedRequestModelProvider,
(current, next) {
if (next?.responseStatus != null) {
_dashbotNavigatorKey.currentState?.pushNamed(
DashbotRoutes.dashbotHome,
);
(prev, next) {
if (prev?.id == next?.id) return;
final initial = _dashbotNavigatorKey.currentState?.widget.initialRoute;
final atRoot = _dashbotNavigatorKey.currentState?.canPop() == false;
// Only push when navigator started on Default and is still at root
if (initial == DashbotRoutes.dashbotDefault && atRoot) {
_dashbotNavigatorKey.currentState
?.pushNamed(DashbotRoutes.dashbotHome);
}
},
);

View File

@@ -30,20 +30,23 @@ class _DashbotTabState extends ConsumerState<DashbotTab>
ref.listen(
selectedRequestModelProvider,
(prev, next) {
if (next?.responseStatus != null) {
if (prev?.id == next?.id) return;
final initial = _navKey.currentState?.widget.initialRoute;
final atRoot = _navKey.currentState?.canPop() == false;
if (initial == DashbotRoutes.dashbotDefault && atRoot) {
_navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome);
}
},
);
return WillPopScope(
onWillPop: () async {
return PopScope(
canPop: true,
onPopInvokedWithResult: (didPop, _) {
if (didPop) return;
final canPop = _navKey.currentState?.canPop() ?? false;
if (canPop) {
_navKey.currentState?.pop();
return false;
}
return true;
},
child: Padding(
padding: kP10,

View File

@@ -34,14 +34,6 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
@override
Widget build(BuildContext context) {
// ref.listen(
// selectedRequestModelProvider,
// (current, next) {
// if (current?.id != next?.id) {
// Navigator.pop(context);
// }
// },
// );
return Scaffold(
body: Column(
children: [

View File

@@ -21,7 +21,11 @@ class DashbotHomePage extends ConsumerStatefulWidget {
class _DashbotHomePageState extends ConsumerState<DashbotHomePage> {
@override
Widget build(BuildContext context) {
final currentRequest = ref.watch(selectedRequestModelProvider);
final hasOkResponse = ref.watch(
selectedRequestModelProvider.select((req) =>
req?.httpResponseModel?.statusCode != null &&
req?.httpResponseModel?.statusCode == 200),
);
// ref.listen(
// selectedRequestModelProvider,
@@ -120,8 +124,7 @@ class _DashbotHomePageState extends ConsumerState<DashbotHomePage> {
);
},
),
if (currentRequest?.httpResponseModel?.statusCode != null &&
currentRequest?.httpResponseModel?.statusCode == 200) ...[
if (hasOkResponse) ...[
HomeScreenTaskButton(
label: "🛠️ Generate Tool",
onPressed: () {