更新测试案例
This commit is contained in:
@@ -3,69 +3,111 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:im_app/features/chat/presentation/chat_db_test_view_model.dart';
|
||||
|
||||
import '../../../core/ui/components/app_button.dart';
|
||||
import '../presentation/chat_view_model.dart';
|
||||
|
||||
/// 聊天页(Demo 按钮)
|
||||
///
|
||||
/// 包含五个演示按钮,覆盖 go_router 的常见导航场景:
|
||||
/// - 「切换 Tab」 — go,替换历史,不可返回
|
||||
/// - 「有参 push(extra)」 — push + extra(Dart Record),可返回
|
||||
/// - 「有参 push(路径参数)」— push + URL 内嵌 id,可返回
|
||||
/// - 「无参 push」 — push,可返回
|
||||
/// - 「退出登录」 — 守卫自动重定向到 /login
|
||||
///
|
||||
/// 所有操作通过 [ChatViewModel] 处理,View 不直接调用路由。
|
||||
/// 正式开发后替换为会话列表,按钮相关代码一并清除。
|
||||
class ChatDbTestPage extends ConsumerWidget {
|
||||
class ChatDbTestPage extends ConsumerStatefulWidget {
|
||||
const ChatDbTestPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
ConsumerState<ChatDbTestPage> createState() => _ChatDbTestPageState();
|
||||
}
|
||||
|
||||
class _ChatDbTestPageState extends ConsumerState<ChatDbTestPage> {
|
||||
final _scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_scrollController.addListener(_onScroll);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
if (_scrollController.position.pixels >=
|
||||
_scrollController.position.maxScrollExtent - 300) {
|
||||
ref.read(chatDbTestViewModelProvider.notifier).loadMore();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final vm = ref.read(chatDbTestViewModelProvider.notifier);
|
||||
final state = ref.watch(chatDbTestViewModelProvider);
|
||||
final testStarted = ref.watch(
|
||||
chatDbTestViewModelProvider.select((s) => s.testStarted),
|
||||
);
|
||||
final currentState = ref.watch(
|
||||
chatDbTestViewModelProvider.select((s) => s.currentState),
|
||||
);
|
||||
final users = ref.watch(chatDbTestViewModelProvider.select((s) => s.users));
|
||||
final hasMore = ref.watch(
|
||||
chatDbTestViewModelProvider.select((s) => s.hasMore),
|
||||
);
|
||||
final totalCount = ref.watch(
|
||||
chatDbTestViewModelProvider.select((s) => s.totalCount),
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('测试数据库'), ),
|
||||
appBar: AppBar(title: Text('测试数据库 ($totalCount)')),
|
||||
body: Column(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
spacing: 16,
|
||||
children: [
|
||||
SizedBox(height: 4),
|
||||
const SizedBox(height: 4),
|
||||
Padding(
|
||||
padding: EdgeInsetsGeometry.symmetric(horizontal: 8),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
AppButton.inverse(
|
||||
label: state.testStarted ? '结束' : '开始',
|
||||
onPressed: () => state.testStarted ? vm.stopDBTest(context) : vm.startDBTest(context),
|
||||
label: testStarted ? '结束' : '开始',
|
||||
onPressed: () => testStarted
|
||||
? vm.stopDBTest(context)
|
||||
: vm.startDBTest(context),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
state.currentState,
|
||||
textAlign: TextAlign.end,
|
||||
),
|
||||
)
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: Text(currentState, textAlign: TextAlign.end)),
|
||||
],
|
||||
)
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: state.testResults.length,
|
||||
controller: _scrollController,
|
||||
itemCount: users.length + (hasMore ? 1 : 0),
|
||||
cacheExtent: 500,
|
||||
itemBuilder: (context, index) {
|
||||
final result = state.testResults[index];
|
||||
return ListTile(
|
||||
titleAlignment: ListTileTitleAlignment.center,
|
||||
title: Text(result.title),
|
||||
subtitle: Text(result.subtitle),
|
||||
// trailing: Text(result.duration),
|
||||
);
|
||||
if (index == users.length) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
final user = users[index];
|
||||
return _UserTile(uid: user.uid, nickname: user.nickname);
|
||||
},
|
||||
),
|
||||
)
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _UserTile extends StatelessWidget {
|
||||
final int uid;
|
||||
final String? nickname;
|
||||
|
||||
const _UserTile({required this.uid, required this.nickname});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
titleAlignment: ListTileTitleAlignment.center,
|
||||
title: Text(nickname ?? '-'),
|
||||
subtitle: Text('uid: $uid'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user