import 'package:flutter/cupertino.dart'; import '../../../core/error_reason.dart'; import 'chat_efforts.dart'; /// The Cupertino action sheet of a chat's thinking levels, the current one /// checked. It stays open with the server's reason when a choice is refused /// and closes once one is accepted. class ChatEffortSheet extends StatefulWidget { const ChatEffortSheet({ super.key, required this.state, required this.running, required this.choose, }); final ChatEffortState state; final bool running; final Future Function(ChatEffortLevel level) choose; @override State createState() => _ChatEffortSheetState(); } class _ChatEffortSheetState extends State { bool _busy = false; String? _error; Future _choose(ChatEffortLevel level) async { if (_busy) return; if (level.id == widget.state.effort) { Navigator.of(context).pop(); return; } setState(() { _busy = true; _error = null; }); try { await widget.choose(level); if (mounted) Navigator.of(context).pop(); } catch (error) { if (!mounted) return; setState(() { _busy = false; _error = describeErrorReason( error, fallback: 'The thinking level was not changed.', ); }); } } @override Widget build(BuildContext context) { final secondary = CupertinoColors.secondaryLabel.resolveFrom(context); final error = _error; return CupertinoActionSheet( key: const Key('chat-effort-sheet'), title: const Text('Thinking'), message: Text( error ?? chatEffortNote(running: widget.running), key: const Key('chat-effort-note'), style: error == null ? null : TextStyle(color: CupertinoColors.systemRed.resolveFrom(context)), ), actions: [ for (final level in widget.state.levels) CupertinoActionSheetAction( key: ValueKey('chat-effort-option-${level.id}'), onPressed: () => _choose(level), child: Column( children: [ Row( mainAxisSize: MainAxisSize.min, children: [ Flexible(child: Text(level.name)), if (level.id == widget.state.current) ...[ const SizedBox(width: 6), const Icon(CupertinoIcons.checkmark_alt), ], ], ), if (level.description.isNotEmpty) Text( level.description, textAlign: TextAlign.center, style: TextStyle(fontSize: 13, color: secondary), ), ], ), ), ], cancelButton: CupertinoActionSheetAction( onPressed: () => Navigator.of(context).pop(), child: const Text('Cancel'), ), ); } }