| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import 'package:easy_refresh/easy_refresh.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:flutter_screenutil/flutter_screenutil.dart';
- import 'package:go_router/go_router.dart';
- import 'package:news_app/ui/topic/topic_item_widget.dart';
- import '../../constant/color_res.dart';
- import '../../constant/config.dart';
- import '../../constant/size_res.dart';
- import '../../model/topic_item_model.dart';
- import '../../provider/topic_list_provider.dart';
- import '../../widget/empty_1_widget.dart';
- import '../../widget/my_txt.dart';
- class TopicListPage extends ConsumerStatefulWidget {
- final String contentId;
- const TopicListPage({super.key, required this.contentId});
- @override
- ConsumerState<TopicListPage> createState() => _TopicListPageState();
- }
- final topicListProvider = NotifierProvider<TopicListProvider, TopicItemModel>(
- () => TopicListProvider(),
- );
- class _TopicListPageState extends ConsumerState<TopicListPage> {
- int _page = 1;
- int _currentTotal = 0;
- @override
- void initState() {
- // TODO: implement initState
- ref
- .read(topicListProvider.notifier)
- .fetchList(page: _page, tid: widget.contentId);
- eventBus.on<String>().listen((event) {
- if (event == "topicCall") {
- _page = 1;
- ref
- .read(topicListProvider.notifier)
- .fetchList(page: _page, tid: widget.contentId);
- }
- });
- super.initState();
- }
- void longTapAction(String contentId) {}
- @override
- Widget build(BuildContext context) {
- final itemData = ref.watch(topicListProvider);
- ref.listen(topicListProvider.select((p) => p.records), (previous, next) {
- _currentTotal = next?.length ?? 0;
- });
- return Scaffold(
- appBar: AppBar(
- scrolledUnderElevation: 0,
- title: myTxt(
- text: '话题列表',
- fontSize: 18.sp,
- fontWeight: FontWeight.bold,
- ),
- centerTitle: true,
- ),
- body: Container(
- // padding: EdgeInsets.symmetric(horizontal: 10.w),
- alignment: Alignment.center,
- color: colorF5F7FD,
- child: EasyRefresh.builder(
- onRefresh: () async {
- _page = 1;
- ref
- .read(topicListProvider.notifier)
- .fetchList(page: _page, tid: widget.contentId);
- return IndicatorResult.success;
- },
- onLoad: () async {
- final newItemData = ref.read(topicListProvider);
- int total = newItemData.total ?? 0;
- if (_currentTotal >= total) {
- return IndicatorResult.noMore;
- } else {
- _page += 1;
- await ref
- .read(topicListProvider.notifier)
- .fetchList(page: _page, tid: widget.contentId);
- return IndicatorResult.success;
- }
- },
- childBuilder: (context, py) {
- return (itemData.records ?? []).isEmpty
- ? Empty1widget()
- : ListView.separated(
- physics: py,
- padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
- itemBuilder: (BuildContext context, int index) {
- return GestureDetector(
- onTap: () {
- context.push(
- "/topic/detail",
- extra: itemData.records?[index].contentId,
- );
- },
- child: Padding(
- padding: EdgeInsets.only(
- // left: horizontalPadding,
- // right: horizontalPadding,
- top: horizontalPadding,
- ),
- child: TopicItemWidget(
- longTapAction,
- item: itemData.records?[index],
- ),
- ),
- );
- },
- separatorBuilder: (BuildContext context, int index) {
- return SizedBox(height: 5.h);
- },
- itemCount: itemData.records?.length ?? 0,
- );
- },
- ),
- ),
- );
- }
- }
|