refactor: member dynamic page

fix: DynamicsDataModel parsing error

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2024-11-21 12:21:35 +08:00
parent 76d1219978
commit f54859098c
14 changed files with 155 additions and 176 deletions

View File

@@ -1,3 +1,5 @@
import 'package:PiliPalaX/common/widgets/refresh_indicator.dart';
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
@@ -36,29 +38,10 @@ class _MemberDynamicsPageState extends State<MemberDynamicsPage>
final String heroTag = Utils.makeHeroTag(mid);
_memberDynamicController =
Get.put(MemberDynamicsController(mid), tag: heroTag);
// _memberDynamicController.scrollController.addListener(
// () {
// if (_memberDynamicController.scrollController.position.pixels >=
// _memberDynamicController.scrollController.position.maxScrollExtent -
// 200) {
// EasyThrottle.throttle(
// 'member_dynamics', const Duration(milliseconds: 1000), () {
// _memberDynamicController.onLoad();
// });
// }
// },
// );
dynamicsWaterfallFlow = GStorage.setting
.get(SettingBoxKey.dynamicsWaterfallFlow, defaultValue: true);
}
@override
void dispose() {
// _memberDynamicController.scrollController.removeListener(() {});
_memberDynamicController.scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
@@ -75,106 +58,91 @@ class _MemberDynamicsPageState extends State<MemberDynamicsPage>
: _buildBody;
}
Widget get _buildBody => CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
// controller: _memberDynamicController.scrollController,
slivers: [
FutureBuilder(
future: _memberDynamicController.futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data != null) {
Map data = snapshot.data as Map;
List list = _memberDynamicController.dynamicsList;
if (data['status']) {
return Obx(() {
if (list.isEmpty) {
return const SliverToBoxAdapter();
}
if (!dynamicsWaterfallFlow) {
return SliverCrossAxisGroup(
slivers: [
const SliverFillRemaining(),
SliverConstrainedCrossAxis(
maxExtent: Grid.maxRowWidth * 2,
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
if (index == list.length - 1) {
EasyThrottle.throttle('member_dynamics',
const Duration(milliseconds: 1000),
() {
_memberDynamicController.onLoad();
});
}
return DynamicPanel(item: list[index]);
},
childCount: list.length,
),
)),
const SliverFillRemaining(),
],
);
}
return SliverWaterfallFlow.extent(
maxCrossAxisExtent: Grid.maxRowWidth * 2,
//cacheExtent: 0.0,
crossAxisSpacing: StyleString.safeSpace,
mainAxisSpacing: StyleString.safeSpace,
/// follow max child trailing layout offset and layout with full cross axis extend
/// last child as loadmore item/no more item in [GridView] and [WaterfallFlow]
/// with full cross axis extend
// LastChildLayoutType.fullCrossAxisExtend,
/// as foot at trailing and layout with full cross axis extend
/// show no more item at trailing when children are not full of viewport
/// if children is full of viewport, it's the same as fullCrossAxisExtend
// LastChildLayoutType.foot,
lastChildLayoutTypeBuilder: (index) {
if (index == list.length - 1) {
EasyThrottle.throttle('member_dynamics',
const Duration(milliseconds: 1000), () {
_memberDynamicController.onLoad();
});
}
return index == list.length
? LastChildLayoutType.foot
: LastChildLayoutType.none;
},
children: [
for (var i in list) DynamicPanel(item: i),
]);
});
} else {
return HttpError(
errMsg: snapshot.data['msg'],
fn: () {
setState(() {
_memberDynamicController.futureBuilderFuture =
_memberDynamicController
.getMemberDynamic('onRefresh');
});
},
);
}
} else {
return HttpError(
errMsg: 'NULL',
fn: () {
setState(() {
_memberDynamicController.futureBuilderFuture =
_memberDynamicController
.getMemberDynamic('onRefresh');
});
},
);
}
} else {
return const SliverToBoxAdapter();
}
},
),
],
Widget get _buildBody => refreshIndicator(
onRefresh: () async {
await _memberDynamicController.onRefresh();
},
child: Obx(
() => _memberDynamicController.loadingState.value is Loading
? Center(
child: CircularProgressIndicator(),
)
: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
_buildContent(_memberDynamicController.loadingState.value),
],
),
),
);
_buildContent(LoadingState loadingState) {
return loadingState is Success
? SliverPadding(
padding: EdgeInsets.only(
bottom: MediaQuery.paddingOf(context).bottom,
),
sliver: dynamicsWaterfallFlow
? SliverWaterfallFlow.extent(
maxCrossAxisExtent: Grid.maxRowWidth * 2,
//cacheExtent: 0.0,
crossAxisSpacing: StyleString.safeSpace,
mainAxisSpacing: StyleString.safeSpace,
/// follow max child trailing layout offset and layout with full cross axis extend
/// last child as loadmore item/no more item in [GridView] and [WaterfallFlow]
/// with full cross axis extend
// LastChildLayoutType.fullCrossAxisExtend,
/// as foot at trailing and layout with full cross axis extend
/// show no more item at trailing when children are not full of viewport
/// if children is full of viewport, it's the same as fullCrossAxisExtend
// LastChildLayoutType.foot,
lastChildLayoutTypeBuilder: (index) {
if (index == loadingState.response.length - 1) {
EasyThrottle.throttle('member_dynamics',
const Duration(milliseconds: 1000), () {
_memberDynamicController.onLoadMore();
});
}
return index == loadingState.response.length
? LastChildLayoutType.foot
: LastChildLayoutType.none;
},
children: (loadingState.response as List)
.map((item) => DynamicPanel(item: item))
.toList(),
)
: SliverCrossAxisGroup(
slivers: [
const SliverFillRemaining(),
SliverConstrainedCrossAxis(
maxExtent: Grid.maxRowWidth * 2,
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
if (index == loadingState.response.length - 1) {
EasyThrottle.throttle('member_dynamics',
const Duration(milliseconds: 1000), () {
_memberDynamicController.onLoadMore();
});
}
return DynamicPanel(
item: loadingState.response[index]);
},
childCount: loadingState.response.length,
),
),
),
const SliverFillRemaining(),
],
),
)
: HttpError(
errMsg: loadingState is Error ? loadingState.errMsg : null,
fn: () {
_memberDynamicController.onReload();
},
);
}
}