687 lines
22 KiB
Dart
687 lines
22 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_markdown/flutter_markdown.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
import '../providers/auth_provider.dart';
|
|
import '../providers/chat_provider.dart';
|
|
import '../models/message.dart';
|
|
import '../theme.dart';
|
|
import '../l10n/app_localizations.dart';
|
|
|
|
class HomePage extends ConsumerStatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
ConsumerState<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends ConsumerState<HomePage> {
|
|
final _messageController = TextEditingController();
|
|
final _scrollController = ScrollController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_scrollToBottom();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_messageController.dispose();
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _scrollToBottom() {
|
|
if (_scrollController.hasClients) {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
}
|
|
|
|
void _sendMessage() {
|
|
final text = _messageController.text.trim();
|
|
if (text.isEmpty) return;
|
|
_messageController.clear();
|
|
ref.read(chatProvider.notifier).sendMessage(text).then((_) {
|
|
Future.delayed(const Duration(milliseconds: 100), _scrollToBottom);
|
|
});
|
|
setState(() {});
|
|
}
|
|
|
|
void _openScanner() {
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (_) => const ScannerPage(),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
final authState = ref.watch(authProvider);
|
|
final chatState = ref.watch(chatProvider);
|
|
final user = authState.user;
|
|
|
|
ref.listen(chatProvider, (prev, next) {
|
|
if (next.messages.length != (prev?.messages.length ?? 0)) {
|
|
Future.delayed(const Duration(milliseconds: 100), _scrollToBottom);
|
|
}
|
|
});
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppColors.background,
|
|
appBar: AppBar(
|
|
backgroundColor: AppColors.surface,
|
|
elevation: 0,
|
|
automaticallyImplyLeading: false,
|
|
title: Row(
|
|
children: [
|
|
Container(
|
|
width: 36,
|
|
height: 36,
|
|
decoration: BoxDecoration(
|
|
gradient: AppGradients.primary,
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: const Icon(Icons.auto_awesome, color: Colors.white, size: 20),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
l10n.aiAssistant,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.textPrimary,
|
|
),
|
|
),
|
|
Row(
|
|
children: [
|
|
Container(
|
|
width: 6,
|
|
height: 6,
|
|
decoration: const BoxDecoration(
|
|
color: AppColors.success,
|
|
shape: BoxShape.circle,
|
|
),
|
|
),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
l10n.online,
|
|
style: const TextStyle(fontSize: 11, color: AppColors.textSecondary),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
if (user != null)
|
|
PopupMenuButton<String>(
|
|
offset: const Offset(0, 40),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
child: Row(
|
|
children: [
|
|
CircleAvatar(
|
|
radius: 14,
|
|
backgroundColor: AppColors.primary.withOpacity(0.1),
|
|
child: Text(
|
|
user.name.substring(0, 1),
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.primary,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 6),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: user.isBoss
|
|
? const Color(0xFFFFF7ED)
|
|
: const Color(0xFFEFF6FF),
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
child: Text(
|
|
user.isBoss ? l10n.boss : l10n.employee,
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
fontWeight: FontWeight.w500,
|
|
color: user.isBoss
|
|
? const Color(0xFFEA580C)
|
|
: const Color(0xFF2563EB),
|
|
),
|
|
),
|
|
),
|
|
const Icon(Icons.keyboard_arrow_down, size: 18),
|
|
],
|
|
),
|
|
),
|
|
onSelected: (value) {
|
|
if (value == 'settings') {
|
|
context.push('/settings');
|
|
} else if (value == 'logout') {
|
|
ref.read(authProvider.notifier).logout();
|
|
}
|
|
},
|
|
itemBuilder: (context) => [
|
|
PopupMenuItem(
|
|
value: 'profile',
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.person_outline, size: 18),
|
|
const SizedBox(width: 10),
|
|
Text('${user.name} (${user.phone})'),
|
|
],
|
|
),
|
|
),
|
|
const PopupMenuDivider(),
|
|
PopupMenuItem(
|
|
value: 'settings',
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.settings_outlined, size: 18),
|
|
const SizedBox(width: 10),
|
|
Text(l10n.settings),
|
|
],
|
|
),
|
|
),
|
|
PopupMenuItem(
|
|
value: 'logout',
|
|
child: Row(
|
|
children: [
|
|
const Icon(Icons.logout, size: 18, color: AppColors.error),
|
|
const SizedBox(width: 10),
|
|
Text(l10n.logout, style: const TextStyle(color: AppColors.error)),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Expanded(
|
|
child: ListView.builder(
|
|
controller: _scrollController,
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: chatState.messages.length,
|
|
itemBuilder: (context, index) {
|
|
final message = chatState.messages[index];
|
|
return _buildMessageBubble(message, index, l10n);
|
|
},
|
|
),
|
|
),
|
|
_buildQuickActions(l10n),
|
|
_buildInputArea(l10n),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildMessageBubble(ChatMessage message, int index, AppLocalizations l10n) {
|
|
final isUser = message.sender == MessageSender.user;
|
|
|
|
if (message.type == MessageType.loading) {
|
|
return Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Container(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.chatAiBubble,
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: AlwaysStoppedAnimation(
|
|
AppColors.primary.withOpacity(0.6),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Text(
|
|
l10n.thinking,
|
|
style: TextStyle(
|
|
fontSize: 14,
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
return Align(
|
|
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
|
|
child: Container(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
constraints: BoxConstraints(
|
|
maxWidth: MediaQuery.of(context).size.width * 0.82,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment:
|
|
isUser ? CrossAxisAlignment.end : CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: isUser ? AppColors.chatUserBubble : AppColors.chatAiBubble,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: const Radius.circular(16),
|
|
topRight: const Radius.circular(16),
|
|
bottomLeft: Radius.circular(isUser ? 16 : 4),
|
|
bottomRight: Radius.circular(isUser ? 4 : 16),
|
|
),
|
|
),
|
|
child: isUser || message.type == MessageType.text
|
|
? Text(
|
|
message.content,
|
|
style: TextStyle(
|
|
fontSize: 15,
|
|
color: isUser ? Colors.white : AppColors.textPrimary,
|
|
height: 1.5,
|
|
),
|
|
)
|
|
: MarkdownBody(
|
|
data: message.content,
|
|
styleSheet: MarkdownStyleSheet(
|
|
p: const TextStyle(
|
|
fontSize: 15,
|
|
color: AppColors.textPrimary,
|
|
height: 1.6,
|
|
),
|
|
code: TextStyle(
|
|
fontSize: 13,
|
|
color: AppColors.primary,
|
|
backgroundColor: AppColors.primary.withOpacity(0.08),
|
|
fontFamily: 'monospace',
|
|
),
|
|
codeblockDecoration: BoxDecoration(
|
|
color: AppColors.primary.withOpacity(0.05),
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
blockquote: TextStyle(
|
|
fontSize: 14,
|
|
color: AppColors.textSecondary,
|
|
fontStyle: FontStyle.italic,
|
|
),
|
|
blockquoteDecoration: BoxDecoration(
|
|
border: Border(
|
|
left: BorderSide(
|
|
color: AppColors.primary.withOpacity(0.4),
|
|
width: 3,
|
|
),
|
|
),
|
|
),
|
|
tableHead: const TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.textPrimary,
|
|
),
|
|
tableBody: const TextStyle(
|
|
color: AppColors.textSecondary,
|
|
),
|
|
tableBorder: TableBorder.all(
|
|
color: AppColors.divider,
|
|
width: 1,
|
|
),
|
|
tableCellsPadding: const EdgeInsets.all(8),
|
|
h1: const TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.textPrimary,
|
|
),
|
|
h2: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.textPrimary,
|
|
),
|
|
h3: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.textPrimary,
|
|
),
|
|
strong: const TextStyle(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.primary,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
_formatTime(message.timestamp),
|
|
style: TextStyle(
|
|
fontSize: 11,
|
|
color: AppColors.textTertiary,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
).animate().fadeIn(duration: 300.ms).slideY(begin: 0.1, end: 0, duration: 300.ms);
|
|
}
|
|
|
|
Widget _buildQuickActions(AppLocalizations l10n) {
|
|
return Container(
|
|
margin: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surface,
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.06),
|
|
blurRadius: 16,
|
|
offset: const Offset(0, 4),
|
|
),
|
|
],
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Wrap(
|
|
spacing: 12,
|
|
runSpacing: 12,
|
|
children: [
|
|
_ActionChip(
|
|
icon: Icons.event_note_outlined,
|
|
label: l10n.eventPublish,
|
|
color: const Color(0xFF8B5CF6),
|
|
onTap: () => context.push('/event/publish'),
|
|
),
|
|
_ActionChip(
|
|
icon: Icons.assignment_outlined,
|
|
label: l10n.orderWork,
|
|
color: const Color(0xFFF59E0B),
|
|
onTap: () => context.push('/order-work'),
|
|
),
|
|
_ActionChip(
|
|
icon: Icons.qr_code_scanner,
|
|
label: l10n.verify,
|
|
color: const Color(0xFF10B981),
|
|
onTap: () => _openScanner(),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildInputArea(AppLocalizations l10n) {
|
|
return Container(
|
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 24),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surface,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.04),
|
|
blurRadius: 12,
|
|
offset: const Offset(0, -4),
|
|
),
|
|
],
|
|
),
|
|
child: SafeArea(
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _messageController,
|
|
maxLines: null,
|
|
textInputAction: TextInputAction.send,
|
|
onSubmitted: (_) => _sendMessage(),
|
|
decoration: InputDecoration(
|
|
hintText: l10n.enterMessage,
|
|
hintStyle: TextStyle(
|
|
fontSize: 14,
|
|
color: AppColors.textTertiary,
|
|
),
|
|
filled: true,
|
|
fillColor: AppColors.background,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(20),
|
|
borderSide: BorderSide.none,
|
|
),
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 16,
|
|
vertical: 10,
|
|
),
|
|
isDense: true,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
GestureDetector(
|
|
onTap: _sendMessage,
|
|
child: Container(
|
|
width: 40,
|
|
height: 40,
|
|
decoration: const BoxDecoration(
|
|
gradient: AppGradients.primary,
|
|
shape: BoxShape.circle,
|
|
),
|
|
child: const Icon(
|
|
Icons.send,
|
|
color: Colors.white,
|
|
size: 18,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
String _formatTime(DateTime time) {
|
|
final now = DateTime.now();
|
|
if (time.year == now.year && time.month == now.month && time.day == now.day) {
|
|
return '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
|
|
}
|
|
return '${time.month}/${time.day} ${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
|
|
}
|
|
}
|
|
|
|
class _ActionChip extends StatelessWidget {
|
|
final IconData icon;
|
|
final String label;
|
|
final Color color;
|
|
final VoidCallback onTap;
|
|
|
|
const _ActionChip({
|
|
required this.icon,
|
|
required this.label,
|
|
required this.color,
|
|
required this.onTap,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
onTap: onTap,
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
color: color.withOpacity(0.08),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(icon, size: 18, color: color),
|
|
const SizedBox(width: 6),
|
|
Text(
|
|
label,
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w500,
|
|
color: color,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class ScannerPage extends StatefulWidget {
|
|
const ScannerPage({super.key});
|
|
|
|
@override
|
|
State<ScannerPage> createState() => _ScannerPageState();
|
|
}
|
|
|
|
class _ScannerPageState extends State<ScannerPage> {
|
|
bool _isScanned = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context)!;
|
|
return Scaffold(
|
|
backgroundColor: Colors.black,
|
|
body: Stack(
|
|
children: [
|
|
MobileScanner(
|
|
onDetect: (capture) {
|
|
if (_isScanned) return;
|
|
final barcodes = capture.barcodes;
|
|
for (final barcode in barcodes) {
|
|
final code = barcode.rawValue;
|
|
if (code != null && code.isNotEmpty) {
|
|
_isScanned = true;
|
|
Navigator.of(context).pop();
|
|
context.push('/scan-result?code=$code');
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
),
|
|
_buildOverlay(),
|
|
SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () => Navigator.of(context).pop(),
|
|
child: Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black.withOpacity(0.5),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
child: const Icon(Icons.close, color: Colors.white),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black.withOpacity(0.5),
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
child: Text(
|
|
l10n.scanning,
|
|
style: const TextStyle(color: Colors.white, fontSize: 13),
|
|
),
|
|
),
|
|
const Spacer(),
|
|
const SizedBox(width: 44),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildOverlay() {
|
|
return Center(
|
|
child: Container(
|
|
width: 260,
|
|
height: 260,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(
|
|
color: AppColors.primary.withOpacity(0.8),
|
|
width: 2,
|
|
),
|
|
),
|
|
child: Stack(
|
|
children: [
|
|
Positioned(
|
|
top: 0,
|
|
left: 0,
|
|
child: _buildCorner(true, true),
|
|
),
|
|
Positioned(
|
|
top: 0,
|
|
right: 0,
|
|
child: _buildCorner(true, false),
|
|
),
|
|
Positioned(
|
|
bottom: 0,
|
|
left: 0,
|
|
child: _buildCorner(false, true),
|
|
),
|
|
Positioned(
|
|
bottom: 0,
|
|
right: 0,
|
|
child: _buildCorner(false, false),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCorner(bool top, bool left) {
|
|
return Container(
|
|
width: 30,
|
|
height: 30,
|
|
decoration: BoxDecoration(
|
|
border: Border(
|
|
top: top ? const BorderSide(color: AppColors.primary, width: 4) : BorderSide.none,
|
|
bottom: !top ? const BorderSide(color: AppColors.primary, width: 4) : BorderSide.none,
|
|
left: left ? const BorderSide(color: AppColors.primary, width: 4) : BorderSide.none,
|
|
right: !left ? const BorderSide(color: AppColors.primary, width: 4) : BorderSide.none,
|
|
),
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: top && left ? const Radius.circular(12) : Radius.zero,
|
|
topRight: top && !left ? const Radius.circular(12) : Radius.zero,
|
|
bottomLeft: !top && left ? const Radius.circular(12) : Radius.zero,
|
|
bottomRight: !top && !left ? const Radius.circular(12) : Radius.zero,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|