b9e68e3bc8
Per-screen application of the design system to the Flutter app. Mirrors the web's surface phase landed in FabledScribe v26.04.28.1. Foundation port shipped in 0f05f47; this is the surface work. Lucide icon migration - Added lucide_icons ^0.257.0 dependency - 107 Material Icons references → LucideIcons across 21 files. Drop-in IconData swap (Icon(LucideIcons.X) instead of Icon(Icons.x)). - Lucide import added to each touched file. Input border radius - theme.dart inputDecorationTheme borderRadius 24 → 8 in both light and dark themes. Doc says radius-md (8px) for inputs; previous pill shape was Material default that the doc deviates from. Illuminated Transcript pattern (ChatMessageBubble) - User bubble: accent-tinted border → neutral Pewter (scheme.outline). Asymmetric corner already correct (bottomRight 4px). - Assistant bubble: topLeft corner 4 → 16; only bottomLeft stays 4 (the "tail" effect, mirroring web's `border-bottom-left-radius: 4px`). Background switched from surfaceContainerHighest (Slate) to surface (Iron) per the doc spec "card surface". - Assistant bubble glow shadow added — accent-tinted blur (28px alpha 0.14) + depth shadow (8px alpha 0.4 black). Mirrors web's --color-bubble-asst-shadow. ActionColors wiring (Hybrid rule) - 5 'Delete' confirm buttons across notes / tasks / chat conversations / calendar event sheet → Oxblood action-destructive via the ActionColors ThemeExtension defined in the foundation port. Foreground for ghost/text variants, backgroundColor for filled. - Calendar event Save button → Moss action-primary. The first call site to wire ActionColors.primary; serves as the pattern for future Save reclassifications. - Other Save buttons (note edit, task edit, project edit, etc.) still flow through colorScheme.primary (dusty violet) and read as brand-moment. Reclassifying those is deferred — the wiring pattern is established and can be applied incrementally as files are touched. Indigo cleanup - 4 hardcoded #7C3AED / #5B21B6 literals → dusty-violet equivalents (#5B4A8A / #3F3560). Spots: project_tasks_screen color fallback (×2), journal_screen gradient. Verification - flutter analyze: No issues found What's deferred - Per-screen Save / Cancel reclassification beyond the calendar event Save button. Wiring pattern established; rollout opportunistic. - Long-form 1.7 line-height on assistant Markdown content (would require MarkdownStyleSheet work; minor). - Surface walk on Knowledge / Projects / Settings screens for any hardcoded styling that needs touch. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
295 lines
10 KiB
Dart
295 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../core/exceptions.dart';
|
|
import '../../data/models/message.dart';
|
|
import '../../providers/chat_provider.dart';
|
|
import '../../providers/voice_provider.dart';
|
|
import '../../widgets/chat_message_bubble.dart';
|
|
import '../../widgets/voice_mic_button.dart';
|
|
|
|
class ChatScreen extends ConsumerStatefulWidget {
|
|
final int conversationId;
|
|
const ChatScreen({super.key, required this.conversationId});
|
|
|
|
@override
|
|
ConsumerState<ChatScreen> createState() => _ChatScreenState();
|
|
}
|
|
|
|
class _ChatScreenState extends ConsumerState<ChatScreen>
|
|
with WidgetsBindingObserver {
|
|
final _controller = TextEditingController();
|
|
final _scrollController = ScrollController();
|
|
bool _refreshing = false;
|
|
|
|
Future<void> _refreshMessages() async {
|
|
if (_refreshing) return;
|
|
setState(() => _refreshing = true);
|
|
try {
|
|
await ref
|
|
.read(messagesProvider(widget.conversationId).notifier)
|
|
.refresh();
|
|
} catch (_) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Could not refresh messages.')),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) setState(() => _refreshing = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
// If we land on a conversation whose last assistant message is already
|
|
// mid-stream (e.g. the /news discuss button creates a conv and
|
|
// auto-kicks generation), attach to the running stream so the user sees
|
|
// live tokens instead of a frozen placeholder.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
ref
|
|
.read(messagesProvider(widget.conversationId).notifier)
|
|
.attachToGeneration();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
ref.read(messagesProvider(widget.conversationId).notifier).refresh();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
_controller.dispose();
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _scrollToBottom() {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (_scrollController.hasClients) {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: 200),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _send() async {
|
|
final text = _controller.text.trim();
|
|
if (text.isEmpty) return;
|
|
_controller.clear();
|
|
try {
|
|
await ref
|
|
.read(messagesProvider(widget.conversationId).notifier)
|
|
.sendMessage(text);
|
|
} on AppException catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(SnackBar(content: Text(e.message)));
|
|
}
|
|
} catch (_) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Failed to send message.')),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _toggleVoiceMode() async {
|
|
final voice = ref.read(voiceProvider);
|
|
if (voice.voiceModeActive) {
|
|
ref.read(voiceProvider.notifier).exitVoiceMode();
|
|
return;
|
|
}
|
|
await ref.read(voiceProvider.notifier).enterVoiceMode(
|
|
onTranscript: (transcript) async {
|
|
await ref
|
|
.read(messagesProvider(widget.conversationId).notifier)
|
|
.sendMessage(transcript);
|
|
},
|
|
enableTts: true,
|
|
onError: (msg) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(SnackBar(content: Text(msg)));
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final messagesAsync = ref.watch(messagesProvider(widget.conversationId));
|
|
final isStreaming = ref.watch(isStreamingProvider(widget.conversationId));
|
|
final streamingStatus =
|
|
ref.watch(streamingStatusProvider(widget.conversationId));
|
|
final voiceState = ref.watch(voiceProvider);
|
|
|
|
// Scroll when messages change.
|
|
ref.listen(messagesProvider(widget.conversationId), (prev, next) {
|
|
_scrollToBottom();
|
|
});
|
|
|
|
// Feed streaming content to VoiceNotifier for TTS.
|
|
ref.listen(messagesProvider(widget.conversationId), (prev, next) {
|
|
if (!voiceState.voiceModeActive) return;
|
|
final messages = next.value;
|
|
if (messages == null || messages.isEmpty) return;
|
|
final last = messages.last;
|
|
if (last.role != MessageRole.assistant) return;
|
|
final isComplete = last.status != 'generating';
|
|
ref
|
|
.read(voiceProvider.notifier)
|
|
.feedContent(last.content, isComplete: isComplete);
|
|
});
|
|
|
|
final convTitle = ref
|
|
.watch(conversationsProvider)
|
|
.value
|
|
?.where((c) => c.id == widget.conversationId)
|
|
.firstOrNull
|
|
?.title;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(convTitle?.isNotEmpty == true ? convTitle! : 'Chat'),
|
|
actions: [
|
|
IconButton(
|
|
tooltip: 'Refresh',
|
|
onPressed: _refreshing ? null : _refreshMessages,
|
|
icon: _refreshing
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(LucideIcons.refreshCw),
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Expanded(
|
|
child: messagesAsync.when(
|
|
loading: () =>
|
|
const Center(child: CircularProgressIndicator()),
|
|
error: (err, stack) => const Center(
|
|
child: Text('Could not load messages.'),
|
|
),
|
|
data: (messages) {
|
|
return RefreshIndicator(
|
|
onRefresh: _refreshMessages,
|
|
child: messages.isEmpty
|
|
? ListView(
|
|
// Needs to be scrollable for RefreshIndicator to
|
|
// fire on empty state — plain Center won't work.
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
children: const [
|
|
SizedBox(height: 240),
|
|
Center(child: Text('Send a message to start.')),
|
|
],
|
|
)
|
|
: ListView.builder(
|
|
controller: _scrollController,
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 8, vertical: 12),
|
|
itemCount: messages.length,
|
|
itemBuilder: (context, i) => ChatMessageBubble(
|
|
message: messages[i],
|
|
streamingStatus: (i == messages.length - 1 &&
|
|
messages[i].status == 'generating')
|
|
? streamingStatus
|
|
: '',
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
// Voice mode banner
|
|
if (voiceState.voiceModeActive)
|
|
Container(
|
|
width: double.infinity,
|
|
color: const Color(0xFFEF4444).withValues(alpha: 0.12),
|
|
padding:
|
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
|
|
child: const Text(
|
|
'🎤 Listening… tap mic to exit voice mode',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: Color(0xFFF87171),
|
|
),
|
|
),
|
|
),
|
|
const Divider(height: 1),
|
|
SafeArea(
|
|
child: Padding(
|
|
padding:
|
|
const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _controller,
|
|
decoration: InputDecoration(
|
|
hintText: voiceState.voiceModeActive
|
|
? 'Listening…'
|
|
: 'Message…',
|
|
hintStyle: voiceState.voiceModeActive
|
|
? const TextStyle(fontStyle: FontStyle.italic)
|
|
: null,
|
|
border: const OutlineInputBorder(),
|
|
isDense: true,
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 12, vertical: 10),
|
|
),
|
|
maxLines:
|
|
MediaQuery.of(context).size.width >= 600 ? 2 : 4,
|
|
minLines: 1,
|
|
textInputAction: TextInputAction.newline,
|
|
enabled: !isStreaming && !voiceState.voiceModeActive,
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
VoiceMicButton(
|
|
mode: voiceState.mode,
|
|
voiceModeActive: voiceState.voiceModeActive,
|
|
amplitude: voiceState.amplitude,
|
|
onTap: _toggleVoiceMode,
|
|
),
|
|
const SizedBox(width: 6),
|
|
IconButton.filled(
|
|
onPressed: (isStreaming || voiceState.voiceModeActive)
|
|
? null
|
|
: _send,
|
|
icon: isStreaming
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child:
|
|
CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(LucideIcons.send),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|