view.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. inlineSyntaxes:
  271. md.ExtensionSet.gitHubWeb.inlineSyntaxes,
  272. blockSyntaxes:
  273. md.ExtensionSet.gitHubWeb.blockSyntaxes),
  274. textStyle: TextStyle(
  275. fontSize: 14.w, color: ColorName.primaryTextColor),
  276. ),
  277. ));
  278. }
  279. Widget _buildUserChatItem(BuildContext context, ChatItem chatItem) {
  280. if (chatItem is FileChatItem) {
  281. return _buildUserFileChatItem(context, chatItem);
  282. } else if (chatItem is ReferenceChatItem) {
  283. return _buildUserNormalChatItem(context, chatItem,
  284. referenceTalkTitle: chatItem.talkInfo.title.value);
  285. }
  286. return _buildUserNormalChatItem(context, chatItem,
  287. referenceTalkTitle: chatItem.talkTitle);
  288. }
  289. Widget _buildUserNormalChatItem(BuildContext context, ChatItem chatItem,
  290. {String? referenceTalkTitle}) {
  291. return Align(
  292. alignment: Alignment.centerRight,
  293. child: Column(
  294. crossAxisAlignment: CrossAxisAlignment.end,
  295. children: [
  296. IntrinsicWidth(
  297. child: Container(
  298. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  299. margin: referenceTalkTitle == null
  300. ? EdgeInsets.symmetric(vertical: 10.h)
  301. : EdgeInsets.only(top: 10.h),
  302. alignment: Alignment.centerRight,
  303. constraints: BoxConstraints(
  304. maxWidth: 0.78.sw,
  305. ),
  306. decoration: BoxDecoration(
  307. color: ColorName.colorPrimary,
  308. borderRadius: BorderRadius.only(
  309. topLeft: Radius.circular(16.w),
  310. bottomRight: Radius.circular(16.w),
  311. bottomLeft: Radius.circular(16.w))),
  312. child: SelectableText(chatItem.content,
  313. style: TextStyle(fontSize: 14.w, color: ColorName.white)),
  314. ),
  315. ),
  316. if (referenceTalkTitle != null)
  317. Container(
  318. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
  319. margin: EdgeInsets.only(top: 8.h, bottom: 10.h),
  320. constraints: BoxConstraints(
  321. maxWidth: 0.78.sw,
  322. ),
  323. decoration: BoxDecoration(
  324. color: "#EFEFEF".color,
  325. borderRadius: BorderRadius.all(Radius.circular(10.w))),
  326. child: Row(
  327. children: [
  328. Image(
  329. image: Assets.images.iconReferenceChatArrow.provider(),
  330. width: 16.w,
  331. height: 16.w),
  332. Container(
  333. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  334. child: Image(
  335. image: Assets.images.iconReferenceChatFile.provider(),
  336. width: 16.w,
  337. height: 16.w),
  338. ),
  339. Text(referenceTalkTitle,
  340. style: TextStyle(
  341. fontSize: 12.w,
  342. color: ColorName.secondaryTextColor,
  343. overflow: TextOverflow.ellipsis)),
  344. ],
  345. ),
  346. ),
  347. ],
  348. ),
  349. );
  350. }
  351. Widget _buildUserFileChatItem(BuildContext context, FileChatItem chatItem) {
  352. return Align(
  353. alignment: Alignment.centerRight,
  354. child: Container(
  355. padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.h),
  356. margin: EdgeInsets.symmetric(vertical: 10.h),
  357. constraints: BoxConstraints(
  358. maxWidth: 0.56.sw,
  359. ),
  360. decoration: BoxDecoration(
  361. color: ColorName.white,
  362. borderRadius: BorderRadius.only(
  363. topLeft: Radius.circular(16.w),
  364. bottomRight: Radius.circular(16.w),
  365. bottomLeft: Radius.circular(16.w)),
  366. border: Border.all(color: "#ECECEC".color, width: 1.w),
  367. ),
  368. child: Row(
  369. crossAxisAlignment: CrossAxisAlignment.center,
  370. children: [
  371. Container(
  372. margin: EdgeInsets.only(right: 6.w),
  373. child: Image(
  374. image: Assets.images.iconFilesFile.provider(),
  375. width: 30.w,
  376. height: 32.w),
  377. ),
  378. Flexible(
  379. child: Column(
  380. crossAxisAlignment: CrossAxisAlignment.start,
  381. children: [
  382. Text(chatItem.talkInfo.title.value.orEmpty,
  383. maxLines: 1,
  384. style: TextStyle(
  385. fontSize: 14.w,
  386. color: ColorName.primaryTextColor,
  387. fontWeight: FontWeight.bold,
  388. overflow: TextOverflow.ellipsis)),
  389. Text(chatItem.talkInfo.summary.value.orEmpty,
  390. maxLines: 1,
  391. style: TextStyle(
  392. fontSize: 12.w,
  393. color: ColorName.secondaryTextColor,
  394. overflow: TextOverflow.ellipsis)),
  395. ],
  396. ),
  397. ),
  398. ],
  399. ),
  400. ),
  401. );
  402. }
  403. _buildReferenceFile(TalkBean talkInfo) {
  404. if (talkInfo.oversizeFile == true) {
  405. return _buildOverSizeReference(talkInfo);
  406. } else {
  407. return _buildNormalReference(talkInfo);
  408. }
  409. }
  410. Container _buildOverSizeReference(TalkBean talkInfo) {
  411. return Container(
  412. margin: EdgeInsets.only(bottom: 14.h),
  413. padding: EdgeInsets.only(left: 8.w, top: 8.h, right: 10.w, bottom: 8.h),
  414. decoration: BoxDecoration(
  415. borderRadius: BorderRadius.all(Radius.circular(8.w)),
  416. border: Border.all(color: "#F0F0F0".color, width: 1.w),
  417. ),
  418. child: Column(
  419. children: [
  420. Row(
  421. children: [
  422. Text(talkInfo.title.value.orEmpty,
  423. style: TextStyle(
  424. fontWeight: FontWeight.bold,
  425. fontSize: 14.w,
  426. color: ColorName.primaryTextColor)),
  427. const Spacer(),
  428. Container(
  429. margin: EdgeInsets.only(left: 8.w),
  430. child: Image(
  431. image: Assets.images.iconReferenceChatDeleteFile.provider(),
  432. width: 18.w,
  433. height: 18.w),
  434. ),
  435. ],
  436. ),
  437. Container(
  438. margin: EdgeInsets.only(top: 11.h),
  439. child: Row(
  440. children: [
  441. Container(
  442. margin: EdgeInsets.only(right: 2.w),
  443. child: Image(
  444. image: Assets.images.iconReferenceChatFile.provider(),
  445. width: 16.w,
  446. height: 16.w),
  447. ),
  448. Text("谈话·超长内容",
  449. style: TextStyle(
  450. fontSize: 12.w, color: ColorName.tertiaryTextColor)),
  451. ],
  452. ),
  453. )
  454. ],
  455. ),
  456. );
  457. }
  458. _buildNormalReference(TalkBean talkInfo) {
  459. return Container(
  460. margin: EdgeInsets.only(bottom: 14.h),
  461. child: Row(
  462. children: [
  463. Image(
  464. image: Assets.images.iconReferenceChatArrow.provider(),
  465. width: 16.w,
  466. height: 16.w),
  467. Container(
  468. margin: EdgeInsets.only(right: 2.w, left: 4.w),
  469. child: Image(
  470. image: Assets.images.iconReferenceChatFile.provider(),
  471. width: 16.w,
  472. height: 16.w),
  473. ),
  474. Text(talkInfo.title.value.orEmpty,
  475. overflow: TextOverflow.ellipsis,
  476. maxLines: 1,
  477. style: TextStyle(
  478. fontSize: 12.w,
  479. color: ColorName.primaryTextColor,
  480. overflow: TextOverflow.ellipsis)),
  481. const Spacer(),
  482. Container(
  483. margin: EdgeInsets.only(left: 8.w),
  484. color: "#F6F6F6".color,
  485. child: GestureDetector(
  486. onTap: () => controller.onDeleteReference(),
  487. child: Image(
  488. image: Assets.images.iconReferenceChatDeleteFile.provider(),
  489. width: 18.w,
  490. height: 18.w),
  491. ),
  492. ),
  493. ],
  494. ),
  495. );
  496. }
  497. Widget _buildTopGradient() {
  498. return Container(
  499. width: 1.sw,
  500. height: 128.h,
  501. decoration: BoxDecoration(
  502. gradient: LinearGradient(
  503. colors: ['#E8EBFF'.toColor(), '#00E8EBFF'.toColor()],
  504. begin: Alignment.topCenter,
  505. end: Alignment.bottomCenter,
  506. stops: const [0.5, 1.0],
  507. ),
  508. ));
  509. }
  510. Widget _buildBackgroundGradient() {
  511. return Container(
  512. width: 1.sw,
  513. height: 1.sh,
  514. decoration: BoxDecoration(
  515. gradient: LinearGradient(
  516. colors: ['#F2F8F4'.toColor(), '#F6F6F6'.toColor()],
  517. begin: Alignment.topCenter,
  518. end: Alignment.bottomCenter,
  519. stops: const [0, 1.0],
  520. ),
  521. ),
  522. );
  523. }
  524. }