import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:farmer_client/pages/infomation/widgets/Information/index.dart';
import 'package:farmer_client/widgets/RefreshMoreList.dart';
import 'package:farmer_client/widgets/layout/BasicPage.dart';
import 'package:farmer_client/models/entities/NewsInformationModel.dart';
import 'package:farmer_client/services/news.dart';

class Infomation extends BasicPage {
  Infomation({Key? key}) : super(key: key) {
    tabIndex = 2;
    naviTitle = '资讯';
  }

  final newsList = Rx<List<NewsInformationModel>>([]);
  final params = Rx<Map<String, dynamic>>({'pageSize': 10});
  Function? Refresh;
  List<NewsInformationModel> GetList(value) {
    List<NewsInformationModel> list = [];
    if (value != null)
      value.forEach((item) => {list.add(NewsInformationModel.fromJson(item))});
    return list;
  }

  @override
  Widget builder(BuildContext context) {
    return RefreshMoreList(
      pPrams: params,
      cardText: '暂无资讯',
      fetch: getNewsList,
      useState: (state) {
        newsList(GetList(state));
      },
      builder: ({required BuildContext context, required Function toggle}) {
        Refresh = toggle;
        return Obx(
          () => Column(
            children: newsList.value
                .map(
                  (item) => Information(item: item),
                )
                .toList(),
          ),
        );
      },
    );
  }
}
