import 'package:apidash/providers/providers.dart'; import 'package:apidash_design_system/apidash_design_system.dart'; import 'core/routes/dashbot_router.dart'; import 'core/routes/dashbot_routes.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'core/providers/dashbot_window_notifier.dart'; import 'core/utils/show_dashbot.dart'; class DashbotTab extends ConsumerStatefulWidget { const DashbotTab({super.key}); @override ConsumerState createState() => _DashbotTabState(); } class _DashbotTabState extends ConsumerState with AutomaticKeepAliveClientMixin { static final GlobalKey _navKey = GlobalKey(); @override bool get wantKeepAlive => true; @override Widget build(BuildContext context) { super.build(context); final currentRequest = ref.watch(selectedRequestModelProvider); // If a response arrives while user is on default, navigate to home. ref.listen( selectedRequestModelProvider, (prev, next) { if (next?.responseStatus != null) { _navKey.currentState?.pushNamed(DashbotRoutes.dashbotHome); } }, ); return WillPopScope( onWillPop: () async { final canPop = _navKey.currentState?.canPop() ?? false; if (canPop) { _navKey.currentState?.pop(); return false; } return true; }, child: Padding( padding: kP10, child: Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceContainerLow, border: Border.all( color: Theme.of(context).colorScheme.surfaceContainerHighest), borderRadius: kBorderRadius8, ), child: Column( children: [ Padding( padding: kP8, child: Align( alignment: AlignmentGeometry.centerRight, child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ IconButton( icon: Icon( Icons.close_fullscreen, ), onPressed: () { ref .read(dashbotWindowNotifierProvider.notifier) .togglePopped(); final newState = ref.read(dashbotWindowNotifierProvider); if (newState.isPopped) { showDashbotWindow(context, ref); } }, ), IconButton( onPressed: () { final isActive = ref.read(dashbotWindowNotifierProvider).isActive; ref .read(dashbotWindowNotifierProvider.notifier) .togglePopped(); if (isActive) { ref .read(dashbotWindowNotifierProvider.notifier) .toggleActive(); } }, icon: Icon(Icons.close), ), ], ), ), ), Expanded( child: Navigator( key: _navKey, initialRoute: currentRequest?.responseStatus == null ? DashbotRoutes.dashbotDefault : DashbotRoutes.dashbotHome, onGenerateRoute: generateRoute, ), ), ], ), ), ), ); } }