You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.0KB

  1. import 'package:farm_tpf/custom_model/LocationUnit.dart';
  2. import 'package:farm_tpf/data/repository/repository.dart';
  3. import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/bottom_loader.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  6. import 'package:farm_tpf/presentation/screens/location_unit/widget_search.dart';
  7. import 'package:farm_tpf/utils/const_common.dart';
  8. import 'package:farm_tpf/utils/const_string.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_bloc/flutter_bloc.dart';
  11. import 'bloc/location_bloc.dart';
  12. class LocationScreen extends StatefulWidget {
  13. final LocationType type;
  14. final int selectedId;
  15. final int filterId;
  16. final String titleName;
  17. LocationScreen({required this.titleName, required this.type, required this.selectedId, required this.filterId});
  18. @override
  19. _LocationScreenState createState() => _LocationScreenState();
  20. }
  21. class _LocationScreenState extends State<LocationScreen> {
  22. @override
  23. Widget build(BuildContext context) {
  24. return BlocProvider(
  25. create: (context) => LocationBloc(repository: Repository()),
  26. child: HoldInfinityWidget(
  27. type: widget.type,
  28. selectedId: widget.selectedId,
  29. filterId: widget.filterId,
  30. titleName: widget.titleName,
  31. ),
  32. );
  33. }
  34. }
  35. class HoldInfinityWidget extends StatelessWidget {
  36. final int selectedId;
  37. final LocationType type;
  38. final int filterId;
  39. final String titleName;
  40. HoldInfinityWidget({required this.selectedId, required this.type, required this.filterId, required this.titleName});
  41. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  42. @override
  43. Widget build(BuildContext context) {
  44. return Scaffold(
  45. backgroundColor: Colors.white,
  46. key: _scaffoldKey,
  47. appBar: AppBarWidget(),
  48. body: Column(
  49. crossAxisAlignment: CrossAxisAlignment.start,
  50. children: <Widget>[
  51. Padding(
  52. padding: const EdgeInsets.all(8.0),
  53. child: Text(
  54. '$titleName',
  55. style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),
  56. ),
  57. ),
  58. WidgetSearchLocation(
  59. filterId: filterId,
  60. type: type,
  61. selectedId: selectedId,
  62. ),
  63. Expanded(
  64. child: InfinityView(
  65. selectedId: selectedId,
  66. type: type,
  67. filterId: filterId,
  68. ))
  69. ],
  70. ));
  71. }
  72. }
  73. class InfinityView extends StatefulWidget {
  74. final int filterId;
  75. final int selectedId;
  76. final LocationType type;
  77. InfinityView({required this.selectedId, required this.type, required this.filterId});
  78. @override
  79. _InfinityViewState createState() => _InfinityViewState();
  80. }
  81. class _InfinityViewState extends State<InfinityView> {
  82. LocationBloc? _locationBloc;
  83. @override
  84. void initState() {
  85. _locationBloc = BlocProvider.of<LocationBloc>(context);
  86. _locationBloc?.add(DataFetched(locationType: widget.type, selectedId: widget.selectedId, filterId: widget.filterId));
  87. super.initState();
  88. }
  89. @override
  90. Widget build(BuildContext context) {
  91. return BlocBuilder<LocationBloc, LocationState>(
  92. builder: (context, state) {
  93. if (state is LocationFailure) {
  94. return Center(child: Text(label_error_get_data));
  95. }
  96. if (state is LocationSuccess) {
  97. if (state.items!.isEmpty) {
  98. return Center(child: Text(label_list_empty));
  99. }
  100. return RefreshIndicator(
  101. child: ListView.builder(
  102. itemBuilder: (BuildContext context, int index) {
  103. return index >= state.items!.length ? BottomLoader() : ItemInfinityWidget(item: state.items![index]);
  104. },
  105. itemCount: state.items!.length),
  106. onRefresh: () async {
  107. _locationBloc?.add(
  108. OnRefresh(
  109. locationType: widget.type,
  110. selectedId: widget.selectedId,
  111. filterId: widget.filterId,
  112. ),
  113. );
  114. });
  115. }
  116. return Center(
  117. child: LoadingListPage(),
  118. );
  119. },
  120. );
  121. }
  122. @override
  123. void dispose() {
  124. super.dispose();
  125. }
  126. }
  127. class ItemInfinityWidget extends StatelessWidget {
  128. final LocationUnit item;
  129. const ItemInfinityWidget({Key? key, required this.item}) : super(key: key);
  130. @override
  131. Widget build(BuildContext context) {
  132. return GestureDetector(
  133. child: Container(
  134. decoration: BoxDecoration(border: Border(bottom: BorderSide(color: Colors.grey, width: 0.35))),
  135. child: RadioListTile(
  136. title: Text("${item.name}"),
  137. value: item,
  138. groupValue: item.isSelected == false ? null : item,
  139. onChanged: (LocationUnit? value) {
  140. Navigator.of(context).pop(value);
  141. }),
  142. ),
  143. onTap: () {});
  144. }
  145. }