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.

178 lines
5.2KB

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