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.

166 lines
4.8KB

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