view.dart 19 KB

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