【问题标题】:how to hide TextFormField based on a value of Radio Button OR DropdownButtonFormField in Flutter如何在 Flutter 中根据 Radio Button OR DropdownButtonFormField 的值隐藏 TextFormField
【发布时间】:2022-02-12 10:05:29
【问题描述】:

我是 Flutter 和 Dart 语言的新手,我正在努力自学。

所以,我的问题是我有一个 DropdownButtonFormField 有 2 个值(私人聊天和电话号码):

Widget menu(){
  return DropdownButtonFormField(
    decoration: InputDecoration(
      border: OutlineInputBorder(),
      labelText: 'how to contact with the buyer?',
      labelStyle: TextStyle(
          fontSize: 16,
          color: Colors.grey,
          fontFamily: 'Almarai'
        ),
    ),
  items: <String>['phone number', 'private chat'].map((String value) {
    return DropdownMenuItem<String>(
      value: value,
      child: Text(value),
    );
  }).toList(),
  onChanged: (String newValue) {
    setState(() {
      dropdownvalue = newValue;
    });
  },
);
}

我有这个TextFormField,如果选择了(私人聊天)选项,我希望它隐藏:

 Widget phonenum(){
    return
          TextFormField( 
            maxLength: 10,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              labelText: 'phone number',
              labelStyle: TextStyle(
                fontSize: 16,
                color: Colors.grey,
                fontFamily: 'Almarai'
              ),
            ),
          );
  }

请问我该如何实现?而且,如何使用 RadioButton 而不是 DropdownButtonFormField 来实现它?因为我还没有决定哪一个更适合我的项目流程。

【问题讨论】:

    标签: flutter dart radio-button


    【解决方案1】:

    您可以在文本字段小部件前面设置 if else 语句。 例如,如果wishOnePerson 为真,则仅在此处显示文本“ABC 先生”。您可以设置一个布尔值,并使用 setState() 切换其值。真时显示时假隐藏。

    Column(
      children: <Widget>[
        Text('Good Morning'), // Always visible
        if (wishOnePerson) Text(' Mr ABC'), // Only visible if condition is true
      ],
    ) 
    

    【讨论】:

      【解决方案2】:
       Widget phonenum(){
      
          if(dropdownvalue= 'phone number'){
              return TextFormField( 
                maxLength: 10,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'phone number',
                  labelStyle: TextStyle(
                    fontSize: 16,
                    color: Colors.grey,
                    fontFamily: 'Almarai'
                  ),
                ),
              );
          }else{
                 return Container();
          
                }
        }
      

      【讨论】:

        猜你喜欢
        • 2021-08-25
        • 2020-04-28
        • 1970-01-01
        • 2019-05-04
        • 2022-01-22
        • 2020-08-11
        • 1970-01-01
        • 2020-08-19
        • 1970-01-01
        相关资源
        最近更新 更多