261 lines
9.0 KiB
Dart
261 lines
9.0 KiB
Dart
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/models/ai/ai_chat_args.dart';
|
|
import 'package:didvan/models/ai/bots_model.dart';
|
|
import 'package:didvan/views/ai/ai_chat_page.dart';
|
|
import 'package:didvan/views/ai/ai_chat_state.dart';
|
|
import 'package:didvan/views/widgets/didvan/text.dart';
|
|
import 'package:didvan/views/widgets/hoshan_home_app_bar.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:didvan/views/ai/history_ai_chat_state.dart';
|
|
import 'package:didvan/views/ai/ai_state.dart';
|
|
|
|
|
|
class AiSectionPage extends StatefulWidget {
|
|
const AiSectionPage({super.key});
|
|
|
|
@override
|
|
State<AiSectionPage> createState() => _AiSectionPageState();
|
|
}
|
|
|
|
class _AiSectionGridItem {
|
|
final String title;
|
|
final String description; // فیلد جدید اضافه شد
|
|
final String iconPath;
|
|
final void Function(BuildContext context) onTap;
|
|
|
|
_AiSectionGridItem({
|
|
required this.title,
|
|
required this.description, // به سازنده اضافه شد
|
|
required this.iconPath,
|
|
required this.onTap,
|
|
});
|
|
}
|
|
|
|
class _AiSectionPageState extends State<AiSectionPage> {
|
|
late final List<_AiSectionGridItem> _gridItems;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
final historyAiChatState = context.read<HistoryAiChatState>();
|
|
if (historyAiChatState.bots.isEmpty) {
|
|
historyAiChatState.getBots();
|
|
}
|
|
|
|
final aiState = context.read<AiState>();
|
|
final aiChatState = context.read<AiChatState>();
|
|
|
|
aiState.onClearChatCallback = () async {
|
|
await aiChatState.clearChat();
|
|
};
|
|
|
|
aiState.endChat();
|
|
if (aiState.tools == null) {
|
|
aiState.getTools();
|
|
}
|
|
});
|
|
|
|
_gridItems = [
|
|
_AiSectionGridItem(
|
|
title: 'ساخت عکس',
|
|
description: 'ایجاد تصاویر خلاقانه با هوش مصنوعی', // توضیحات اضافه شد
|
|
iconPath: 'lib/assets/icons/create image.svg',
|
|
onTap: (context) {
|
|
final aiState = context.read<AiState>();
|
|
aiState.endChat();
|
|
if (aiState.tools != null && aiState.tools!.isNotEmpty) {
|
|
try {
|
|
final tool = aiState.tools![0];
|
|
if (tool.bots != null && tool.bots!.isNotEmpty) {
|
|
aiState.startChat(AiChatArgs(bot: tool.bots!.first));
|
|
}
|
|
} catch (e) {
|
|
debugPrint('خطا در یافتن ابزار تصویرساز: $e');
|
|
}
|
|
}
|
|
},
|
|
),
|
|
_AiSectionGridItem(
|
|
title: 'ترجمه',
|
|
description: 'ترجمه متون به زبانهای مختلف', // توضیحات اضافه شد
|
|
iconPath: 'lib/assets/icons/translate.svg',
|
|
onTap: (context) {
|
|
final aiState = context.read<AiState>();
|
|
aiState.endChat();
|
|
if (aiState.tools != null && aiState.tools!.length > 2) {
|
|
try {
|
|
final tool = aiState.tools![2];
|
|
if (tool.bots != null && tool.bots!.isNotEmpty) {
|
|
aiState.startChat(AiChatArgs(bot: tool.bots!.first));
|
|
}
|
|
} catch (e) {
|
|
debugPrint('خطا در یافتن ابزار ترجمه: $e');
|
|
}
|
|
}
|
|
},
|
|
),
|
|
_AiSectionGridItem(
|
|
title: 'خلاصهساز',
|
|
description: 'ساخت نمودار با تحلیل هوشمند',
|
|
iconPath: 'lib/assets/icons/summary.svg',
|
|
onTap: (context) {
|
|
final aiState = context.read<AiState>();
|
|
aiState.endChat();
|
|
|
|
// ساخت BotsModel برای Aisummery
|
|
final aisummeryBot = BotsModel(
|
|
id: 100,
|
|
name: 'Aisummery',
|
|
responseType: 'text',
|
|
attachmentType: ['pdf', 'image', 'audio'],
|
|
attachment: 1,
|
|
);
|
|
|
|
aiState.startChat(AiChatArgs(bot: aisummeryBot));
|
|
},
|
|
),
|
|
];
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final aiState = context.watch<AiState>();
|
|
|
|
return Column(
|
|
children: [
|
|
if (!aiState.isChatting)
|
|
const HoshanHomeAppBar(),
|
|
Expanded(
|
|
child: aiState.isChatting
|
|
? AiChatPage(args: aiState.currentChatArgs!)
|
|
: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Row(
|
|
children: [
|
|
SvgPicture.asset('lib/assets/icons/clarity_tools-line.svg'),
|
|
const SizedBox(width: 8,),
|
|
DidvanText(
|
|
'جعبه ابزار استراتژیک هوشان',
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
color: Theme.of(context).colorScheme.title,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(
|
|
height: MediaQuery.of(context).size.height - 200,
|
|
child: _buildAiGrid(context, aiState),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildAiGrid(BuildContext context, AiState aiState) {
|
|
if (aiState.tools == null) {
|
|
if (aiState.loading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
return const Center(
|
|
child: DidvanText('لیست ابزارها هنوز بارگذاری نشده یا خالی است.'));
|
|
}
|
|
|
|
return ListView.builder(
|
|
scrollDirection: Axis.horizontal,
|
|
reverse: true,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
|
|
itemCount: (_gridItems.length / 2).ceil(),
|
|
itemBuilder: (context, columnIndex) {
|
|
return Padding(
|
|
padding: EdgeInsets.only(
|
|
left: columnIndex == (_gridItems.length / 2).ceil() - 1 ? 0 : 16.0,
|
|
right: 16.0,
|
|
),
|
|
child: SizedBox(
|
|
width: 180,
|
|
height: 340, // ارتفاع افزایش یافت (قبلا 300)
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
if (columnIndex * 2 < _gridItems.length)
|
|
SizedBox(
|
|
width: 180,
|
|
height: 160, // ارتفاع کارت افزایش یافت (قبلا 140)
|
|
child: _buildGridItemCard(context, _gridItems[columnIndex * 2]),
|
|
),
|
|
if (columnIndex * 2 + 1 < _gridItems.length)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 16.0),
|
|
child: SizedBox(
|
|
width: 180,
|
|
height: 160, // ارتفاع کارت افزایش یافت (قبلا 140)
|
|
child: _buildGridItemCard(context, _gridItems[columnIndex * 2 + 1]),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildGridItemCard(BuildContext context, _AiSectionGridItem item) {
|
|
return InkWell(
|
|
onTap: () => item.onTap(context),
|
|
borderRadius: BorderRadius.circular(25),
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: const Color.fromARGB(255, 245, 245, 245),
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(
|
|
color: const Color.fromARGB(255, 184, 184, 184),
|
|
width: 1,
|
|
),
|
|
),
|
|
padding: const EdgeInsets.all(12.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
SvgPicture.asset(
|
|
item.iconPath,
|
|
width: 48,
|
|
height: 48,
|
|
),
|
|
const SizedBox(height: 8),
|
|
DidvanText(
|
|
item.title,
|
|
style: Theme.of(context).textTheme.titleSmall,
|
|
color: const Color.fromARGB(255, 0, 126, 167),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
const SizedBox(height: 4), // فاصله برای توضیحات
|
|
Expanded( // برای مدیریت بهتر فضا و جلوگیری از overflow
|
|
child: DidvanText(
|
|
item.description, // نمایش توضیحات
|
|
style: Theme.of(context).textTheme.bodySmall!.copyWith(
|
|
color: Theme.of(context).colorScheme.caption,
|
|
),
|
|
maxLines: 2, // حداکثر 2 خط
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |