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.

84 lines
2.9KB

  1. import 'package:farm_tpf/custom_model/action_form/CommonData.dart';
  2. import 'package:farm_tpf/data/repository/repository.dart';
  3. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_dropdown_controller.dart';
  4. import 'package:farm_tpf/presentation/screens/resources/bloc/bloc/common_data_bloc.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. import 'package:get/get.dart';
  8. import 'package:group_radio_button/group_radio_button.dart';
  9. class RadioButtonWidget extends StatefulWidget {
  10. final String condition;
  11. final String supply;
  12. final String tag;
  13. final Function(CommonData) onPressed;
  14. RadioButtonWidget(
  15. {@required this.condition,
  16. @required this.supply,
  17. @required this.onPressed,
  18. @required this.tag});
  19. @override
  20. _RadioButtonWidgetState createState() => _RadioButtonWidgetState();
  21. }
  22. class _RadioButtonWidgetState extends State<RadioButtonWidget> {
  23. var selectedItem = CommonData();
  24. CommonDataBloc _commonDataBloc;
  25. ChangeDropdownController controller;
  26. @override
  27. void initState() {
  28. super.initState();
  29. controller = Get.put(ChangeDropdownController(), tag: widget.tag);
  30. }
  31. @override
  32. Widget build(BuildContext context) {
  33. return GetBuilder<ChangeDropdownController>(
  34. tag: widget.tag,
  35. builder: (data) {
  36. return BlocProvider(
  37. create: (context) => CommonDataBloc(repository: Repository())
  38. ..add(DataFetched(
  39. tbSupply: widget.supply, condition: widget.condition)),
  40. child: BlocBuilder<CommonDataBloc, CommonDataState>(
  41. cubit: _commonDataBloc,
  42. builder: (contextb, state) {
  43. if (state is CommonDataFailure) {
  44. return Center(child: Text(''));
  45. }
  46. if (state is CommonDataSuccess) {
  47. if (state.items.isEmpty) {
  48. return Center(child: Text(''));
  49. }
  50. List<CommonData> listItem = state.items;
  51. if (data?.currentData?.id != null) {
  52. listItem.forEach((element) {
  53. if (element.id == data.currentData.id) {
  54. selectedItem = element;
  55. }
  56. });
  57. }
  58. return RadioGroup<CommonData>.builder(
  59. groupValue: selectedItem,
  60. onChanged: (value) {
  61. setState(() {
  62. selectedItem = value;
  63. widget.onPressed(value);
  64. controller?.change(value);
  65. });
  66. },
  67. items: state.items,
  68. itemBuilder: (item) => RadioButtonBuilder(item.name),
  69. );
  70. }
  71. return Center(child: CircularProgressIndicator());
  72. },
  73. ),
  74. );
  75. });
  76. }
  77. }