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.

173 lines
5.1KB

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