view.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. import 'package:electronic_assistant/base/base_page.dart';
  2. import 'package:electronic_assistant/data/bean/agenda.dart';
  3. import 'package:electronic_assistant/data/bean/chat_item.dart';
  4. import 'package:electronic_assistant/data/bean/file_chat_item.dart';
  5. import 'package:electronic_assistant/data/bean/reference_chat_item.dart';
  6. import 'package:electronic_assistant/module/chat/controller.dart';
  7. import 'package:electronic_assistant/resource/colors.gen.dart';
  8. import 'package:electronic_assistant/resource/string.gen.dart';
  9. import 'package:electronic_assistant/utils/expand.dart';
  10. import 'package:electronic_assistant/widget/gradually_md_text.dart';
  11. import 'package:flutter/cupertino.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:flutter/services.dart';
  14. import 'package:flutter_screenutil/flutter_screenutil.dart';
  15. import 'package:get/get.dart';
  16. import 'package:lottie/lottie.dart';
  17. import 'package:pull_to_refresh/pull_to_refresh.dart';
  18. import '../../data/bean/progressing_chat_item.dart';
  19. import '../../data/bean/talks.dart';
  20. import '../../resource/assets.gen.dart';
  21. import '../../router/app_pages.dart';
  22. enum ChatFromType {
  23. fromMain,
  24. fromTalkDetail,
  25. fromAnalysisBtn,
  26. fromTalkExample,
  27. fromMine,
  28. unknown
  29. }
  30. class ChatPage extends BasePage<ChatController> {
  31. const ChatPage({super.key});
  32. static start(ChatFromType fromType) {
  33. Get.toNamed(RoutePath.chat, arguments: [fromType]);
  34. }
  35. static startByTalk(ChatFromType fromType, TalkBean talkInfo,
  36. {Agenda? agenda}) {
  37. Get.toNamed(RoutePath.chat, arguments: [fromType, talkInfo, agenda]);
  38. }
  39. static startByTalkId(ChatFromType fromType, String talkId, {Agenda? agenda}) {
  40. Get.toNamed(RoutePath.chat, arguments: [fromType, talkId, agenda]);
  41. }
  42. @override
  43. bool immersive() {
  44. return true;
  45. }
  46. @override
  47. Color navigationBarColor() {
  48. return "#F6F6F6".color;
  49. }
  50. @override
  51. Widget buildBody(BuildContext context) {
  52. // 第一次启动时弹出定制窗口
  53. return Stack(
  54. children: [
  55. _buildBackgroundGradient(),
  56. _buildTopGradient(),
  57. Scaffold(
  58. backgroundColor: Colors.transparent,
  59. appBar: AppBar(
  60. leading: IconButton(
  61. icon: SizedBox(
  62. width: 24.w,
  63. height: 24.w,
  64. child: Assets.images.iconBack.image()),
  65. onPressed: () {
  66. Get.back();
  67. },
  68. ),
  69. scrolledUnderElevation: 0,
  70. backgroundColor: Colors.transparent,
  71. systemOverlayStyle: SystemUiOverlayStyle.dark,
  72. centerTitle: true,
  73. title: IntrinsicWidth(
  74. child: Row(
  75. mainAxisAlignment: MainAxisAlignment.center,
  76. children: [
  77. Image(
  78. image: Assets.images.iconChatXiaoTin.provider(),
  79. width: 28.w,
  80. height: 28.w),
  81. Container(
  82. margin: EdgeInsets.only(left: 6.w),
  83. child: Text('聊天',
  84. style: TextStyle(
  85. fontSize: 16.w,
  86. fontWeight: FontWeight.bold,
  87. color: ColorName.primaryTextColor))),
  88. ],
  89. ),
  90. ),
  91. ),
  92. body: buildBodyContent(context),
  93. )
  94. ],
  95. );
  96. }
  97. Widget buildBodyContent(BuildContext context) {
  98. return Column(
  99. children: [
  100. Expanded(
  101. child: Container(
  102. padding: EdgeInsets.symmetric(horizontal: 12.w),
  103. child: Obx(() {
  104. return NotificationListener<ScrollNotification>(
  105. onNotification: (scrollNotification) {
  106. if (scrollNotification is ScrollStartNotification) {
  107. FocusScope.of(context).unfocus();
  108. }
  109. return false;
  110. },
  111. child: SmartRefresher(
  112. controller: controller.refreshController,
  113. footer: CustomFooter(
  114. loadStyle: LoadStyle.ShowWhenLoading,
  115. builder: (context, mode) {
  116. if (mode == LoadStatus.loading ||
  117. mode == LoadStatus.canLoading) {
  118. return const SizedBox(
  119. height: 60.0,
  120. child: SizedBox(
  121. height: 20.0,
  122. width: 20.0,
  123. child: CupertinoActivityIndicator(),
  124. ),
  125. );
  126. } else {
  127. return Container();
  128. }
  129. },
  130. ),
  131. enablePullDown: false,
  132. enablePullUp: true,
  133. onLoading: controller.loadMoreHistory,
  134. onRefresh: controller.loadMoreHistory,
  135. child: ListView.builder(
  136. reverse: true,
  137. controller: controller.listScrollController,
  138. itemBuilder: _chatItemBuilder,
  139. itemCount: controller.chatItems.length),
  140. ),
  141. );
  142. }),
  143. )),
  144. Container(
  145. margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.h),
  146. width: 1.sw,
  147. decoration: BoxDecoration(
  148. color: Colors.white,
  149. borderRadius: BorderRadius.circular(12.w),
  150. boxShadow: const [
  151. BoxShadow(
  152. color: Color(0x4CDDDEE8),
  153. blurRadius: 10,
  154. offset: Offset(0, 4),
  155. spreadRadius: 0,
  156. )
  157. ]),
  158. child: Padding(
  159. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
  160. child: Column(
  161. children: [
  162. Obx(() {
  163. TalkBean? talkInfo = controller.talkInfo.value;
  164. if (talkInfo == null) {
  165. return Container();
  166. } else {
  167. return _buildReferenceFile(talkInfo);
  168. }
  169. }),
  170. Row(
  171. crossAxisAlignment: CrossAxisAlignment.end,
  172. children: [
  173. Expanded(
  174. child: Container(
  175. margin: EdgeInsets.only(right: 6.w),
  176. child: CupertinoTextField(
  177. controller: controller.inputController,
  178. padding: EdgeInsets.symmetric(vertical: 3.w),
  179. style: TextStyle(
  180. fontSize: 14.w, color: ColorName.primaryTextColor),
  181. placeholder: '有问题尽管问我~',
  182. placeholderStyle: TextStyle(
  183. fontSize: 14.w, color: const Color(0xFFAFAFAF)),
  184. textCapitalization: TextCapitalization.sentences,
  185. textInputAction: TextInputAction.newline,
  186. cursorColor: ColorName.colorPrimary,
  187. decoration: const BoxDecoration(),
  188. expands: false,
  189. minLines: 1,
  190. maxLines: 6,
  191. ),
  192. )),
  193. GestureDetector(
  194. onTap: () {
  195. controller.onAddFileClick();
  196. },
  197. child: Image(
  198. image: Assets.images.iconChatAddFile.provider(),
  199. width: 26.w,
  200. height: 26.w),
  201. ),
  202. Container(
  203. margin: EdgeInsets.only(left: 16.w),
  204. child: GestureDetector(
  205. onTap: () {
  206. controller.onSendClick();
  207. },
  208. child: Image(
  209. image: Assets.images.iconChatSend.provider(),
  210. width: 26.w,
  211. height: 26.w),
  212. ),
  213. )
  214. ],
  215. )
  216. ],
  217. ),
  218. ),
  219. ),
  220. ],
  221. );
  222. }
  223. Widget _chatItemBuilder(BuildContext context, int index) {
  224. ChatItem chatItem = controller.chatItems[index];
  225. if (chatItem.role == 'user') {
  226. return _buildUserChatItem(context, chatItem);
  227. } else if (chatItem.role == 'assistant') {
  228. return _buildAssistantChatItem(context, chatItem);
  229. } else {
  230. return Container();
  231. }
  232. }
  233. Widget _buildAssistantChatItem(BuildContext context, ChatItem chatItem) {
  234. ProgressingChatItem? progressingChatItem;
  235. if (chatItem is ProgressingChatItem) {
  236. progressingChatItem = chatItem;
  237. }
  238. return Align(
  239. alignment: Alignment.centerLeft,
  240. child: IntrinsicWidth(
  241. child: progressingChatItem == null
  242. ? _buildAssistantChatItemContent(
  243. null, null, chatItem.content, chatItem.id)
  244. : Obx(() {
  245. bool? isStreamStarted = progressingChatItem == null
  246. ? null
  247. : progressingChatItem.isGradually.value ||
  248. progressingChatItem.isFinished.value ||
  249. progressingChatItem.isFailed.value;
  250. return _buildAssistantChatItemContent(
  251. isStreamStarted,
  252. progressingChatItem?.graduallyController,
  253. progressingChatItem!.isFailed.value
  254. ? progressingChatItem.error.value
  255. : chatItem.content,
  256. chatItem.id);
  257. }),
  258. ),
  259. );
  260. }
  261. Widget _buildAssistantChatItemContent(bool? isStreamStarted,
  262. GraduallyController? graduallyController, String? content, String id) {
  263. return Column(
  264. crossAxisAlignment: CrossAxisAlignment.start,
  265. children: [
  266. SizedBox(height: 10.h),
  267. isStreamStarted != null && isStreamStarted == false
  268. ? Container(
  269. padding: const EdgeInsets.all(1),
  270. decoration: BoxDecoration(
  271. color: ColorName.colorPrimary,
  272. gradient: LinearGradient(
  273. colors: ['#B57AFF'.toColor(), '#4466FF'.toColor()],
  274. stops: const [0, 1.0],
  275. begin: Alignment.topLeft,
  276. end: Alignment.bottomRight,
  277. ),
  278. borderRadius: BorderRadius.only(
  279. topRight: Radius.circular(20.w),
  280. bottomRight: Radius.circular(20.w),
  281. bottomLeft: Radius.circular(20.w))),
  282. child: Container(
  283. decoration: BoxDecoration(
  284. color: ColorName.white,
  285. borderRadius: BorderRadius.only(
  286. topRight: Radius.circular(20.w),
  287. bottomRight: Radius.circular(20.w),
  288. bottomLeft: Radius.circular(20.w))),
  289. padding:
  290. EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  291. child: Lottie.asset(
  292. "assets/anim/anim_chat_response_loading.zip",
  293. width: 46.w,
  294. height: 20.w)),
  295. )
  296. : Container(
  297. decoration: BoxDecoration(
  298. color: ColorName.white,
  299. border: Border.all(color: '#ECECEC'.color, width: 1.w),
  300. borderRadius: BorderRadius.only(
  301. topRight: Radius.circular(20.w),
  302. bottomRight: Radius.circular(20.w),
  303. bottomLeft: Radius.circular(20.w))),
  304. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  305. alignment: Alignment.centerLeft,
  306. constraints: BoxConstraints(
  307. maxWidth: 0.78.sw,
  308. ),
  309. child: GraduallyMdText(
  310. initTxt: content,
  311. graduallyController: graduallyController,
  312. textStyle: TextStyle(
  313. fontSize: 14.w, color: ColorName.primaryTextColor)),
  314. ),
  315. Obx(() {
  316. return Visibility(
  317. visible: id == controller.chatAiTagId.value,
  318. child: Container(
  319. margin: EdgeInsets.only(top: 6.h),
  320. padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 3.w),
  321. decoration: BoxDecoration(
  322. borderRadius: BorderRadius.all(Radius.circular(12.w)),
  323. color: "#EFEEF1".color),
  324. child: Text(
  325. StringName.chatItemAiTag.tr,
  326. style: TextStyle(
  327. height: 1,
  328. fontSize: 10.sp,
  329. color: ColorName.tertiaryTextColor),
  330. ),
  331. ),
  332. );
  333. }),
  334. SizedBox(height: 10.h)
  335. ],
  336. );
  337. }
  338. Widget _buildUserChatItem(BuildContext context, ChatItem chatItem) {
  339. if (chatItem is FileChatItem) {
  340. return _buildUserFileChatItem(context, chatItem);
  341. } else if (chatItem is ReferenceChatItem) {
  342. return _buildUserNormalChatItem(context, chatItem,
  343. referenceTalkTitle: chatItem.talkInfo.title.value);
  344. }
  345. return _buildUserNormalChatItem(context, chatItem,
  346. referenceTalkTitle: chatItem.talkTitle);
  347. }
  348. Widget _buildUserNormalChatItem(BuildContext context, ChatItem chatItem,
  349. {String? referenceTalkTitle}) {
  350. return Align(
  351. alignment: Alignment.centerRight,
  352. child: Column(
  353. crossAxisAlignment: CrossAxisAlignment.end,
  354. children: [
  355. IntrinsicWidth(
  356. child: Container(
  357. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  358. margin: referenceTalkTitle == null
  359. ? EdgeInsets.symmetric(vertical: 10.h)
  360. : EdgeInsets.only(top: 10.h),
  361. alignment: Alignment.centerRight,
  362. constraints: BoxConstraints(
  363. maxWidth: 0.78.sw,
  364. ),
  365. decoration: BoxDecoration(
  366. color: ColorName.colorPrimary,
  367. borderRadius: BorderRadius.only(
  368. topLeft: Radius.circular(16.w),
  369. bottomRight: Radius.circular(16.w),
  370. bottomLeft: Radius.circular(16.w))),
  371. child: SelectableText(chatItem.content,
  372. style: TextStyle(fontSize: 14.w, color: ColorName.white)),
  373. ),
  374. ),
  375. if (referenceTalkTitle != null)
  376. Container(
  377. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  378. margin: EdgeInsets.only(top: 8.h, bottom: 10.h),
  379. constraints: BoxConstraints(
  380. maxWidth: 0.78.sw,
  381. ),
  382. decoration: BoxDecoration(
  383. color: "#EFEFEF".color,
  384. borderRadius: BorderRadius.all(Radius.circular(10.w))),
  385. child: Row(
  386. children: [
  387. Image(
  388. image: Assets.images.iconReferenceChatArrow.provider(),
  389. width: 16.w,
  390. height: 16.w),
  391. Container(
  392. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  393. child: Image(
  394. image: Assets.images.iconReferenceChatFile.provider(),
  395. width: 16.w,
  396. height: 16.w),
  397. ),
  398. Text(referenceTalkTitle,
  399. style: TextStyle(
  400. fontSize: 12.w,
  401. color: ColorName.secondaryTextColor,
  402. overflow: TextOverflow.ellipsis)),
  403. ],
  404. ),
  405. ),
  406. ],
  407. ),
  408. );
  409. }
  410. Widget _buildUserFileChatItem(BuildContext context, FileChatItem chatItem) {
  411. return Align(
  412. alignment: Alignment.centerRight,
  413. child: Container(
  414. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.h),
  415. margin: EdgeInsets.symmetric(vertical: 10.h),
  416. constraints: BoxConstraints(
  417. maxWidth: 0.56.sw,
  418. ),
  419. decoration: BoxDecoration(
  420. color: ColorName.white,
  421. borderRadius: BorderRadius.only(
  422. topLeft: Radius.circular(16.w),
  423. bottomRight: Radius.circular(16.w),
  424. bottomLeft: Radius.circular(16.w)),
  425. border: Border.all(color: "#ECECEC".color, width: 1.w),
  426. ),
  427. child: Row(
  428. crossAxisAlignment: CrossAxisAlignment.center,
  429. children: [
  430. Container(
  431. margin: EdgeInsets.only(right: 6.w),
  432. child: Image(
  433. image: Assets.images.iconFilesFile.provider(),
  434. width: 30.w,
  435. height: 32.w),
  436. ),
  437. Flexible(
  438. child: Column(
  439. crossAxisAlignment: CrossAxisAlignment.start,
  440. children: [
  441. Text(chatItem.talkInfo.title.value.orEmpty,
  442. maxLines: 1,
  443. style: TextStyle(
  444. fontSize: 14.w,
  445. color: ColorName.primaryTextColor,
  446. fontWeight: FontWeight.bold,
  447. overflow: TextOverflow.ellipsis)),
  448. Text(chatItem.talkInfo.summary.value.orEmpty,
  449. maxLines: 1,
  450. style: TextStyle(
  451. fontSize: 12.w,
  452. color: ColorName.secondaryTextColor,
  453. overflow: TextOverflow.ellipsis)),
  454. ],
  455. ),
  456. ),
  457. ],
  458. ),
  459. ),
  460. );
  461. }
  462. _buildReferenceFile(TalkBean talkInfo) {
  463. if (talkInfo.oversizeFile == true) {
  464. return _buildOverSizeReference(talkInfo);
  465. } else {
  466. return _buildNormalReference(talkInfo);
  467. }
  468. }
  469. Container _buildOverSizeReference(TalkBean talkInfo) {
  470. return Container(
  471. margin: EdgeInsets.only(bottom: 14.h),
  472. padding: EdgeInsets.only(left: 8.w, top: 8.h, right: 10.w, bottom: 8.h),
  473. decoration: BoxDecoration(
  474. borderRadius: BorderRadius.all(Radius.circular(8.w)),
  475. border: Border.all(color: "#F0F0F0".color, width: 1.w),
  476. ),
  477. child: Column(
  478. children: [
  479. Row(
  480. children: [
  481. Text(talkInfo.title.value.orEmpty,
  482. style: TextStyle(
  483. fontWeight: FontWeight.bold,
  484. fontSize: 14.w,
  485. color: ColorName.primaryTextColor)),
  486. const Spacer(),
  487. GestureDetector(
  488. onTap: () => controller.onDeleteReference(),
  489. child: Container(
  490. margin: EdgeInsets.only(left: 8.w),
  491. child: Image(
  492. image:
  493. Assets.images.iconReferenceChatDeleteFile.provider(),
  494. width: 18.w,
  495. height: 18.w),
  496. ),
  497. ),
  498. ],
  499. ),
  500. Container(
  501. margin: EdgeInsets.only(top: 11.h),
  502. child: Row(
  503. children: [
  504. Container(
  505. margin: EdgeInsets.only(right: 2.w),
  506. child: Image(
  507. image: Assets.images.iconReferenceChatFile.provider(),
  508. width: 16.w,
  509. height: 16.w),
  510. ),
  511. Text("谈话·超长内容",
  512. style: TextStyle(
  513. fontSize: 12.w, color: ColorName.tertiaryTextColor)),
  514. ],
  515. ),
  516. )
  517. ],
  518. ),
  519. );
  520. }
  521. _buildNormalReference(TalkBean talkInfo) {
  522. return Container(
  523. decoration: BoxDecoration(
  524. color: "#F6F6F6".color,
  525. borderRadius: BorderRadius.all(Radius.circular(6.w)),
  526. ),
  527. padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 7.h),
  528. margin: EdgeInsets.only(bottom: 14.h),
  529. child: Row(
  530. children: [
  531. Image(
  532. image: Assets.images.iconReferenceChatArrow.provider(),
  533. width: 16.w,
  534. height: 16.w),
  535. Container(
  536. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  537. child: Image(
  538. image: Assets.images.iconReferenceChatFile.provider(),
  539. width: 16.w,
  540. height: 16.w),
  541. ),
  542. Text(talkInfo.title.value.orEmpty,
  543. overflow: TextOverflow.ellipsis,
  544. maxLines: 1,
  545. style: TextStyle(
  546. fontSize: 12.w,
  547. color: ColorName.primaryTextColor,
  548. overflow: TextOverflow.ellipsis)),
  549. const Spacer(),
  550. Container(
  551. margin: EdgeInsets.only(left: 8.w),
  552. child: GestureDetector(
  553. onTap: () => controller.onDeleteReference(),
  554. child: Image(
  555. image: Assets.images.iconReferenceChatDeleteFile.provider(),
  556. width: 18.w,
  557. height: 18.w),
  558. ),
  559. ),
  560. ],
  561. ),
  562. );
  563. }
  564. Widget _buildTopGradient() {
  565. return Container(
  566. width: 1.sw,
  567. height: 128.h,
  568. decoration: BoxDecoration(
  569. gradient: LinearGradient(
  570. colors: ['#E8EBFF'.toColor(), '#00E8EBFF'.toColor()],
  571. begin: Alignment.topCenter,
  572. end: Alignment.bottomCenter,
  573. stops: const [0.5, 1.0],
  574. ),
  575. ));
  576. }
  577. Widget _buildBackgroundGradient() {
  578. return Container(
  579. width: 1.sw,
  580. height: 1.sh,
  581. decoration: BoxDecoration(
  582. gradient: LinearGradient(
  583. colors: ['#F2F8F4'.toColor(), '#F6F6F6'.toColor()],
  584. begin: Alignment.topCenter,
  585. end: Alignment.bottomCenter,
  586. stops: const [0, 1.0],
  587. ),
  588. ),
  589. );
  590. }
  591. }