import 'package:cpd_2022_energy/provider/energy_model.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class InputWidget extends StatelessWidget { const InputWidget({super.key}); @override Widget build(BuildContext context) { var heightStartInputController = TextEditingController(); var heightEndInputController = TextEditingController(); var velocityStartInputController = TextEditingController(); var velocityEndInputController = TextEditingController(); var weightInputController = TextEditingController(); final ButtonStyle style = ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)); final formKey = GlobalKey(); final energyModel = Provider.of(context, listen: false); void onSubmitButtonClick() { if (formKey.currentState!.validate() == false) return; energyModel.heightStart = double.parse(heightStartInputController.text); energyModel.heightEnd = double.parse(heightEndInputController.text); energyModel.velocityStart = double.parse(velocityStartInputController.text); energyModel.velocityEnd = double.parse(velocityEndInputController.text); energyModel.weight = double.parse(weightInputController.text); energyModel.calc(); } bool isNumeric(String s) { return double.tryParse(s) != null; } return Form( key: formKey, child: Column( children: [ const Text('Height'), TextFormField( decoration: const InputDecoration( hintText: 'Enter the height of the starting point (in m)', ), validator: (String? value) { if (value == null || value.isEmpty || !isNumeric(value)) { return 'Invalid input. Please enter the height of the starting point (in m)'; } return null; }, controller: heightStartInputController, ), TextFormField( decoration: const InputDecoration( hintText: 'Enter the height of the ending point (in m)', ), validator: (String? value) { if (value == null || value.isEmpty || !isNumeric(value)) { return 'Invalid input. Please enter the height of the ending point (in m)'; } return null; }, controller: heightEndInputController, ), const Text('Velocity'), TextFormField( decoration: const InputDecoration( hintText: 'Enter the velocity of the starting point (in s)', ), validator: (String? value) { if (value == null || value.isEmpty || !isNumeric(value)) { return 'Invalid input. Please enter the velocity of the starting point (in s)'; } return null; }, controller: velocityStartInputController, ), TextFormField( decoration: const InputDecoration( hintText: 'Enter the velocity of the ending point (in s)', ), validator: (String? value) { if (value == null || value.isEmpty || !isNumeric(value)) { return 'Invalid input. Please enter the velocity of the ending point (in s)'; } return null; }, controller: velocityEndInputController, ), const Text('Weight'), TextFormField( decoration: const InputDecoration( hintText: 'Enter the weight in kg', ), validator: (String? value) { if (value == null || value.isEmpty || !isNumeric(value)) { return 'Invalid input. Please enter the weight (in kg)'; } return null; }, controller: weightInputController, ), ElevatedButton( style: style, onPressed: onSubmitButtonClick, child: const Text('Calculate Energy'), ), ], ), ); } }