首页 > 解决方案 > Flutter - 执行计算的单选按钮

问题描述

例如,如果用户输入逗号等特殊字符,应用程序将显示最后一个值,则存在一个小问题。如何使其显示零值或其他显示错误的通知。

注意我在文本字段中使用“keyboardType:TextInputType.number”,除此之外,有时单击按钮后,它会第一次显示值为零。解决方法是我必须单击其他单选按钮才能显示值

例子:

https://i.stack.imgur.com/MnkQ1.png

代码:

    void handleTsTBRadioWater(int option) {
      setState(() {
        radioValue = option;
      });
      switch (radioValue) {
        case 1:
          output = input * 0.06763;
          break;
        case 2:
          output = input * 0.202884136;
          break;
      }
    }

    ListTile tb = ListTile(
      title: const Text('Tablespoon'),
      leading: Radio(
        value: 1,
        groupValue: radioValue,
        onChanged: (handleTsTBRadioWater),
      ),
    );

    ListTile ts = ListTile(
      title: const Text('Teaspoon'),
      leading: Radio(
        value: 2,
        groupValue: radioValue,
        onChanged: (handleTsTBRadioWater),
      ),
    );

    Container calcBtn = Container(
      child: ButtonTheme(
        minWidth: 40.0,
        height: 40.0,
        child: RaisedButton(
          child: Text("Calculate Water"),
          color: Colors.blueAccent,
          onPressed: () {
            AlertDialog dialog = AlertDialog(
                content: Text(
                    "${input.toStringAsFixed(2)} Gram : ${output.toStringAsFixed(2)} Teaspoon"));
            showDialog(builder: (context) => dialog, context: context);
          },
        ),
      ),
    );

标签: flutterradio-button

解决方案


推荐阅读