import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:news_getx/modules/category/category_controller.dart';
import 'package:news_getx/modules/category/widgets/news_item.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class NewsPageList extends StatefulWidget {
  const NewsPageList({Key? key}) : super(key: key);

  @override
  State<NewsPageList> createState() => _NewsPageListState();
}

class _NewsPageListState extends State<NewsPageList>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  final controller = Get.find<CategoryController>();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return GetX<CategoryController>(
        init: controller,
        builder: (controller) {
          return SmartRefresher(
            controller: controller.refreshController,
            enablePullUp: true,
            onRefresh: controller.onRefresh,
            onLoading: controller.onLoading,
            child: CustomScrollView(
              slivers: [
                SliverPadding(
                  padding: EdgeInsets.symmetric(vertical: 0.w, horizontal: 0.h),
                  sliver: SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (BuildContext context, int index) {
                        var item = controller.state.newsList[index];
                        return NewsListItem(newsItem: item);
                      },
                      childCount: controller.state.newsList.length,
                    ),
                  ),
                ),
              ],
            ),
          );
        });
  }
}
