gpt4 book ai didi

flutter - 如何返回StatefulWidget包含的值以在ListView.builder中使用它?

转载 作者:行者123 更新时间:2023-12-03 04:49:19 25 4
gpt4 key购买 nike

我有一个问题,碰巧我有一个带有返回StatefulWidget的方法的ListView.builder

带有两个子项的列的ListView.builder:textField和带有两个rowradios

radio 按钮内置于另一个StatefulWidget中,对于这个选定的值,我对这个statefullwidget感兴趣,但是我在上一个StatefulWidget中需要此返回值。

我不知道除了返回一个小部件以返回另一个值之外,是否还可以使用StatefulWidget wigdet,请帮助我。

这是ListView.builder:

ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: preguntas.secciones[0].preguntas.length,
itemBuilder: (BuildContext context, int i) {
final pregunta = questionsList[i];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(pregunta.descripcion,
style: TextStyle(fontFamily: "OpenSans-Bold")),
RadioButtom(),
TextFormField(
enabled:
(!_radioValue && pregunta.respuestas[0].pregunta.obligatoria)
? false
: true,
validator: (value) {
if (pregunta.respuestas[0].pregunta.obligatoria == true &&
value.isEmpty) {
return 'Por favor ingrese texto';
}
return null;
},
decoration: InputDecoration(
hintMaxLines: 500,
hintText: pregunta.respuestas[0].pregunta.descripcion,
hintStyle:
TextStyle(fontFamily: "OpenSans-Regular", fontSize: 14.0),
),
onChanged: (value) {
preguntasGlobal = preguntas;
// preguntasGlobal[i].respuestas[0].comentario = value;
// preguntasGlobal[i].respuestas[0].respuesta = true;
},
),
SizedBox(height: 20.0)
],
);
},
);

这是RadioButtom StatefulWidget

class RadioButtom extends StatefulWidget {
@override
_RadioButtomState createState() => _RadioButtomState();
}

class _RadioButtomState extends State<RadioButtom> {
bool _radioValue =
false; //se usa como estado y tambien para mantener el valor del radiobuttom seleccionado
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Radio(
value: true,
groupValue: _radioValue,
onChanged: (value) {
setState(() {
_radioValue = value;
});
}),
Text(
'Yes',
style: TextStyle(
fontSize: 12.0,
fontFamily: "OpenSans-Regular",
),
),
Radio(
value: false,
groupValue: _radioValue,
onChanged: (value) {
setState(() {
_radioValue = value;
});
}),
Text(
'No',
style: TextStyle(
fontSize: 12.0,
fontFamily: "OpenSans-Regular",
),
)
],
);
}
}

最佳答案

您提升状态。您无需将状态存储在RadioButtom中,而是将状态存储在包含ListView的小部件中,例如ListViewBuilder

  • 更改RadioButtom的构造函数以接受一个值和一个回调

  • class RadioButtom extends StatefulWidget {
    @override
    _RadioButtomState createState() => _RadioButtomState();

    RadioButtom({this.value, this.onChanged});

    final bool value;
    final ValueChanged<bool> onChanged;
    }
  • 使用您的State字段

  • Radio(
    value: true,
    groupValue: widget.value,
    onChanged: widget.onChanged,
    ),
  • 将您的状态存储在ListViewBuilder


  • // For example
    List<bool> _answers = []; // Initialize it with number of elements you need
  • 在您的itemBuilder中使用它

  • RadioButtom(
    value: _answers[i],
    onChanged: (value) {
    setState(() {
    _answers[i] = value;
    });
    }),
    ),

    关于flutter - 如何返回StatefulWidget包含的值以在ListView.builder中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61674632/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com