I am trying to insert a row of data into a sqflite db with three columns
- id
- value of water consumed
- date I want to use the date so that for every day, there will be one set of data since every day will get one date. and later update it if the user wished to increase the value of water drank on that day.
this the widget in charge of a group of buttons. it will hold the db commands to insert and update. My issue here is that i have no need for a builder and therefore cant induce a "index" variable which i can use to filter my values in my table - i dont know how to go about it as i am only familiar with this method.
class WaterButtonsGrouped extends StatelessWidget {
//past here are my attempts to fix this issue- didnt work
int? WaterDrank = 0;
late int TotalWaterDrank = 0;
WaterButtonsGrouped({super.key});
String date = DateFormat.yMMMd().format(DateTime.now());
String dateNow = DateFormat.yMMMd().format(DateTime.now());
var _filteredInputs = [];
getUserInputDate() async {
_filteredInputs = await WaterDBHelper.getWaterDateFilter();
print('1 ${_filteredInputs.length}');
return _filteredInputs;
}
addInput(WaterButtonModel) {
print('${WaterButtonModel.waterMillimeters} ml');
WaterDrank = WaterButtonModel.waterMillimeters;
TotalWaterDrank = TotalWaterDrank + WaterDrank!;
print(date);
getUserInputDate();
checkDate();
return TotalWaterDrank;
}
addWater() {
return TotalWaterDrank;
}
// date in db is equal to date-now then 2 options
checkDate() async {
if (_filteredInputs.isNotEmpty) {
//insert
print('insert command ran - ${TotalWaterDrank}');
_addWater();
}
else {
//update milliliters
print('update command ran - ${TotalWaterDrank}');
_updateWaterThrouDate(dateNow);
}
}
@override
Widget build(BuildContext context) {
//buttons that will be shown from this widget - not important
return Wrap(
children: [
WaterButton(
buttonModel: WaterButtonModel.cup(),
onPressed: addInput,
),
WaterButton(
buttonModel: WaterButtonModel.glass(),
onPressed: addInput,
),
WaterButton(
buttonModel: WaterButtonModel.bottle(),
onPressed: addInput,
),
WaterButton(
buttonModel: WaterButtonModel.canister(),
onPressed: addInput,
),
],
);
}
//db commands
Future <void> _addWater() async {
await WaterDBHelper.insertWater(
date,
TotalWaterDrank
);
}
Future <void> _updateWater(int id) async {
await WaterDBHelper.updateWaterDrank(
id,
TotalWaterDrank,
);
}
Future <void> _updateWaterThrouDate(String date) async {
await WaterDBHelper.updateWaterDrankThrouDate(
date,
TotalWaterDrank
);
}
}
my database is working correctly after tests, i am very comfortable with creating dbhelpers - no issues here.
here is my "main" page that will link all associated data. it will present the group of buttons above and a circular progress indicator that i have linked below.
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget> [
SizedBox(height: 15),
Align(
alignment: Alignment.center,
child: Text(
"Hydration Level",
style: GoogleFonts.comfortaa(
fontSize: 50,
),
),
),
SizedBox(height: 50),
Container(
width: 300,
height: 300,
child: WaterProgressIndicator(WaterDrank: addedWater()),
),
SizedBox(height: 50),
WaterButtonsGrouped(),
],
),
),
);
}
addedWater() {
int TotalWaterDrank = WaterButtonsGrouped().addWater();
return TotalWaterDrank!;
}
}
here is the indicator. this will receive the data from the WaterButtonsGrouped() class and send any information to update the values here.
class WaterProgressIndicator extends StatefulWidget {
int? WaterDrank;
WaterProgressIndicator({
this.WaterDrank,
});
@override
State<WaterProgressIndicator> createState() => _WaterProgressIndicatorState(WaterDrank!);
}
class _WaterProgressIndicatorState extends State<WaterProgressIndicator> {
late double Percentage = 0;
int WaterGoal = 2000;
int WaterDrank;
_WaterProgressIndicatorState(this.WaterDrank);
PercentageTotal() {
setState(() {
Percentage = WaterDrank / WaterGoal;
});
return Percentage;
}
@override
void initState() {
super.initState();
setState(() {
PercentageTotal();
print('${Percentage * 100} %');
});
}
@override
Widget build(BuildContext context) {
return LiquidCircularProgressIndicator(
value: PercentageTotal(),
borderColor: Colors.blue.shade800,
borderWidth: 7.0,
direction: Axis.vertical,
center: Column(
children: [
SizedBox(height: 50),
Text(
'${PercentageTotal() * 100} %',
style: GoogleFonts.deliciousHandrawn(
color: Colors.blue.shade700,
fontSize: 100,
)),
Text(
'DRANK',
style: TextStyle(
color: Colors.black,
fontSize: 25,
)),
Text('TOTAL',
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 20,
)),
],
)
);
}
}
.Just to reiterate. The Water() class will be the final location to where all the data will be stored.
.The WaterButtonsGrouped() class will be in-charge of the functionality of the buttons, here is where i'm having troubles as i can insert data into db but not update it as for some reason my if operator is not workings as intended
.progress indicator will receive updated data from either the db or WaterButtonsGrouped() class - i have not finished it as i am stuck with the index problem
In my weird case it was working fine. i only had to delete the db and have it created again.