【发布时间】:2021-09-26 22:38:31
【问题描述】:
单击单选按钮后,我正在尝试更改文本字段的标签。
这是我的例子:
如果我单击私有单选按钮,则文本字段标签应更改。 如果我单击单选按钮,我会尝试更改变量。 变量的值改变但不更新标签。
这里是完整代码:
//check radio
List<bool> btn_radio = [true, false];
//field name
String? lable;
//controller
TextEditingController _controller_Name = new TextEditingController();
class DebugPage extends StatefulWidget {
@override
_DebugPage createState() => _DebugPage();
}
class _DebugPage extends State<DebugPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: customSubAppBar("Debug", context),
body: _body(context));
}
}
//body
_body(context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(height: MediaQuery.of(context).size.height * 0.3),
BuildAppRadioButton(),
BuildAppFieldName(),
],
),
);
}
//radio button
class BuildAppRadioButton extends StatefulWidget {
@override
_BuildAppRadioButton createState() => _BuildAppRadioButton();
}
//radio button
class _BuildAppRadioButton extends State<BuildAppRadioButton> {
@override
Widget build(BuildContext context) {
return Row(
children: [
//spacer
Spacer(),
//radio button
Container(
width: MediaQuery.of(context).size.width * 0.37,
child: CustomRadioButtonRegister(
isPressed: btn_radio[0],
label: 'Private',
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: true,
groupValue: btn_radio[0],
onChanged: (bool newValue) {
setState(() {
btn_radio[0] = true;
btn_radio[1] = false;
lable = 'label';
});
},
),
),
//spacer
Spacer(),
//radion button
Container(
width: MediaQuery.of(context).size.width * 0.37,
child: CustomRadioButtonRegister(
isPressed: btn_radio[1],
label: "Company",
padding: const EdgeInsets.symmetric(horizontal: 5.0),
value: true,
groupValue: btn_radio[1],
onChanged: (bool newValue) {
setState(() {
btn_radio[1] = true;
btn_radio[0] = false;
});
},
),
),
//spacer
Spacer(),
],
);
}
}
//name
class BuildAppFieldName extends StatefulWidget {
@override
_BuildAppFieldName createState() => _BuildAppFieldName();
}
//name
class _BuildAppFieldName extends State<BuildAppFieldName> {
@override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.height * 0.09,
child: TextFormField(
//controller: _controller_companyName,
style: TextStyle(
color: Colors.black,
),
decoration: InputDecoration(
labelText: lable,
labelStyle: TextStyle(
color: Colors.black,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
),
),
),
onChanged: (value) {},
),
);
}
}
任何人都知道我该怎么做。 非常感谢(:
【问题讨论】:
-
创建变量
textLabel = label 1,然后当您按下单选按钮时,更改与您希望在onChanged回调中显示的值相对应的值。示例在 onChanged setState 中设置textLabel = label 2
标签: flutter textfield onchange