【问题标题】:How to use ValueListenableBuilder and the ValueNotifier on my radio button so when the radio Button is checked,it displays the formfield如何在我的单选按钮上使用 ValueListenableBuilder 和 ValueNotifier,以便在选中单选按钮时显示表单域
【发布时间】:2022-11-01 22:37:24
【问题描述】:

所以我将我的表单域包装在一个可见的小部件中,以在选中单选按钮时隐藏和显示文本表单域,但我工作正常但你有时必须双击它想要使用 ValueListenableBuilder 和 ValueNotifier 来存档它。


                        
              
Visibility(
                visible: _isVisible,
                child: Container(
                  width: 396,
                  height: 73,
                  decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Color(0xff000000).withOpacity(0.08),
                          offset: Offset(0, 2),
                          blurRadius: 6,
                        ),
                      ],
                      color: Color(0xffFAFAFA),
                      borderRadius: BorderRadius.circular(17)),
                  child: Padding(
                    padding: const EdgeInsets.fromLTRB(15, 25, 0, 0),
                    child: TextFormField(
                      enabled: _character == SingingCharacter.unchecked
                          ? true
                          : false,
                      onSaved: (Value) => print(hello),
                      decoration: InputDecoration(
                        hintStyle: TextStyle(
                          fontFamily: "Proxima Nova",
                          fontWeight: FontWeight.w300,
                        ),
                        border: InputBorder.none,
                        labelStyle: TextStyle(
                          color: Color(0xffFAFAFA),
                        ),
                      ),
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(RegExp(r"[0-9]+|\s"))
                      ],
                      controller: kiloMeter,
                      validator: (value) {
                        if (value != null && value.isEmpty || value != 1000) {
                          return 'Please enter your Kilometer';
                        }
                        return null;
                      },
                    ),
                  ),
                ),
              ),


【问题讨论】:

  • _isVisible 是什么,你能提供完整的小部件和你的值通知器吗

标签: flutter dart flutter-layout


【解决方案1】:

创建一个valueNotiferbool 喜欢

final ValueNotifier<bool> _visiBilityNotifier = ValueNotifier(false);

用例将是

ValueListenableBuilder<bool>(
  valueListenable: _visiBilityNotifier,
  builder: (context, value, child) {
    return value ? Text("VisibleWidgget") : const SizedBox.shrink();
  },
)

更新值使用

_visiBilityNotifier.value  = yourNewValue; //true or false

您可以直接值(不会刷新 UI)

_visiBilityNotifier.value 

【讨论】:

    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多