首页 > 解决方案 > 如何根据 Flutter 中的条件启用/禁用 DropDownButton?

问题描述

我是 Flutter 的新手,我正在制作休假申请,因为我创建了两个 DropdownButton。在From Date Half Leave:的一个 DropdownButton 和To Date Half Leave的第二个DropdownButton 中。我在两个下拉按钮中都显示了相同的项目列表。问题是,当我从From Date Half Leave中选择相同的项目时,它不应该在第二个 DropdownButton 中隐藏相同的选项,这意味着在To Date Half Leave中。

例如,如果From dateTo date 相同,那么我想在两个下拉按钮中显示选项,但是当用户从From Date Half Leave中选择一个项目时,它会隐藏To Date Half Leave中的项目选项。如果从日期到日期不同,那么它在从日期半假到日期半假中显示相反的选项,例如从日期半假选择早上然后在截止日期半假中我不显示早上选项它只显示下午选项如果开始日期半假截止日期半假只有那么不同。我不能这样做所以请帮助我..

我的代码如下:我只是尝试禁用To Date Half Leave的项目,当From DateTo Date相同时,当用户选择From Date Half Leave下拉按钮项目时,它禁用To Date Half Leave中的项目。

var _days = ['早上', '下午',];

    Padding(
                   padding: const EdgeInsets.only(top:8.0),
                   child: Row(
                  children: <Widget>[
                    Container(
                      //width: 200.0,
                      child: Text('From Date :',
                      style: Theme.of(context).textTheme.subtitle2.copyWith(color: Colors.black),
                      ),
                    ),            
                  ]
                ),
              ), 
                 Container(
                       child: Column(
                         children: <Widget>[
                           ListTile(
                            trailing: Icon(Icons.calendar_today),
                            title: Text('$_formdate'),
                            contentPadding: EdgeInsets.only(left:4.0,right:0.0,),
                            enabled: true,
                            onTap: (){
                             _chooseDate(context);
                             } 
                          ),
                        ],
                      ),
                    ), 
                Padding(
                   padding: const EdgeInsets.only(top:8.0),
                     child: Row(
                  children: <Widget>[
                    Container(
                      child: Text('To Date :',
                      style: Theme.of(context).textTheme.subtitle2.copyWith(color: Colors.black),
                      ),
                    ),            
                  ]
                ),
               ), 
               Container(
                       child: Column(
                        children: <Widget>[
                        ListTile(
                        trailing: Icon(Icons.calendar_today),
                        title: Text('$_todate'),
                        contentPadding: EdgeInsets.only(left:4.0,right:0.0,),
                        enabled: true,
                        onTap: (){
                        _chooseData2(context);
                             } 
                          ),
                        ],
                      ),
                    ),
                Padding(
                  padding: const EdgeInsets.only(top:8.0),
                  child: Row(
                  children: <Widget>[
                    Container(
                    child: Text('From Date Half leave :',
                    style: Theme.of(context).textTheme.subtitle2.copyWith(color: Colors.black),
                      ),
                    ),            
                  ]
                ),
             ),   
        new DropdownButton<String>(
            dropdownColor: Colors.grey[300],
            value: currentFDHLvalue,
            isExpanded: true,
            items: _days.map((String value) {
              return new DropdownMenuItem<String>(
              value: value,
                child: new Text(value),
                            );
                          }).toList(),
                          underline: Container(
                        height: 1,
                        color: Colors.black26,
            ),
              onChanged: (newValue) {             
              if (_formdate == _todate && currentFDHLvalue != null) {
                setState(() {  });
                             } else{
                               setState((){ 
                                 currentTDHLvalue;
                               disabledItems = false;
                               });
                             }
                         setState(() { 
                            currentFDHLvalue = newValue;
                          }); 
                        },     
          ) ,
      Padding(
        padding: const EdgeInsets.only(top:8.0),
           child: Row(
            hildren: <Widget>[
            Container(
            child: Text('To Date Half leave :',
            style: Theme.of(context).textTheme.subtitle2.copyWith(color: Colors.black),
          ),
        ),            
      ]
    ),
  ),            
        new DropdownButton<String>(
          dropdownColor: Colors.grey[300],
            value: currentTDHLvalue,
            isExpanded: true,
            elevation: 22,
            items: _days.map((String value) {
               return new DropdownMenuItem<String>(
                value: value,
                 child: new Text(value,
                 style: TextStyle(
                  color: !disabledItems ? Colors.grey : null,
            )
          ),
        );
      }).toList(),
        underline: Container(
         height: 1,
         color: Colors.black26,
            ),
      onChanged: (newValue) {
              if (!disabledItems ? _formdate != _todate : currentTDHLvalue != newValue ) {
                setState(() {
                  currentTDHLvalue = newValue;
                  disabledItems = false;
                });
              }
            setState(() {
              currentTDHLvalue = newValue;
              });   
        },
) , 

图片

标签: flutterdartdropdownbutton

解决方案


推荐阅读