根据滑块值设置文本值

时间:2019-02-02 21:04:27

标签: dart visual-studio-code flutter

根据滑块值设置文本元素

我正在尝试将文本元素设置为由滑块设置的表情符号的描述。我在滑块上设置了5种不同的表情符号,因此值是0-4,因为表情符号列表是一个数组。文本不更新corrctly基于我有当前的代码。

 const _emojis = ['','\u{1F928}','\u{1F610}','\u{1F612}',''];


            child: Slider(
                      value: _value,
                     // label: _emojis[_value.toInt()],
                      min: 0.0,
                      max: 4.0,
                      divisions: 4,
                      onChangeStart: (double value) {
                        print('Start value is ' + value.toString());                         
                      },
                      onChangeEnd: (double value) {
                        print('Finish value is ' + value.toString());
                        if(value.toString() == '0.0'){
                        return  _emotionalStatus = 'Happy';
                        }
                         if(value.toString() == '1.0'){
                        return  _emotionalStatus = 'Optimistic';
                        }
                          if(value.toString() == '2.0'){
                        return  _emotionalStatus = 'Neutral';
                        }
                          if(value.toString() == '3.0'){
                        return  _emotionalStatus = 'Pessimistic';
                        }
                          if(value.toString() == '4.0'){
                        return  _emotionalStatus = 'Sad';
                        }
                      },
                      onChanged: (double value) {
                        setState(() {
                          _value = value;
                        });
                      },
                      activeColor: Colors.white,
                      inactiveColor: Colors.white,
                    ),   

1 个答案:

答案 0 :(得分:1)

更改您的onChangeEnd方法,不需要返回值,只需在满足条件后调用setState:

     onChangeEnd: (double value) {
              print('Finish value is ' + value.toString());
              if (value.toString() == '0.0') {
                 _emotionalStatus = 'Happy';
              }
              if (value.toString() == '1.0') {
                 _emotionalStatus = 'Optimistic';
              }
              if (value.toString() == '2.0') {
                 _emotionalStatus = 'Neutral';
              }
              if (value.toString() == '3.0') {
                 _emotionalStatus = 'Pessimistic';
              }
              if (value.toString() == '4.0') {
                 _emotionalStatus = 'Sad';
              }
              setState(() {

              });
            },