Skip to content

Commit

Permalink
优化桌面版推荐标签滚动
Browse files Browse the repository at this point in the history
原先电脑上只能使用横向滚轮滚动,
修改后支持纵向滚轮和左键拖动,
  • Loading branch information
AoEiuV020 committed Dec 21, 2024
1 parent 17b56c2 commit 8795765
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 65 deletions.
46 changes: 46 additions & 0 deletions lib/bean/widget/scrollable_wrapper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

/// 滚动容器
/// 支持鼠标滚轮滚动和拖动滚动
/// 传入ListView的scrollController
class ScrollableWrapper extends StatelessWidget {
final Widget child;
final ScrollController scrollController;

const ScrollableWrapper({
super.key,
required this.child,
required this.scrollController,
});

@override
Widget build(BuildContext context) {
return MouseRegion(
child: Listener(
onPointerSignal: (pointerSignal) {
// 鼠标滚轮滚动
if (pointerSignal is PointerScrollEvent &&
scrollController.hasClients) {
scrollController.position.moveTo(
scrollController.offset + pointerSignal.scrollDelta.dy,
curve: Curves.linear,
);
}
},
child: GestureDetector(
onPanUpdate: (details) {
// 拖动滚动
if (scrollController.hasClients) {
scrollController.position.moveTo(
scrollController.offset - details.delta.dx,
curve: Curves.linear,
);
}
},
child: child,
),
),
);
}
}
138 changes: 73 additions & 65 deletions lib/pages/popular/popular_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:window_manager/window_manager.dart';
import 'package:kazumi/bean/appbar/sys_app_bar.dart';
import 'package:logger/logger.dart';
import 'package:kazumi/utils/logger.dart';
import 'package:kazumi/bean/widget/scrollable_wrapper.dart';

class PopularPage extends StatefulWidget {
const PopularPage({super.key});
Expand Down Expand Up @@ -376,74 +377,81 @@ class _PopularPageState extends State<PopularPage>
'偶像',
'治愈',
];

final ScrollController tagScrollController = ScrollController();

return Row(
children: <Widget>[
Expanded(
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: tags.length,
itemBuilder: (context, index) {
final filter = tags[index];
return Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8, left: 8),
child: filter == popularController.currentTag
? FilledButton(
child: Text(filter),
onPressed: () async {
scrollController.jumpTo(0.0);
setState(() {
timeout = false;
popularController.currentTag = '';
searchLoading = true;
});
await popularController
.queryBangumiListFeed(
tag: popularController.currentTag,
)
.then((_) {
if (popularController.bangumiList.isEmpty &&
mounted) {
setState(() {
timeout = true;
});
}
});
setState(() {
searchLoading = false;
});
},
)
: FilledButton.tonal(
child: Text(filter),
onPressed: () async {
_focusNode.unfocus();
scrollController.jumpTo(0.0);
setState(() {
timeout = false;
popularController.currentTag = filter;
keywordController.text = '';
showSearchBar = false;
searchLoading = true;
});
await popularController
.queryBangumiListFeed(
tag: popularController.currentTag,
)
.then((_) {
if (popularController.bangumiList.isEmpty &&
mounted) {
setState(() {
timeout = true;
});
}
});
setState(() {
searchLoading = false;
});
},
),
);
},
child: ScrollableWrapper(
scrollController: tagScrollController,
child: ListView.builder(
controller: tagScrollController,
scrollDirection: Axis.horizontal,
itemCount: tags.length,
itemBuilder: (context, index) {
final filter = tags[index];
return Padding(
padding: const EdgeInsets.only(top: 8, bottom: 8, left: 8),
child: filter == popularController.currentTag
? FilledButton(
child: Text(filter),
onPressed: () async {
scrollController.jumpTo(0.0);
setState(() {
timeout = false;
popularController.currentTag = '';
searchLoading = true;
});
await popularController
.queryBangumiListFeed(
tag: popularController.currentTag,
)
.then((_) {
if (popularController.bangumiList.isEmpty &&
mounted) {
setState(() {
timeout = true;
});
}
});
setState(() {
searchLoading = false;
});
},
)
: FilledButton.tonal(
child: Text(filter),
onPressed: () async {
_focusNode.unfocus();
scrollController.jumpTo(0.0);
setState(() {
timeout = false;
popularController.currentTag = filter;
keywordController.text = '';
showSearchBar = false;
searchLoading = true;
});
await popularController
.queryBangumiListFeed(
tag: popularController.currentTag,
)
.then((_) {
if (popularController.bangumiList.isEmpty &&
mounted) {
setState(() {
timeout = true;
});
}
});
setState(() {
searchLoading = false;
});
},
),
);
},
),
),
),
// Tooltip(
Expand Down

0 comments on commit 8795765

Please sign in to comment.