import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:healthcare_user/Sugar/sugar_history.dart'; import 'package:healthcare_user/common/settings.dart'; import 'package:intl/intl.dart'; class SugarCalculator extends StatefulWidget { const SugarCalculator({Key? key}) : super(key: key); @override State createState() => _SugarCalculatorState(); } class _SugarCalculatorState extends State { TextEditingController fastingValueController = TextEditingController(); TextEditingController postParandialValueController = TextEditingController(); TextEditingController dateInput = TextEditingController(); String sugarValue = ''; Color sugarValueColor = Colors.black; @override Widget build(BuildContext context) { return Scaffold( appBar: AppSettings.appBar('Diabetes'), body: SingleChildScrollView( child: Container( child: Padding( padding: EdgeInsets.all(10), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [ TextFormField( cursorColor: greyColor, controller: fastingValueController, textCapitalization: TextCapitalization.characters, keyboardType: TextInputType.number, decoration: const InputDecoration( prefixIcon: Icon( Icons.person, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: primaryColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), labelText: 'Enter fasting blood sugar value', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), SizedBox(height: 10), TextFormField( cursorColor: greyColor, controller: postParandialValueController, textCapitalization: TextCapitalization.characters, keyboardType: TextInputType.number, decoration: const InputDecoration( prefixIcon: Icon( Icons.person, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: primaryColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), labelText: 'Enter PostPrandial value', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), ), SizedBox(height: 10), SizedBox(height: 10), Container( child: TextFormField( cursorColor: greyColor, controller: dateInput, decoration: textFormFieldDecorationBMI( Icons.calendar_today, 'Enter Date'), readOnly: true, onTap: () async { DateTime? pickedDate = await showDatePicker( context: context, initialDate: DateTime.now(), firstDate: DateTime(1950), lastDate: DateTime.now(), builder: (BuildContext context, Widget? child) { return Theme( data: ThemeData.dark().copyWith( colorScheme: ColorScheme.dark( primary: buttonColors, onPrimary: Colors.white, surface: buttonColors, onSurface: Colors.white, ), dialogBackgroundColor: primaryColor, ), child: child!, ); }, ); if (pickedDate != null) { print( pickedDate); //pickedDate output format => 2021-03-10 00:00:00.000 String formattedDate = DateFormat('dd-MM-yyyy').format(pickedDate); print( formattedDate); //formatted date output using intl package => 2021-03-16 setState(() { dateInput.text = formattedDate; //set output date to TextField value. }); } else {} }, ), ), SizedBox(height: 10), Container( width: double.infinity, height: MediaQuery.of(context).size.height * .05, child: ElevatedButton( style: ElevatedButton.styleFrom( primary: buttonColors, // background onPrimary: Colors.black, // foreground ), onPressed: () async { if (fastingValueController.text != '' && postParandialValueController.text != ''&& dateInput.text!='') { if(int.parse(fastingValueController.text)>int.parse(postParandialValueController.text)){ AppSettings.longFailedToast('Please enter post parandial value is higher than fasting blood sugar value details'); } else{ AppSettings.preLoaderDialog(context); var payload = new Map(); payload["fasting"] = double.parse(fastingValueController.text.toString()); payload["postPrandial"] = double.parse(postParandialValueController.text.toString()); payload["date"] = dateInput.text.toString(); var value = await AppSettings.calculateSugar(payload); var valueResponse = jsonDecode(value); print(valueResponse); setState(() { sugarValue = valueResponse['userSugarDetails']['sugarCategory'].toString(); if(sugarValue.toUpperCase().toUpperCase()=='VERY HIGH BLOOD SUGAR'){ sugarValueColor=Colors.red; } else if(sugarValue.toUpperCase().toUpperCase()=='HIGH BLOOD SUGAR'){ sugarValueColor=Colors.red; } else if(sugarValue.toUpperCase().toUpperCase()=='PREDIABETES (IMPAIRED FASTING GLUCOSE)'){ sugarValueColor=Colors.red; } else if(sugarValue.toUpperCase().toUpperCase()=='VERY LOW BLOOD SUGAR'){ sugarValueColor=Colors.red; } else if(sugarValue.toUpperCase().toUpperCase()=='LOW BLOOD SUGAR'){ sugarValueColor=Colors.red; } else if(sugarValue.toUpperCase().toUpperCase()=='NORMAL BLOOD SUGAR (HEALTHY RANGE)'){ sugarValueColor=buttonColors; } }); fastingValueController.clear(); postParandialValueController.clear(); dateInput.clear(); Navigator.of(context,rootNavigator: true).pop(); } } else{ AppSettings.longFailedToast('Please enter valid details'); } }, child: const Text('Check Diabetes'), )), SizedBox(height: 10), Container( child: Row( children: [ Text(sugarValue,style: TextStyle(color: sugarValueColor,fontWeight: FontWeight.bold,fontSize: 20),), Visibility( visible:sugarValue!='' , child: Icon( Icons.emoji_emotions_outlined, color: buttonColors, size: 40, ),) ], ) ), SizedBox(height: 20), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( children: [ IconButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => SugarHistory()), ); }, icon: Icon( Icons.history, color: primaryColor, size: 40, ), ), SizedBox(height: 10), Padding(padding: EdgeInsets.fromLTRB(10,0,0,0), child: Container( child: Text('History',style:TextStyle(color:Colors.black,fontSize: 12,fontWeight: FontWeight.bold,)), ),) ], ), ElevatedButton( style: ElevatedButton.styleFrom( primary: buttonColors, // background onPrimary: Colors.black, // foreground ), onPressed: () async { Navigator.pop(context); }, child: const Text('Cancel'), ) ], ) ], ), ) ), ) ); } }