Skip to content

Commit

Permalink
new: option of scroll switch chat (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit authored Jun 21, 2024
1 parent 649ded8 commit 9be4b06
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
2 changes: 2 additions & 0 deletions lib/data/store/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ class SettingStore extends PersistentStore {

/// Save the chat after each message sent or received even if it has error.
late final saveErrChat = property('saveErrChat', false);

late final scrollSwitchChat = property('scrollSwitchChat', true);
}
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"save": "Save",
"saveErrChat": "Save chat with errors",
"saveErrChatTip": "Save the chat after each message sent or received even if it has error.",
"scrollSwitchChat": "Scroll to switch chat",
"search": "Search",
"secretKey": "Secret key",
"select": "Select",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"save": "保存",
"saveErrChat": "保存聊天错误",
"saveErrChatTip": "在接收/发送每条消息后保存,即使有报错",
"scrollSwitchChat": "上下滑动切换聊天",
"search": "搜索",
"secretKey": "密钥",
"select": "选择",
Expand Down
70 changes: 39 additions & 31 deletions lib/view/page/home/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,44 @@ class _ChatPageState extends State<_ChatPage>

Widget _buildChat() {
var switchDirection = SwitchDirection.next;
final scrollSwitchChat = Stores.setting.scrollSwitchChat.fetch();

final child = ListenBuilder(
listenable: _chatRN,
builder: () {
final item = _curChat?.items;
if (item == null) return UIs.placeholder;
final listView = ListView.builder(
key: Key(_curChatId),
controller: _chatScrollCtrl,
padding: const EdgeInsets.all(7),
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
itemCount: item.length,
itemBuilder: (_, index) {
return _buildChatItem(item, index);
},
);
if (!scrollSwitchChat) return listView;
return AnimatedSwitcher(
duration: _durationShort,
switchInCurve: Easing.standardDecelerate,
switchOutCurve: Easing.standardDecelerate,
transitionBuilder: (child, animation) => SlideTransitionX(
position: animation,
direction: switchDirection == SwitchDirection.next
? AxisDirection.up
: AxisDirection.down,
child: child,
),
child: listView,
);
},
);

if (!scrollSwitchChat) return child;

return SwitchIndicator(
onSwitchPage: (direction) {
switchDirection = direction;
Expand All @@ -97,37 +135,7 @@ class _ChatPageState extends State<_ChatPage>
}
return Future.value();
},
child: ListenBuilder(
listenable: _chatRN,
builder: () {
final item = _curChat?.items;
if (item == null) return UIs.placeholder;
return AnimatedSwitcher(
duration: _durationShort,
switchInCurve: Easing.standardDecelerate,
switchOutCurve: Easing.standardDecelerate,
transitionBuilder: (child, animation) => SlideTransitionX(
position: animation,
direction: switchDirection == SwitchDirection.next
? AxisDirection.up
: AxisDirection.down,
child: child,
),
child: ListView.builder(
key: Key(_curChatId),
controller: _chatScrollCtrl,
padding: const EdgeInsets.all(7),
physics: const AlwaysScrollableScrollPhysics(
parent: BouncingScrollPhysics(),
),
itemCount: item.length,
itemBuilder: (_, index) {
return _buildChatItem(item, index);
},
),
);
},
),
child: child,
);
}

Expand Down
10 changes: 10 additions & 0 deletions lib/view/page/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class _SettingPageState extends State<SettingPage> {

Widget _buildMore() {
final children = [
_buildScrollSwitchChat(),
_buildSaveErrChat(),
_buildCompressImg(),
_buildFollowChatModel(),
Expand Down Expand Up @@ -767,4 +768,13 @@ class _SettingPageState extends State<SettingPage> {
trailing: StoreSwitch(prop: _store.saveErrChat),
);
}

Widget _buildScrollSwitchChat() {
return ListTile(
leading: const Icon(Icons.swap_vert),
title: Text(l10n.scrollSwitchChat),
subtitle: Text(l10n.needRestart, style: UIs.textGrey),
trailing: StoreSwitch(prop: _store.scrollSwitchChat),
);
}
}
2 changes: 1 addition & 1 deletion lib/view/widget/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CodeElementBuilder extends MarkdownElementBuilder {
CodeElementBuilder({this.onCopy, this.isForCapture = false});

static final _bgPaint = Paint()
..color = const Color.fromARGB(28, 159, 159, 159);
..color = const Color.fromARGB(23, 159, 159, 159);

@override
Widget? visitElementAfter(md.Element element, TextStyle? preferredStyle) {
Expand Down

0 comments on commit 9be4b06

Please sign in to comment.