|
- import 'package:farm_tpf/data/repository/repository.dart';
- import 'package:farm_tpf/models/index.dart';
- import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
- import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
- import 'package:farm_tpf/utils/bloc/infinity_scroll_bloc.dart';
- import 'package:farm_tpf/utils/const_string.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
-
- class ResourceHelperScreen extends StatefulWidget {
- final String resourceName;
- final int selectedPlotId;
- ResourceHelperScreen(
- {@required this.resourceName, @required this.selectedPlotId});
- @override
- _ResourceHelperScreenState createState() => _ResourceHelperScreenState();
- }
-
- class _ResourceHelperScreenState extends State<ResourceHelperScreen> {
- @override
- Widget build(BuildContext context) {
- return BlocProvider(
- create: (context) =>
- InfinityScrollBloc(repository: Repository())..add(DataFetched()),
- child: HoldInfinityWidget(
- selectedPlotId: widget.selectedPlotId,
- ),
- );
- }
- }
-
- class HoldInfinityWidget extends StatelessWidget {
- final int selectedPlotId;
- HoldInfinityWidget({@required this.selectedPlotId});
- final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(title: Text("Chọn danh sách")),
- body: InfinityView(
- selectedPlotId: selectedPlotId,
- ));
- }
- }
-
- class InfinityView extends StatefulWidget {
- final int selectedPlotId;
- InfinityView({@required this.selectedPlotId});
- @override
- _InfinityViewState createState() => _InfinityViewState();
- }
-
- class _InfinityViewState extends State<InfinityView> {
- final _scrollController = ScrollController();
- final _scrollThreshold = 250.0;
- InfinityScrollBloc _infinityScrollBloc;
-
- @override
- void initState() {
- _scrollController.addListener(() {
- final maxScroll = _scrollController.position.maxScrollExtent;
- final currentScroll = _scrollController.position.pixels;
- if (maxScroll - currentScroll < _scrollThreshold) {
- _infinityScrollBloc.add(DataFetched());
- }
- });
- _infinityScrollBloc = BlocProvider.of<InfinityScrollBloc>(context);
- super.initState();
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocBuilder<InfinityScrollBloc, InfinityScrollState>(
- builder: (context, state) {
- if (state is InfinityScrollFailure) {
- return Center(child: Text(label_error_get_data));
- }
- if (state is InfinityScrollSuccess) {
- if (state.items.isEmpty) {
- return Center(child: Text(label_list_empty));
- }
-
- Plot selectedPlot;
- List<Plot> plots = state.items.map((e) => e as Plot).toList();
- plots.forEach((plot) {
- plot.id == widget.selectedPlotId ? selectedPlot = plot : Plot();
- });
- return RefreshIndicator(
- child: ListView.builder(
- itemBuilder: (BuildContext context, int index) {
- return index >= state.items.length
- ? BottomLoader()
- : ItemInfinityWidget(
- item: state.items[index],
- selectedPlot: selectedPlot,
- );
- },
- itemCount: state.hasReachedMax
- ? state.items.length
- : state.items.length + 1,
- controller: _scrollController,
- ),
- onRefresh: () async {
- _infinityScrollBloc.add(OnRefresh());
- });
- }
- return Center(
- child: LoadingListPage(),
- );
- },
- );
- }
-
- @override
- void dispose() {
- _scrollController.dispose();
- super.dispose();
- }
- }
-
- class ItemInfinityWidget extends StatelessWidget {
- final Plot item;
- final Plot selectedPlot;
-
- const ItemInfinityWidget({Key key, @required this.item, this.selectedPlot})
- : super(key: key);
-
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- child: Card(
- color: item.id % 3 == 0 ? Colors.white : Colors.greenAccent[100],
- child: Material(
- child: RadioListTile(
- title: Text(item.activityExecuteDate.toString()),
- subtitle: Text(item.id.toString()),
- value: item,
- groupValue: selectedPlot,
- onChanged: (Plot value) {
- print("selected value: ${value.id}");
- Navigator.of(context).pop(value.id);
- }),
- )),
- onTap: () {});
- }
- }
|