widgetSocialPosts function

PostsWidgetBundle widgetSocialPosts(
  1. dynamic service, {
  2. Key? key,
  3. required BuildContext context,
  4. required dynamic profileFunction({
    1. required Media? avatar,
    2. required Media? banner,
    3. required String? displayname,
    4. required int userID,
    5. required String username,
    }),
  5. ScrollController? scrollController,
  6. List<Post>? cachedpostsList,
  7. dynamic onPostsUpdated(
    1. List<Post> updatedPosts
    )?,
  8. Function? refreshPosts,
  9. bool isPostdetail = false,
  10. String? category,
  11. int? userID,
  12. String? username,
  13. bool shrinkWrap = false,
  14. EdgeInsetsGeometry padding = EdgeInsets.zero,
  15. ScrollPhysics physics = const ClampingScrollPhysics(),
  16. bool sliverWidget = false,
  17. bool autofetchposts = true,
})

Implementation

PostsWidgetBundle widgetSocialPosts(
  service, {
  Key? key,
  required BuildContext context,
  required Function({
    required int userID,
    required String username,
    required String? displayname,
    required Media? avatar,
    required Media? banner,
  }) profileFunction,
  ScrollController? scrollController,
  List<Post>? cachedpostsList,
  Function(List<Post> updatedPosts)? onPostsUpdated,
  Function? refreshPosts,
  bool isPostdetail = false,
  String? category,
  int? userID,
  String? username,
  bool shrinkWrap = false,
  EdgeInsetsGeometry padding = EdgeInsets.zero,
  ScrollPhysics physics = const ClampingScrollPhysics(),
  bool sliverWidget = false,
  bool autofetchposts = true,
}) {
  final controller = Get.put(
    PostController(
      service: service,
      scrollController: scrollController,
      category: category,
      userID: userID,
      username: username,
      cachedpostsList: cachedpostsList,
      onPostsUpdated: onPostsUpdated,
      autofetchposts: autofetchposts,
    ),
    tag:
        "postWidget-$category$userID-Uniq-${DateTime.now().millisecondsSinceEpoch}",
  );

  Widget widget = Obx(
    () => controller.postsList.value == null
        ? const Center(
            child: CupertinoActivityIndicator(),
          )
        : !sliverWidget
            ? ListView.builder(
                controller:
                    shrinkWrap ? null : controller.xscrollController.value,
                key: key,
                shrinkWrap: shrinkWrap,
                padding: padding,
                physics:
                    shrinkWrap ? const NeverScrollableScrollPhysics() : physics,
                itemCount: controller.postsList.value!.length,
                itemBuilder: (context, postIndex) {
                  var postdetail =
                      Rx<Post>(controller.postsList.value![postIndex]);

                  return PostWidget.postWidget(
                    service: service,
                    postdetail: postdetail,
                    controller: controller,
                    profileFunction: profileFunction,
                    showPOPcard: postIndex % 5 == 0 && postIndex != 0,
                    showTPcard: postIndex % 5 == 3 && postIndex != 0,
                  );
                },
              )
            : CustomScrollView(
                physics: refreshPosts != null
                    ? const BouncingScrollPhysics()
                    : const ClampingScrollPhysics(),
                slivers: [
                  refreshPosts != null
                      ? CupertinoSliverRefreshControl(
                          onRefresh: () async {
                            await refreshPosts();
                          },
                        )
                      : const SliverToBoxAdapter(child: SizedBox.shrink()),
                  SliverToBoxAdapter(
                    child: ListView.builder(
                      controller: shrinkWrap
                          ? null
                          : controller.xscrollController.value,
                      key: key,
                      shrinkWrap: shrinkWrap,
                      padding: padding,
                      physics: shrinkWrap
                          ? const NeverScrollableScrollPhysics()
                          : physics,
                      itemCount: controller.postsList.value!.length,
                      itemBuilder: (context, postIndex) {
                        var postdetail =
                            Rx<Post>(controller.postsList.value![postIndex]);
                        return PostWidget.postWidget(
                          service: service,
                          postdetail: postdetail,
                          controller: controller,
                          profileFunction: profileFunction,
                          showPOPcard: postIndex % 5 == 0 && postIndex != 0,
                          showTPcard: postIndex % 5 == 3 && postIndex != 0,
                        );
                      },
                    ),
                  ),
                ],
              ),
  );

  return PostsWidgetBundle(
    widget: Rxn(widget),
    refresh: () async => await controller.refreshAllPosts(),
    loadMore: () async => await controller.loadMorePosts(),
  );
}