Home > Net >  flutter web onPressed Button is not working
flutter web onPressed Button is not working

Time:05-18


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'

is there any alternative for button or is there any ways to fix it?

CodePudding user response:

height and weight are TextEditingController. To get text from TextEditingController you need use .text.

final textFromTextEditingController = TextEditingController.text.toString();

And I use tryParse because it is possible text will get null. and null cant be parse into int and will throw errors.

// 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

  • Related