import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:healthcare_user/models/bmi_history_model.dart'; import 'package:healthcare_user/common/settings.dart'; import 'package:healthcare_user/models/bp_history_model.dart'; class BPHistory extends StatefulWidget { const BPHistory({Key? key}) : super(key: key); @override State createState() => _BPHistoryState(); } class _BPHistoryState extends State { bool isLoading=false; List BpHistoryList = []; List FilteredList = []; var dateItems = [ 'last 7 days', 'last one month', 'last one year', ]; var dateItemsVariable = 'last 7 days'; Future getBmiHistoryDetails(var selectedRange) async { isLoading=true; var response1= await AppSettings.getBPHistory(); print(response1); setState(() { BpHistoryList = ((jsonDecode(response1)) as List).map((dynamic model) { return BPHistoryModel.fromJson(model); }).toList(); var now = new DateTime.now(); var now_1w = now.subtract(Duration(days: 7)); var now_1m = new DateTime(now.year, now.month-1, now.day); var now_1y = new DateTime(now.year-1, now.month, now.day); if(selectedRange.toString().toUpperCase()=='LAST 7 DAYS'){ FilteredList=BpHistoryList.where((product) { final date = product.dateForFilter; return now_1w.isBefore(date); }).toList(); } else if(selectedRange.toString().toUpperCase()=='LAST ONE MONTH'){ FilteredList=BpHistoryList.where((product) { final date = product.dateForFilter; return now_1m.isBefore(date); }).toList(); } else if(selectedRange.toString().toUpperCase()=='LAST ONE YEAR'){ FilteredList=BpHistoryList.where((product) { final date = product.dateForFilter; return now_1y.isBefore(date); }).toList(); } else{ FilteredList=BpHistoryList; } isLoading=false; }); } @override void initState() { getBmiHistoryDetails(dateItemsVariable); super.initState(); } Widget renderzUi(){ if(BpHistoryList.length!=0){ return Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Padding(padding: EdgeInsets.fromLTRB(10, 10, 10, 10), child: DropdownButtonFormField( // Initial Value value: dateItemsVariable, isExpanded: true, decoration: const InputDecoration( prefixIcon: Icon( Icons.calendar_month, color: primaryColor, ), border: OutlineInputBorder( borderSide: BorderSide(color: primaryColor)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: primaryColor), ), labelText: 'Units', labelStyle: TextStyle( color: greyColor, //<-- SEE HERE ), ), hint: Text('Units'), // Down Arrow Icon //icon: const Icon(Icons.keyboard_arrow_down), // Array list of items items: dateItems.map((String items) { return DropdownMenuItem( value: items, child: Text( items, style: TextStyle( fontSize: 16, ), textAlign: TextAlign.center, )); }).toList(), // After selecting the desired option,it will // change button value to selected value onChanged: (String? newValue) { setState(() { dateItemsVariable = newValue!; }); getBmiHistoryDetails(dateItemsVariable); }, ),), Expanded(child:ListView.builder( padding: EdgeInsets.all(0), itemCount: FilteredList.length, itemBuilder: (BuildContext context, int index) { return Card( color: Colors.white, child: Padding( padding:EdgeInsets.all(8) , child: Container( width: MediaQuery.of(context).size.width * .55, child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ /* Image( image: const AssetImage('images/height.png'), height: 25, width: 25, fit: BoxFit.fill),*/ Text('Systolic',style: TextStyle(color: primaryColor),), SizedBox(width: 5,), Padding(padding: EdgeInsets.all(1), child: Text(FilteredList[index].systolic.toString().toUpperCase(),style: valuesTextStyle()),) ], ), Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('Diastolic',style: TextStyle(color: primaryColor),), Padding(padding: EdgeInsets.all(1), child: Text(FilteredList[index].diastolic.toString().toUpperCase(),style: valuesTextStyle()),) ], ), Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Image( image: const AssetImage('images/height.png'), height: 25, width: 25, fit: BoxFit.fill), Padding(padding: EdgeInsets.all(5), child: Text(FilteredList[index].bpText.toString().toUpperCase(),style: valuesTextStyle()),) ], ), Row( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Image( image: const AssetImage('images/date.png'), height: 25, width: 25, fit: BoxFit.fill), Padding(padding: EdgeInsets.all(5), child: Text(FilteredList[index].displayDate.toString().toUpperCase(),style: valuesTextStyle()),) ], ), ], ), ), ), ); }) ), ]); } else{ return Center( child: Padding( padding: EdgeInsets.fromLTRB(0, 40, 0, 0), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ SizedBox(height: MediaQuery.of(context).size.height * .25,), Text('No data'), SizedBox( height: 20, ), CircleAvatar( backgroundColor: primaryColor, radius: 40, child: IconButton( iconSize: 40, icon: const Icon( Icons.info, color: Colors.white, ), onPressed: () async { }, ), ) ], ), ) ); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppSettings.appBar('Blood Pressure'), body: isLoading?Center( child: CircularProgressIndicator( color: primaryColor, strokeWidth: 5.0, ), ):renderzUi(), ); } }