flutter web onPressed 按钮不起作用

flutter web onPressed Button is not working


这是代码:

class BmiScreen extends StatefulWidget {
  const BmiScreen({Key? key}) : super(key: key);

  @override
  State<BmiScreen> createState() => _BmiScreenState();
}

class _BmiScreenState extends State<BmiScreen> {
  double result = 0;
  TextEditingController height = TextEditingController();
  TextEditingController weight = TextEditingController();
  bool isMetric = true;
  bool isImperial = false;
  late List<bool> isSelected;

  @override
  void initState() {
    isSelected = [isMetric,isImperial];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          ToggleButtons(
            isSelected: isSelected,
            onPressed: toggleMeasure,
            children: const [
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: Text('Metric',style: TextStyle(fontSize: 18),),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 16),
                child: Text('Imperial',style: TextStyle(fontSize: 18),),
              ),
            ],
          ),
          Row(children: [Text('Height ${((isMetric) ? "(cm)" : "(inches)")}')]),
          TextField(
            controller: height,
          ),
          Row(children: [Text('Weight ${((isMetric) ? "(kg)" : "(pounds)")}')]),
          TextField(
            controller: weight,
          ),
          Text('Result : $result'),
          ElevatedButton(
            onPressed: (){
              debugPrint(countBmi(int.parse(height.toString()),int.parse(weight.toString())).toString());
              setState(() {
                result = countBmi(int.parse(height.toString()),int.parse(weight.toString()));
              });
            }, 
            child: const Text('Count')
          )
        ],
      ),
    ); 
  }

  double countBmi(int height, int weight){
    final double heightMeter = height/100;
    return weight/(math.pow(heightMeter, 2));

  }
  

  void toggleMeasure(value){
    if(value==0){
      isMetric = true;
      isImperial = false;
    }else{
      isMetric = false;
      isImperial = true;
    }
    setState(() {
      isSelected = [isMetric, isImperial];
    });
  }
}

这是错误:

══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following FormatException was thrown while handling a gesture:
TextEditingController#d73aa(TextEditingValue(text: ┤├, selection: TextSelection.invalid, composing:
TextRange(start: -1, end: -1)))

似乎 flutter web 无法执行 'onPressed' 但 'onTap' 正在网络上运行,但问题是 ElevatedButton 没有 'onTap'

有没有替代按钮的方法或者有什么方法可以修复它?

heightweightTextEditingController。要从 TextEditingController 获取文本,您需要使用 .text.

final textFromTextEditingController = TextEditingController.text.toString();

我使用 tryParse 因为文本可能会变为空。和 null 不能被解析为 int 并且会抛出错误。

// you can also return from the method instead of providing default value
final _height = int.tryParse(height.text.toString()) ?? 0; 
final _weight = int.tryParse(weight.text.toString()) ?? 1;

 result = countBmi(_height, _weight );

更多关于 TextEditingController and int.tryParse