Skip to content

Commit

Permalink
improve message
Browse files Browse the repository at this point in the history
  • Loading branch information
flowbehappy committed Jul 18, 2023
1 parent 8676fa0 commit 8a4d520
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 85 deletions.
125 changes: 51 additions & 74 deletions lib/components/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,82 +154,59 @@ class _ChatWindowState extends State<ChatWindow> {
}

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) {
Expand Down
1 change: 1 addition & 0 deletions lib/components/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions lib/components/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class SettingPage extends GetResponsiveView<SettingsController> {
Widget? builder() {
const sizedBoxSpace = SizedBox(height: 24);
return Scaffold(
appBar: AppBar(
title: Text('settings'.tr),
),
body: GetX<SettingsController>(builder: (controller) {
return ListView(
padding: const EdgeInsets.only(left: 20.0, right: 20.0),
Expand Down
19 changes: 13 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down Expand Up @@ -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),
Expand All @@ -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()),
Expand Down
60 changes: 60 additions & 0 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -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()],
// ),
// ],
// ),
// );
// }
}
11 changes: 11 additions & 0 deletions lib/pages/unknown.dart
Original file line number Diff line number Diff line change
@@ -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();
}
}
12 changes: 7 additions & 5 deletions lib/route.dart
Original file line number Diff line number Diff line change
@@ -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())
];

0 comments on commit 8a4d520

Please sign in to comment.