简体   繁体   English

flutter web onPressed按钮不起作用

[英]flutter web onPressed Button is not working


here is the code :这是代码:

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];
    });
  }
}

here is the error :这是错误:

══╡ 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)))

it seems like flutter web cannot perform an 'onPressed' but 'onTap' is working on the web, but the problem is ElevatedButton doesnt have 'onTap'似乎颤动的网络无法执行“onPressed”,但“onTap”正在网络上运行,但问题是 ElevatedButton 没有“onTap”

is there any alternative for button or is there any ways to fix it?按钮是否有任何替代方案,或者有什么方法可以修复它?

height and weight are TextEditingController . heightweightTextEditingController To get text from TextEditingController you need use .text .要从TextEditingController获取文本,您需要使用.text

final textFromTextEditingController = TextEditingController.text.toString();

And I use tryParse because it is possible text will get null.我使用tryParse是因为文本可能会为空。 and null cant be parse into int and will throw errors.并且 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 );

More about TextEditingController and int.tryParse更多关于TextEditingControllerint.tryParse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM