From 8a4d520b3d163f10b3dd9e5655c16c997eb03f0a Mon Sep 17 00:00:00 2001 From: flow Date: Tue, 18 Jul 2023 19:44:24 +0800 Subject: [PATCH] improve message --- lib/components/chat.dart | 125 ++++++++++++++--------------------- lib/components/markdown.dart | 1 + lib/components/setting.dart | 3 + lib/main.dart | 19 ++++-- lib/pages/home.dart | 60 +++++++++++++++++ lib/pages/unknown.dart | 11 +++ lib/route.dart | 12 ++-- 7 files changed, 146 insertions(+), 85 deletions(-) create mode 100644 lib/pages/home.dart create mode 100644 lib/pages/unknown.dart diff --git a/lib/components/chat.dart b/lib/components/chat.dart index fd9428e..a790f66 100644 --- a/lib/components/chat.dart +++ b/lib/components/chat.dart @@ -154,82 +154,59 @@ class _ChatWindowState extends State { } Widget _buildMessageCard(Message message) { - if (message.role == Role.user) { - return Column( - children: [ - const Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - SizedBox( - width: 10, - ), - FaIcon(FontAwesomeIcons.person), - SizedBox( - width: 5, - ), - Text("User"), - SizedBox( - width: 10, - ) - ], - ), - Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Flexible( - child: Container( - // padding: const EdgeInsets.all(8), - margin: const EdgeInsets.only( - left: 8, top: 8, right: 8, bottom: 16), - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(8), - ), - child: Card( - color: Colors.blue[100], - child: Padding( - padding: const EdgeInsets.all(8.0), - child: SelectableText( - message.text, - ), - ), - ), - ), - ), - ], - ), - ], - ); - } else { - return Column( - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - const SizedBox( - width: 10, - ), - const FaIcon(FontAwesomeIcons.robot), - const SizedBox( - width: 5, - ), - Text(message.role == Role.system ? "System" : "assistant"), - ], + IconData icon = FontAwesomeIcons.question; + String name = "?"; + Color? color; + Widget? text_box; + switch (message.role) { + case Role.user: + icon = FontAwesomeIcons.fish; + name = "User"; + color = Colors.blue[100]; + text_box = Padding( + padding: const EdgeInsets.all(8.0), + child: SelectableText( + message.text, ), - Row( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - Expanded( - child: Card( - elevation: 8, - margin: const EdgeInsets.all(8), - child: Markdown(text: message.text), - ), - ), - ], - ), - ], - ); + ); + break; + case Role.assistant: + case Role.system: + icon = FontAwesomeIcons.robot; + name = message.role == Role.assistant ? "assistant" : "assistant"; + text_box = Markdown(text: message.text); + break; + default: } + return Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + const SizedBox( + width: 10, + ), + FaIcon(icon), + const SizedBox( + width: 5, + ), + Text(name), + ], + ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + Expanded( + child: Card( + color: color, + margin: const EdgeInsets.all(8), + child: text_box, + ), + ), + ], + ), + ], + ); } void _handleKeyEvent(RawKeyEvent value) { diff --git a/lib/components/markdown.dart b/lib/components/markdown.dart index 34a6c86..3262d0c 100644 --- a/lib/components/markdown.dart +++ b/lib/components/markdown.dart @@ -13,6 +13,7 @@ class Markdown extends StatelessWidget { final config = isDark ? MarkdownConfig.darkConfig : MarkdownConfig.defaultConfig; codeWrapper(child, text) => CodeWrapperWidget(child: child, text: text); + return SelectionArea( child: Padding( padding: const EdgeInsets.all(8.0), diff --git a/lib/components/setting.dart b/lib/components/setting.dart index 33598de..3c85ef4 100644 --- a/lib/components/setting.dart +++ b/lib/components/setting.dart @@ -9,6 +9,9 @@ class SettingPage extends GetResponsiveView { Widget? builder() { const sizedBoxSpace = SizedBox(height: 24); return Scaffold( + appBar: AppBar( + title: Text('settings'.tr), + ), body: GetX(builder: (controller) { return ListView( padding: const EdgeInsets.only(left: 20.0, right: 20.0), diff --git a/lib/main.dart b/lib/main.dart index 06e060f..883276f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,6 @@ +import 'package:flex_color_scheme/flex_color_scheme.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:moyubie/controller/conversation.dart'; import 'package:moyubie/controller/message.dart'; import 'package:moyubie/controller/prompt.dart'; @@ -7,8 +9,11 @@ import 'package:moyubie/components/chat.dart'; import 'package:moyubie/components/setting.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; +import 'package:moyubie/pages/unknown.dart'; +import 'package:moyubie/route.dart'; import 'package:sqflite_common_ffi_web/sqflite_ffi_web.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:moyubie/configs/translations.dart'; import 'package:flutter/foundation.dart' show kIsWeb; import 'dart:io' show Platform; @@ -67,10 +72,6 @@ class MyApp extends StatelessWidget { text: "News", icon: Icon(Icons.newspaper), ), - Tab( - text: "Like", - icon: Icon(Icons.favorite), - ), Tab( text: "Settings", icon: Icon(Icons.settings), @@ -87,13 +88,19 @@ class MyApp extends StatelessWidget { Get.put(MessageController()); Get.put(PromptController()); return MaterialApp( + theme: FlexThemeData.light(scheme: FlexScheme.ebonyClay), + darkTheme: FlexThemeData.dark(scheme: FlexScheme.ebonyClay), + themeMode: ThemeMode.system, + locale: const Locale('zh'), + // translations: MyTranslations(), + builder: EasyLoading.init(), + debugShowCheckedModeBanner: false, home: DefaultTabController( - length: 4, + length: 3, child: Scaffold( bottomNavigationBar: menu(), body: TabBarView( children: [ - Container(child: ChatWindow()), Container(child: ChatWindow()), Container(child: ChatWindow()), Container(child: SettingPage()), diff --git a/lib/pages/home.dart b/lib/pages/home.dart new file mode 100644 index 0000000..4709e18 --- /dev/null +++ b/lib/pages/home.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; +import 'package:moyubie/components/chat.dart'; +import 'package:moyubie/components/conversation.dart'; +import 'package:get/get.dart'; + +class MyHomePage extends GetResponsiveView { + @override + Widget? phone() { + return Scaffold( + appBar: AppBar( + title: Text('appTitle'.tr), + ), + drawer: const ConversationWindow(), + body: const ChatWindow(), + ); + } + + @override + Widget? desktop() { + return Scaffold( + body: Row( + children: const [ + ConversationWindow(), + Expanded(child: ChatWindow()), + ], + ), + ); + } + + @override + Widget? builder() { + return super.builder(); + } + + // @override + // Widget builder() { + // bool useTabs = screen.isPhone || screen.isTablet; + // return Scaffold( + // appBar: useTabs + // ? AppBar( + // title: Text('appTitle'.tr), + // ) + // : null, + // drawer: useTabs ? const ConversationWindow() : null, + // body: Stack( + // children: [ + // useTabs + // ? Row( + // children: const [ + // ChatWindow(), + // ], + // ) + // : Row( + // children: const [ConversationWindow(), ChatWindow()], + // ), + // ], + // ), + // ); + // } +} diff --git a/lib/pages/unknown.dart b/lib/pages/unknown.dart new file mode 100644 index 0000000..1c4cacf --- /dev/null +++ b/lib/pages/unknown.dart @@ -0,0 +1,11 @@ +import 'package:flutter/src/widgets/framework.dart'; +import 'package:flutter/src/widgets/placeholder.dart'; + +class UnknownRoutePage extends StatelessWidget { + const UnknownRoutePage({super.key}); + + @override + Widget build(BuildContext context) { + return const Placeholder(); + } +} diff --git a/lib/route.dart b/lib/route.dart index 881fdd3..4176a2c 100644 --- a/lib/route.dart +++ b/lib/route.dart @@ -1,7 +1,9 @@ import 'package:get/get.dart'; +import 'package:moyubie/pages/home.dart'; -// final routes = [ -// GetPage(name: '/', page: () => MyHomePage()), -// GetPage(name: '/second', page: () => const SecondPage()), -// GetPage(name: '/setting', page: () => SettingPage()) -// ]; +import 'components/setting.dart'; + +final routes = [ + GetPage(name: '/', page: () => MyHomePage()), + GetPage(name: '/setting', page: () => SettingPage()) +];