view.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. physics: const BouncingScrollPhysics(
  138. parent: AlwaysScrollableScrollPhysics()),
  139. controller: controller.listScrollController,
  140. itemBuilder: _chatItemBuilder,
  141. itemCount: controller.chatItems.length),
  142. ),
  143. );
  144. }),
  145. )),
  146. Container(
  147. margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 12.h),
  148. width: 1.sw,
  149. decoration: BoxDecoration(
  150. color: Colors.white,
  151. borderRadius: BorderRadius.circular(12.w),
  152. boxShadow: const [
  153. BoxShadow(
  154. color: Color(0x4CDDDEE8),
  155. blurRadius: 10,
  156. offset: Offset(0, 4),
  157. spreadRadius: 0,
  158. )
  159. ]),
  160. child: Padding(
  161. padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
  162. child: Column(
  163. children: [
  164. Obx(() {
  165. TalkBean? talkInfo = controller.talkInfo.value;
  166. if (talkInfo == null) {
  167. return Container();
  168. } else {
  169. return _buildReferenceFile(talkInfo);
  170. }
  171. }),
  172. Row(
  173. crossAxisAlignment: CrossAxisAlignment.end,
  174. children: [
  175. Expanded(
  176. child: Container(
  177. margin: EdgeInsets.only(right: 6.w),
  178. child: CupertinoTextField(
  179. controller: controller.inputController,
  180. padding: EdgeInsets.symmetric(vertical: 3.w),
  181. style: TextStyle(
  182. fontSize: 14.w, color: ColorName.primaryTextColor),
  183. placeholder: '有问题尽管问我~',
  184. placeholderStyle: TextStyle(
  185. fontSize: 14.w, color: const Color(0xFFAFAFAF)),
  186. textCapitalization: TextCapitalization.sentences,
  187. textInputAction: TextInputAction.newline,
  188. cursorColor: ColorName.colorPrimary,
  189. decoration: const BoxDecoration(),
  190. expands: false,
  191. minLines: 1,
  192. maxLines: 6,
  193. ),
  194. )),
  195. GestureDetector(
  196. onTap: () {
  197. controller.onAddFileClick();
  198. },
  199. child: Image(
  200. image: Assets.images.iconChatAddFile.provider(),
  201. width: 26.w,
  202. height: 26.w),
  203. ),
  204. Container(
  205. margin: EdgeInsets.only(left: 16.w),
  206. child: GestureDetector(
  207. onTap: () {
  208. controller.onSendClick();
  209. },
  210. child: Image(
  211. image: Assets.images.iconChatSend.provider(),
  212. width: 26.w,
  213. height: 26.w),
  214. ),
  215. )
  216. ],
  217. )
  218. ],
  219. ),
  220. ),
  221. ),
  222. ],
  223. );
  224. }
  225. Widget _chatItemBuilder(BuildContext context, int index) {
  226. ChatItem chatItem = controller.chatItems[index];
  227. if (chatItem.role == 'user') {
  228. return _buildUserChatItem(context, chatItem);
  229. } else if (chatItem.role == 'assistant') {
  230. return _buildAssistantChatItem(context, chatItem);
  231. } else {
  232. return Container();
  233. }
  234. }
  235. Widget _buildAssistantChatItem(BuildContext context, ChatItem chatItem) {
  236. ProgressingChatItem? progressingChatItem;
  237. if (chatItem is ProgressingChatItem) {
  238. progressingChatItem = chatItem;
  239. }
  240. return Align(
  241. alignment: Alignment.centerLeft,
  242. child: IntrinsicWidth(
  243. child: progressingChatItem == null
  244. ? _buildAssistantChatItemContent(
  245. null, null, chatItem.content, chatItem.id)
  246. : Obx(() {
  247. bool? isStreamStarted = progressingChatItem == null
  248. ? null
  249. : progressingChatItem.isGradually.value ||
  250. progressingChatItem.isFinished.value ||
  251. progressingChatItem.isFailed.value;
  252. return _buildAssistantChatItemContent(
  253. isStreamStarted,
  254. progressingChatItem?.graduallyController,
  255. progressingChatItem!.isFailed.value
  256. ? progressingChatItem.error.value
  257. : chatItem.content,
  258. chatItem.id);
  259. }),
  260. ),
  261. );
  262. }
  263. Widget _buildAssistantChatItemContent(bool? isStreamStarted,
  264. GraduallyController? graduallyController, String? content, String id) {
  265. return Column(
  266. crossAxisAlignment: CrossAxisAlignment.start,
  267. children: [
  268. SizedBox(height: 10.h),
  269. isStreamStarted != null && isStreamStarted == false
  270. ? Container(
  271. padding: const EdgeInsets.all(1),
  272. decoration: BoxDecoration(
  273. color: ColorName.colorPrimary,
  274. gradient: LinearGradient(
  275. colors: ['#B57AFF'.toColor(), '#4466FF'.toColor()],
  276. stops: const [0, 1.0],
  277. begin: Alignment.topLeft,
  278. end: Alignment.bottomRight,
  279. ),
  280. borderRadius: BorderRadius.only(
  281. topRight: Radius.circular(20.w),
  282. bottomRight: Radius.circular(20.w),
  283. bottomLeft: Radius.circular(20.w))),
  284. child: Container(
  285. decoration: BoxDecoration(
  286. color: ColorName.white,
  287. borderRadius: BorderRadius.only(
  288. topRight: Radius.circular(20.w),
  289. bottomRight: Radius.circular(20.w),
  290. bottomLeft: Radius.circular(20.w))),
  291. padding:
  292. EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  293. child: Lottie.asset(
  294. "assets/anim/anim_chat_response_loading.zip",
  295. width: 46.w,
  296. height: 20.w)),
  297. )
  298. : Container(
  299. decoration: BoxDecoration(
  300. color: ColorName.white,
  301. border: Border.all(color: '#ECECEC'.color, width: 1.w),
  302. borderRadius: BorderRadius.only(
  303. topRight: Radius.circular(20.w),
  304. bottomRight: Radius.circular(20.w),
  305. bottomLeft: Radius.circular(20.w))),
  306. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  307. alignment: Alignment.centerLeft,
  308. constraints: BoxConstraints(
  309. maxWidth: 0.78.sw,
  310. ),
  311. child: GraduallyMdText(
  312. initTxt: content,
  313. graduallyController: graduallyController,
  314. textStyle: TextStyle(
  315. fontSize: 14.w, color: ColorName.primaryTextColor)),
  316. ),
  317. Obx(() {
  318. return Visibility(
  319. visible: id == controller.chatAiTagId.value,
  320. child: Container(
  321. margin: EdgeInsets.only(top: 6.h),
  322. padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 3.w),
  323. decoration: BoxDecoration(
  324. borderRadius: BorderRadius.all(Radius.circular(12.w)),
  325. color: "#EFEEF1".color),
  326. child: Text(
  327. StringName.chatItemAiTag.tr,
  328. style: TextStyle(
  329. height: 1,
  330. fontSize: 10.sp,
  331. color: ColorName.tertiaryTextColor),
  332. ),
  333. ),
  334. );
  335. }),
  336. SizedBox(height: 10.h)
  337. ],
  338. );
  339. }
  340. Widget _buildUserChatItem(BuildContext context, ChatItem chatItem) {
  341. if (chatItem is FileChatItem) {
  342. return _buildUserFileChatItem(context, chatItem);
  343. } else if (chatItem is ReferenceChatItem) {
  344. return _buildUserNormalChatItem(context, chatItem,
  345. referenceTalkTitle: chatItem.talkInfo.title.value);
  346. }
  347. return _buildUserNormalChatItem(context, chatItem,
  348. referenceTalkTitle: chatItem.talkTitle);
  349. }
  350. Widget _buildUserNormalChatItem(BuildContext context, ChatItem chatItem,
  351. {String? referenceTalkTitle}) {
  352. return Align(
  353. alignment: Alignment.centerRight,
  354. child: Column(
  355. crossAxisAlignment: CrossAxisAlignment.end,
  356. children: [
  357. IntrinsicWidth(
  358. child: Container(
  359. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  360. margin: referenceTalkTitle == null
  361. ? EdgeInsets.symmetric(vertical: 10.h)
  362. : EdgeInsets.only(top: 10.h),
  363. alignment: Alignment.centerRight,
  364. constraints: BoxConstraints(
  365. maxWidth: 0.78.sw,
  366. ),
  367. decoration: BoxDecoration(
  368. color: ColorName.colorPrimary,
  369. borderRadius: BorderRadius.only(
  370. topLeft: Radius.circular(16.w),
  371. bottomRight: Radius.circular(16.w),
  372. bottomLeft: Radius.circular(16.w))),
  373. child: SelectableText(chatItem.content,
  374. style: TextStyle(fontSize: 14.w, color: ColorName.white)),
  375. ),
  376. ),
  377. if (referenceTalkTitle != null)
  378. Container(
  379. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  380. margin: EdgeInsets.only(top: 8.h, bottom: 10.h),
  381. constraints: BoxConstraints(
  382. maxWidth: 0.78.sw,
  383. ),
  384. decoration: BoxDecoration(
  385. color: "#EFEFEF".color,
  386. borderRadius: BorderRadius.all(Radius.circular(10.w))),
  387. child: Row(
  388. children: [
  389. Image(
  390. image: Assets.images.iconReferenceChatArrow.provider(),
  391. width: 16.w,
  392. height: 16.w),
  393. Container(
  394. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  395. child: Image(
  396. image: Assets.images.iconReferenceChatFile.provider(),
  397. width: 16.w,
  398. height: 16.w),
  399. ),
  400. Text(referenceTalkTitle,
  401. style: TextStyle(
  402. fontSize: 12.w,
  403. color: ColorName.secondaryTextColor,
  404. overflow: TextOverflow.ellipsis)),
  405. ],
  406. ),
  407. ),
  408. ],
  409. ),
  410. );
  411. }
  412. Widget _buildUserFileChatItem(BuildContext context, FileChatItem chatItem) {
  413. return Align(
  414. alignment: Alignment.centerRight,
  415. child: Container(
  416. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.h),
  417. margin: EdgeInsets.symmetric(vertical: 10.h),
  418. constraints: BoxConstraints(
  419. maxWidth: 0.56.sw,
  420. ),
  421. decoration: BoxDecoration(
  422. color: ColorName.white,
  423. borderRadius: BorderRadius.only(
  424. topLeft: Radius.circular(16.w),
  425. bottomRight: Radius.circular(16.w),
  426. bottomLeft: Radius.circular(16.w)),
  427. border: Border.all(color: "#ECECEC".color, width: 1.w),
  428. ),
  429. child: Row(
  430. crossAxisAlignment: CrossAxisAlignment.center,
  431. children: [
  432. Container(
  433. margin: EdgeInsets.only(right: 6.w),
  434. child: Image(
  435. image: Assets.images.iconFilesFile.provider(),
  436. width: 30.w,
  437. height: 32.w),
  438. ),
  439. Flexible(
  440. child: Column(
  441. crossAxisAlignment: CrossAxisAlignment.start,
  442. children: [
  443. Text(chatItem.talkInfo.title.value.orEmpty,
  444. maxLines: 1,
  445. style: TextStyle(
  446. fontSize: 14.w,
  447. color: ColorName.primaryTextColor,
  448. fontWeight: FontWeight.bold,
  449. overflow: TextOverflow.ellipsis)),
  450. Text(chatItem.talkInfo.summary.value.orEmpty,
  451. maxLines: 1,
  452. style: TextStyle(
  453. fontSize: 12.w,
  454. color: ColorName.secondaryTextColor,
  455. overflow: TextOverflow.ellipsis)),
  456. ],
  457. ),
  458. ),
  459. ],
  460. ),
  461. ),
  462. );
  463. }
  464. _buildReferenceFile(TalkBean talkInfo) {
  465. if (talkInfo.oversizeFile == true) {
  466. return _buildOverSizeReference(talkInfo);
  467. } else {
  468. return _buildNormalReference(talkInfo);
  469. }
  470. }
  471. Container _buildOverSizeReference(TalkBean talkInfo) {
  472. return Container(
  473. margin: EdgeInsets.only(bottom: 14.h),
  474. padding: EdgeInsets.only(left: 8.w, top: 8.h, right: 10.w, bottom: 8.h),
  475. decoration: BoxDecoration(
  476. borderRadius: BorderRadius.all(Radius.circular(8.w)),
  477. border: Border.all(color: "#F0F0F0".color, width: 1.w),
  478. ),
  479. child: Column(
  480. children: [
  481. Row(
  482. children: [
  483. Text(talkInfo.title.value.orEmpty,
  484. style: TextStyle(
  485. fontWeight: FontWeight.bold,
  486. fontSize: 14.w,
  487. color: ColorName.primaryTextColor)),
  488. const Spacer(),
  489. GestureDetector(
  490. onTap: () => controller.onDeleteReference(),
  491. child: Container(
  492. margin: EdgeInsets.only(left: 8.w),
  493. child: Image(
  494. image:
  495. Assets.images.iconReferenceChatDeleteFile.provider(),
  496. width: 18.w,
  497. height: 18.w),
  498. ),
  499. ),
  500. ],
  501. ),
  502. Container(
  503. margin: EdgeInsets.only(top: 11.h),
  504. child: Row(
  505. children: [
  506. Container(
  507. margin: EdgeInsets.only(right: 2.w),
  508. child: Image(
  509. image: Assets.images.iconReferenceChatFile.provider(),
  510. width: 16.w,
  511. height: 16.w),
  512. ),
  513. Text("谈话·超长内容",
  514. style: TextStyle(
  515. fontSize: 12.w, color: ColorName.tertiaryTextColor)),
  516. ],
  517. ),
  518. )
  519. ],
  520. ),
  521. );
  522. }
  523. _buildNormalReference(TalkBean talkInfo) {
  524. return Container(
  525. decoration: BoxDecoration(
  526. color: "#F6F6F6".color,
  527. borderRadius: BorderRadius.all(Radius.circular(6.w)),
  528. ),
  529. padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 7.h),
  530. margin: EdgeInsets.only(bottom: 14.h),
  531. child: Row(
  532. children: [
  533. Image(
  534. image: Assets.images.iconReferenceChatArrow.provider(),
  535. width: 16.w,
  536. height: 16.w),
  537. Container(
  538. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  539. child: Image(
  540. image: Assets.images.iconReferenceChatFile.provider(),
  541. width: 16.w,
  542. height: 16.w),
  543. ),
  544. Text(talkInfo.title.value.orEmpty,
  545. overflow: TextOverflow.ellipsis,
  546. maxLines: 1,
  547. style: TextStyle(
  548. fontSize: 12.w,
  549. color: ColorName.primaryTextColor,
  550. overflow: TextOverflow.ellipsis)),
  551. const Spacer(),
  552. Container(
  553. margin: EdgeInsets.only(left: 8.w),
  554. child: GestureDetector(
  555. onTap: () => controller.onDeleteReference(),
  556. child: Image(
  557. image: Assets.images.iconReferenceChatDeleteFile.provider(),
  558. width: 18.w,
  559. height: 18.w),
  560. ),
  561. ),
  562. ],
  563. ),
  564. );
  565. }
  566. Widget _buildTopGradient() {
  567. return Container(
  568. width: 1.sw,
  569. height: 128.h,
  570. decoration: BoxDecoration(
  571. gradient: LinearGradient(
  572. colors: ['#E8EBFF'.toColor(), '#00E8EBFF'.toColor()],
  573. begin: Alignment.topCenter,
  574. end: Alignment.bottomCenter,
  575. stops: const [0.5, 1.0],
  576. ),
  577. ));
  578. }
  579. Widget _buildBackgroundGradient() {
  580. return Container(
  581. width: 1.sw,
  582. height: 1.sh,
  583. decoration: BoxDecoration(
  584. gradient: LinearGradient(
  585. colors: ['#F2F8F4'.toColor(), '#F6F6F6'.toColor()],
  586. begin: Alignment.topCenter,
  587. end: Alignment.bottomCenter,
  588. stops: const [0, 1.0],
  589. ),
  590. ),
  591. );
  592. }
  593. }