Group button's on selected method doesn't working?

Viewed 22

Group button's on selected method doesn't working the function is'print('object');' It should be because of group button and radioTile aren't connected, and I do not know how to do that. Any suggestion to fix this problem. I would be very delightful. On the other hand. List tile's onTap method is perfectly working I just wanted to state that.

import 'package:group_button/group_button.dart';

// ignore: must_be_immutable
class FirstQuestion extends StatefulWidget {
  List<String> radioButtons;
  FocusNode focus;
  FirstQuestion({required this.focus, required this.radioButtons, Key? key})
      : super(key: key);

  @override
  State<FirstQuestion> createState() => _FirstQuestionState();
}

class _FirstQuestionState extends State<FirstQuestion> {
  late GroupButtonController _radioController;

  // ignore: prefer_typing_uninitialized_variables
  late final _radioButtons;

  @override
  void initState() {
    _radioButtons = widget.radioButtons;
    _radioController = GroupButtonController(
      selectedIndexes: [0, 1, 2, 3],
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GroupButton(
        controller: _radioController,
        isRadio: true,
        options: const GroupButtonOptions(groupingType: GroupingType.column),
        buttons: _radioButtons,
        buttonIndexedBuilder: (selected, index, context) {
          return RadioTile(
            title: _radioButtons[index],
            selected: _radioController.selectedIndex,
            index: index,
            onTap: () {
              _radioController.selectIndex(index);
            },
          );
        },
        onSelected: (val, i, selected) {
          print('object');
        });
  }
}

class RadioTile extends StatelessWidget {
  const RadioTile({
    Key? key,
    required this.selected,
    required this.onTap,
    required this.index,
    required this.title,
  }) : super(key: key);

  final String title;
  final int index;
  final int? selected;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(title),
      onTap: onTap,
      leading: Radio<int>(
        groupValue: selected,
        value: index,
        onChanged: (val) {
          print(val);
          onTap();
        },
      ),
    );
  }
}```
1 Answers

You can create a standard method and then pass it to the two onTap properties like this:

import 'package:flutter/material.dart';
import 'package:group_button/group_button.dart';

// ignore: must_be_immutable
class FirstQuestion extends StatefulWidget {
  List<String> radioButtons;
  FirstQuestion({required this.radioButtons, Key? key}) : super(key: key);

  @override
  State<FirstQuestion> createState() => _FirstQuestionState();
}

class _FirstQuestionState extends State<FirstQuestion> {
  late GroupButtonController _radioController;

  // ignore: prefer_typing_uninitialized_variables
  late List<String> _radioButtons;

  @override
  void initState() {
    _radioButtons = widget.radioButtons;
    _radioController = GroupButtonController(
      selectedIndexes: [0, 1, 2, 3],
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GroupButton(
          controller: _radioController,
          isRadio: false,
          options: const GroupButtonOptions(groupingType: GroupingType.column),
          buttons: _radioButtons,
          buttonIndexedBuilder: (selected, index, context) {
            return RadioTile(
              selected: _radioController.selectedIndex,
              index: index,
              onTap: () => onSelectedMethod(null, index, selected),
              title: _radioButtons[index],
            );
          },
          onSelected: (val, i, selected) => onSelectedMethod(val, i, selected)),
    );
  }

  //this method
  void onSelectedMethod(dynamic value, index, selected) {
    debugPrint('Button #$index $selected');
    _radioController.selectIndex(index);
  }
}

class RadioTile extends StatelessWidget {
  const RadioTile({
    Key? key,
    required this.selected,
    required this.onTap,
    required this.index,
    required this.title,
  }) : super(key: key);

  final String title;
  final int index;
  final int? selected;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(title),
      onTap: onTap,
      leading: Radio<int>(
        groupValue: selected,
        value: index,
        onChanged: (_) => onTap(),
      ),
    );
  }
}

Only the RadioTile will work, because you are putting it before the GroupButton, however with that method it will have the same function as the onSelected property

Related