Flutter动态收音机

问题描述 投票:0回答:1

我创建了一个包含 5 个问题的动态表单,每个问题有 5 个单选选项和一个好的答案,我可以使用每个答案的单选按钮来选择

但我无法得到按钮答案:在此处输入图像描述

当我点击单选按钮时我没有改变


class EditQuizPage extends StatefulWidget {
  const EditQuizPage({
    super.key,
    required this.user,
  });
  final UserModel user;
  @override
  State<EditQuizPage> createState() => _EditQuizPagetate();
}


class _EditQuizPagetate extends State<EditQuizPage> {
  final _formKey = GlobalKey<FormBuilderState>();
  final List<Widget> fields = [];
  var _newTextFieldId = 0;
  String savedValue = '';

  @override
  void initState() {
    savedValue = _formKey.currentState?.value.toString() ?? '';
    final textFieldId = _newTextFieldId;
    final newTextFieldName = 'Q_${_newTextFieldId++}';
    final newTextFieldKey = ValueKey(_newTextFieldId);
    fields.add(NewTextField(
      key: newTextFieldKey,
      name: newTextFieldName,
      textFieldId:textFieldId,
      onDelete: () {
        setState(() {
          fields.removeWhere((e) => e.key == newTextFieldKey);
        });
      },
    ));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FormBuilder(
      key: _formKey,
      // IMPORTANT to remove all references from dynamic field when delete
      clearValueOnUnregister: true,
      child: Column(
        children: <Widget>[
          const SizedBox(height: 20),
          ...fields,
          const SizedBox(height: 10),
          Row(
            children: <Widget>[
              Expanded(
                child: ElevatedButton(
                  child: const Text("Submit"),
                  onPressed: () {
                    _formKey.currentState!.saveAndValidate();
                    setState(() {
                      savedValue =
                          _formKey.currentState?.value.toString() ?? '';
                    });
                  },
                ),
              ),
              const SizedBox(width: 20),
              (fields.length<4)?
              Expanded(
                child: ElevatedButton(
                  child: const Text("Ajouter une question"),
                  onPressed: () {
                    final textFieldId = _newTextFieldId;
                    final newTextFieldName = 'Q_${_newTextFieldId++}';
                    final newTextFieldKey = ValueKey(_newTextFieldId);
                    setState(() {
                      fields.add(NewTextField(
                        key: newTextFieldKey,
                        name: newTextFieldName,
                        textFieldId:textFieldId,
                        onDelete: () {
                          setState(() {
                            fields.removeWhere((e) => e.key == newTextFieldKey);
                          });
                        },
                      ));

                    });
                  },
                ),
              ):const Text(''),
            ],
          ),
          const Divider(height: 40),
          Text('Saved value: $savedValue'),
        ],
      ),
    );
  }

}

class NewTextField extends StatefulWidget {
  const NewTextField({
    super.key,
    required this.name,
    required this.textFieldId,
    this.onDelete,
  });
  final String name;
  final int textFieldId;
  final VoidCallback? onDelete;

  @override
  State<NewTextField> createState() => _NewTextFieldState();
}

int? selectedOption = 0;
class _NewTextFieldState extends State<NewTextField> {
  final List<Widget> options = [];
  var _optionsId = 0;


  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(
                child: FormBuilderTextField(
                  name: widget.name,
                  validator: FormBuilderValidators.minLength(4),
                  decoration: const InputDecoration(
                    label: Text('Nouvelle question'),
                  ),
                ),
              ),
              IconButton(
                icon: const Icon(Icons.delete_forever),
                onPressed: widget.onDelete,
              ),
            ]
            ,
          ),
          ...options,
          Row(children: [
            Expanded(
              child: ElevatedButton(
                child: const Text("Ajouter une réponse"),
                onPressed: () {
                  final index = _optionsId;
                  final newOptiondName = 'A_${widget.textFieldId}_${_optionsId++}';
                  final newOptiondKey = ValueKey(_optionsId);
                  setState(() {

                    options.add(NewOptionField(
                      key: newOptiondKey,
                      index: index,
                      name: newOptiondName,
                      group: widget.textFieldId,
                      onDelete: () {
                        setState(() {
                          options.removeWhere((e) => e.key == newOptiondKey);
                        });
                      },
                    ));

                  });
                },
              ),
            ),
          ],)
        ],
      ),
    );
  }
}

class NewOptionField extends StatefulWidget {
  const NewOptionField({
    super.key,
    required this.index,
    required this.name,
    required this.group,
    this.onDelete,
  });

  final int index;
  final String name;
  final int group;
  final VoidCallback? onDelete;

  @override
  State<NewOptionField> createState() => _NewOptionFieldState();
}


class _NewOptionFieldState extends State<NewOptionField> {


  @override
  Widget build(BuildContext context) {

print('${widget.index}  $selectedOption');
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Row(
        children: [
          Radio<int>(
            value: widget.index,
            groupValue: selectedOption,
            onChanged: (value) {
              setState(() {
                selectedOption = value;
                print("Button value: $value");
              });
            },
          ),
          Expanded(
            child:FormBuilderTextField(
            name: widget.name,
            validator: FormBuilderValidators.minLength(3),
            decoration: const InputDecoration(
              label: Text('Réponse'),
            ),
          ),
          ),

          IconButton(
            icon: const Icon(Icons.delete_forever),
            onPressed: widget.onDelete,
          ),
        ],
      ),
    );
  }
}

我尝试为刷新状态添加父函数

flutter
1个回答
0
投票

抱歉,Crunchy无法理解这一点。访问 Crunchy 也许你会得到一些东西。

© www.soinside.com 2019 - 2024. All rights reserved.