-
-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Plate 46 calc #688
base: master
Are you sure you want to change the base?
Plate 46 calc #688
Changes from all commits
b919e62
debd328
2395284
9cfa6c1
3c3d245
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class PlateConfiguration extends ChangeNotifier { | ||
//olympic standard weights | ||
List<double> _plateWeights = [1.25, 2.5, 5, 10, 15, 20, 25]; | ||
|
||
List<double> get plateWeights => _plateWeights; | ||
|
||
void setPlateWeights(List<double> weights) { | ||
_plateWeights = weights; | ||
notifyListeners(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:wger/helpers/gym_mode.dart'; | ||
|
||
class PlateWeights extends ChangeNotifier{ | ||
String unitOfPlate = 'kg'; | ||
bool flag = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this do? Probably needs a better name ;) |
||
num totalWeight = 0; | ||
num barWeight = 20; | ||
num convertTolbs = 2.205; | ||
num totalWeightInKg = 0; | ||
num barWeightInKg = 20; | ||
List<num> selectedWeights = []; | ||
List<num> kgWeights = [1.25, 2.5, 5, 10, 15, 20, 25]; | ||
List<num> lbsWeights = [2.5, 5, 10, 25, 35, 45]; | ||
List<num> customPlates = []; | ||
late Map<num,int> grouped; | ||
|
||
void unitChange() { | ||
if(unitOfPlate=='lbs') { | ||
totalWeight = totalWeightInKg; | ||
unitOfPlate = 'kg'; | ||
barWeight = barWeightInKg; | ||
} else { | ||
unitOfPlate = 'lbs'; | ||
totalWeight = totalWeightInKg*2.205; | ||
barWeight = barWeightInKg*2.205; | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
void toggleSelection(num x) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would save the selected weights to the device, then we don't need to add this every time. You can simply save the array to SharedPreferences (just dump it as a string and later read from that) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am encountering difficulties in calculating plate denominations using the saved plate weight denominations while implementing shared preferences. I've explored several approaches: Async Function in initState(): I attempted to call an async function containing await loadFromSharedPref() within the initState() of my widget. However, this resulted in widget.getPlates executing before loadFromSharedPref(), leading to an empty list being displayed initially but it is being shown inside the slider selection menu. Calling loadFromSharedPref() from main(): I tried calling loadFromSharedPref() from the main() function to ensure it executed before widget.getPlates. While this successfully loaded and printed the saved values in the main() function, the widget.getPlates still displayed an empty list but it is being shown inside the slider selection menu. Flag in PlateWeightsProvider: I introduced a flag (loadedFromSharedPref) within the PlateWeightsProvider, setting it to true after loadFromSharedPref() completes. In widget.getPlates, I attempted to wait for the flag to become true before displaying the plate denominations. Unfortunately, this approach also failed to display the saved results correctly, and I'm now unable to select/deselect any options or reset them. I would greatly appreciate any guidance or suggestions on how to effectively load and display shared preferences within my PlateWeightsProvider and ensure proper functionality within my widgets. Screen.Recording.2024-12-22.at.4.36.30.PM.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mhh, hadn't thought this would cause problems. I'll take a look! |
||
if(unitOfPlate == 'kg') { | ||
if(selectedWeights.contains(x)) { | ||
selectedWeights.remove(x); | ||
} else { | ||
selectedWeights.add(x); | ||
} | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't the if and else branches the same? :D In this respect, I think the different variables could be simplified, e.g.having always totalWeight and totalWeightKg seems redundant. |
||
if(selectedWeights.contains(x)) { | ||
selectedWeights.remove(x); | ||
} else { | ||
selectedWeights.add(x); | ||
} | ||
} | ||
notifyListeners(); | ||
} | ||
|
||
void clear() { | ||
selectedWeights.clear(); | ||
notifyListeners(); | ||
} | ||
|
||
void setWeight(num x) { | ||
totalWeight = x; | ||
totalWeightInKg=x; | ||
notifyListeners(); | ||
} | ||
|
||
void calculatePlates() { | ||
selectedWeights.sort(); | ||
customPlates = plateCalculator(totalWeight,barWeight,selectedWeights); | ||
grouped = groupPlates(customPlates); | ||
notifyListeners(); | ||
} | ||
|
||
void resetPlates() { | ||
selectedWeights = []; | ||
notifyListeners(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:wger/providers/plate_weights.dart'; | ||
|
||
class AddPlateWeights extends StatefulWidget { | ||
const AddPlateWeights({super.key}); | ||
|
||
@override | ||
State<AddPlateWeights> createState() => _AddPlateWeightsState(); | ||
} | ||
|
||
class _AddPlateWeightsState extends State<AddPlateWeights> { | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Consumer<PlateWeights> ( | ||
builder:(context,plateProvider,child)=> Scaffold ( | ||
appBar: AppBar ( | ||
title: const Text('Enter Custom Plate Weights'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps "Select available plates" would be better. Also, all user-facing strings should be translatable (this also applies for the buttons below, for those there should be already strings). For this, just add the string to |
||
), | ||
body: Column ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
children: [ | ||
Row ( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Text('Kg or lbs'), | ||
DropdownButton ( | ||
onChanged: (newValue){ | ||
plateProvider.clear(); | ||
if((newValue!) != plateProvider.unitOfPlate) { | ||
plateProvider.unitChange(); | ||
} | ||
}, | ||
items: ['kg','lbs'].map((unit){ | ||
return DropdownMenuItem<String> ( | ||
value: unit, | ||
child: Text(unit), | ||
); | ||
}).toList(), | ||
), | ||
], | ||
), | ||
SingleChildScrollView ( | ||
scrollDirection: Axis.horizontal, | ||
child: Row ( | ||
children: plateProvider.unitOfPlate == 'kg' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both branches are basically the same, you can just do plateProvider.unitOfPlate == 'kg' ? '$number kg' : '$number lbs' within the Text widget |
||
? plateProvider.kgWeights.map((number) { | ||
return GestureDetector( | ||
onTap: () => plateProvider.toggleSelection(number), | ||
child: Container ( | ||
margin: const EdgeInsets.all(8), | ||
padding: const EdgeInsets.all(16), | ||
decoration: BoxDecoration ( | ||
color: plateProvider.selectedWeights.contains(number) | ||
? const Color.fromARGB(255, 82, 226, 236) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of hard coding a color, I would use something from the Theme, then this will also automatically work on the dark mode. There should be something in |
||
: const Color.fromARGB(255, 97, 105, 101), | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
child: Text ( | ||
'$number kg', // Add unit to text | ||
style: const TextStyle( | ||
color: Colors.white, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
), | ||
); | ||
}).toList() | ||
: plateProvider.lbsWeights.map((number) { | ||
return GestureDetector( | ||
onTap: () => plateProvider.toggleSelection(number), | ||
child: Container( | ||
margin: const EdgeInsets.all(8), | ||
padding: const EdgeInsets.all(16), | ||
decoration: BoxDecoration ( | ||
color: plateProvider.selectedWeights.contains(number) | ||
? const Color.fromARGB(255, 82, 226, 236) | ||
: const Color.fromARGB(255, 97, 105, 101), | ||
borderRadius: BorderRadius.circular(10), | ||
), | ||
child: Text ( | ||
'$number lbs', // Add unit to text | ||
style: const TextStyle( | ||
color: Colors.white, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
), | ||
); | ||
}).toList(), | ||
), | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
TextButton( | ||
onPressed: (){ | ||
if(plateProvider.selectedWeights.isNotEmpty){ | ||
plateProvider.flag=true; | ||
plateProvider.calculatePlates();} | ||
Navigator.pop(context); | ||
}, | ||
child: const Text('Done'), | ||
), | ||
ElevatedButton( | ||
onPressed: (){ | ||
plateProvider.resetPlates(); | ||
}, | ||
child: const Text('Reset',style: TextStyle(color: Colors.red,fontWeight: FontWeight.bold)) | ||
), | ||
], | ||
), | ||
] | ||
), | ||
), | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a boolean would be better here, something like isMetric, then we don't have to use magic strings in the code, which will avoid mistakes like e.g. writing "Lb"