I have a getx Controller. In it i have declared two variables, which will be updated
var percentageEVS = 0.obs;
var percentOthers =0.obs;
Im trying to change the values of these variables using the following function
calculatespentTime(){
final totalDuration = dashboard[0].duration??0;
final durationEVS = dashboard[1].duration!.toInt();
final _percentageEVS = (durationEVS/totalDuration)*100;
percentageEVS.value = _percentageEVS.toInt() ;
}
How ever I'm not getting the changed values
final List<ChartData> chartData = [
ChartData(x: 'Maths', y:_controller.percentageEVS.value.toDouble(), color: Colors.red),
ChartData(x: 'English', y: 38, color: Colors.blue),
]
How can i get the changed value and pass it to chartData??
Change the line
ChartData(x: 'Maths', y:_controller.percentageEVS.value.toDouble(), color: Colors.red)
to
Obx(()=>ChartData(x: 'Maths', y:_controller.percentageEVS.value.toDouble(), color: Colors.red))
Related
I want to change the icon button color according to the variable value.
eg: if the variable value is greater than 40 icon color should be red other wise icon color is white.
I get a variable values from SQLite table.
following code, i have tried but its shows null check operator used on null value.\
int? tatalLeave=0;
IconButton(
onPressed: (() {
getTotalLeave();
}),
icon: Icon(
Icons.notifications_active_rounded,
color:
tatalLeave! >= 40 ? Colors.red : Colors.white,
size: 30.0,
),
)
following i have added sqlite codes
//get total number of leaves
getTotalLeave() async {
int? count = await DatabaseHelper.instance.countAllLeave();
setState(() {
tatalLeave = count;
print(tatalLeave);
});
}
}
db helper class
Future<int?> countAllLeave() async {
Database db = await database;
final allLeave = Sqflite.firstIntValue(
await db.rawQuery('SELECT SUM(num_leave_days) FROM leave_Details'));
return allLeave;
}
please help me to slove my issue.
Try modifiying the code as below,
As the getTotalLeave function is asynchronous you need to put await.
(await is to interrupt the process flow until the async method completes)
int? tatalLeave=0;
IconButton(
onPressed: (()async {
await getTotalLeave();
}),
icon: Icon(
Icons.notifications_active_rounded,
color:
tatalLeave! >= 40 ? Colors.red :
Colors.white,
size: 30.0,
),
)
GetX: Color Model Error
I separate the Model and Controller classes.
model. dart:
import 'package:get/get. dart'; import 'package:flutter/material. dart';
class Model { var name = "agus".obs; var color = Colors.red[700].obs; }
controller. dart:
import 'package:get/get. dart';
import '../models/models. dart';
class PersonController extends GetxController {
var models = Models();
void changeUpperCase() {
models.name.value = "testing";
}
}
main.dart:
var modelA = Get.put(OrangController());
body: Center(
child: Obx(() => Text(
"My name is ${modelA. models.name}",
style: TextStyle(fontSize: 35, color: modelA.models.color),
))),
floatingActionButton: FloatingActionButton(onPressed: () {
modelA. changeUpperCase();
})
In the VS Code IDE, I get an error at IDE:
The argument type 'Rx<Color?>' can't be assigned to the parameter type
'Color?'
When you write this line :
var color = Colors.red[700].obs;
This is not a just Color, it's an Rx<Color> observable that contains inside of it the Color value, it's equivalent to :
Rx<Color> color = Colors.red[700].obs;
So when you assign it directly to the color property like this:
// ...
style: TextStyle(fontSize: 35, color: modelA.models.color),
You're trying here to assign the wholeRx<Color> instead of only its value which is the Color, basically, you need to assign it like this:
// ...
style: TextStyle(fontSize: 35, color: modelA.models.color.value), // added .value here
Now the error should be gone and the code works normally.
I have proxy object companyCustomColors:
class CustomColors {
final CompanyCustomColors companyCustomColors;
CustomColors(BuildContext context)
: companyCustomColors =
Theme.of(context).extension<CompanyCustomColors>() ?? defaultColors;
Color get vipColor => companyCustomColors.vipColor;
Color get linksColor => companyCustomColors.linksColor;
Color get linkPressedColor => companyCustomColors.linkPressedColor;
}
Is it possible to use some Dart features (proxy, mixin, delegate) to get rid of these getters (vipColor, linksColor, linkPressedColor), but still have IDE autocomplete suggestions for CustomColors?
This object used like this one:
Text('sample',
style: TextStyle(
color: CustomColors(context).vipColor,
height: lineHeight,
),
)
Other classes used in this example:
class CompanyCustomColors extends ThemeExtension<CompanyCustomColors> {
const CompanyCustomColors({
required this.vipColor,
required this.linksColor,
required this.linkPressedColor,
});
final Color vipColor;
final Color linksColor;
final Color linkPressedColor;
}
const CompanyCustomColors defaultColors = CompanyCustomColors(
vipColor: AppColors.orange,
linksColor: AppColors.blue,
linkPressedColor: AppColors.blue_pressed,
);
so I am using MultiSelectBottomSheetField in this package. I posted on their github as well as an issue but it seems fairly inactive so i came here looking for help.
And I am having some issues with the initialValue parameter for it. So at the moment, I have data saved in firestore as a string but its in the format of a list. And what i was trying to do was get the string data from firestore -> convert to a list with the respective class -> and then show as initial value in the above package/widget. But whats happening is that the initial value isnt showing, even though the value is not empty.
So for context this is how I change to list from firestore string:
List<Skill?> skillList = [];
void changeSkillToList(String? stringList) {
int indexOfOpenBracket = stringList!.indexOf("[");
int indexOfLastBracket = stringList.lastIndexOf("]");
var noBracketString =
stringList.substring(indexOfOpenBracket + 1, indexOfLastBracket);
var list = noBracketString.split(", ");
for (var i = 0; i < list.length; i++) {
skillList.add(Skill(id: 1, name: list[i].toString()));
}
}
this is how i use the acc widget:
final _skillItems =
skill.map((skill) => MultiSelectItem<Skill>(skill, skill.name)).toList();
MultiSelectBottomSheetField<Skill?>(
selectedColor: Color(0xFF5DB075),
selectedItemsTextStyle:
TextStyle(color: Colors.white),
initialChildSize: 0.4,
decoration: BoxDecoration(),
listType: MultiSelectListType.CHIP,
initialValue: skillList,
searchable: true,
items: _skillItems,
buttonText: Text("Select your skills...",
style: GoogleFonts.inter(
color: Color(0xFFBDBDBD),
fontSize: 16)),
onConfirm: (values) {
context
.read(pharmacistSignUpProvider.notifier)
.changeSkillList(values);
},
chipDisplay: MultiSelectChipDisplay(
items: context
.read(pharmacistSignUpProvider.notifier)
.skillList
?.map((e) =>
MultiSelectItem(e, e.toString()))
.toList(),
chipColor: Color(0xFF5DB075),
onTap: (value) {
context
.read(
pharmacistSignUpProvider.notifier)
.skillList
?.remove(value);
return context
.read(
pharmacistSignUpProvider.notifier)
.skillList;
},
textStyle: TextStyle(color: Colors.white),
),
),
And this is my initState:
List<Skill?> skillList = [];
#override
void initState() {
skillList = changeSkillToList(context
.read(pharmacistMainProvider.notifier)
.userDataMap?["knownSkills"]);
print(skillList);
super.initState();
}
If someone could help me out, it would be very appreciated. Let me know if you guys have any questions
Thanks!!
I get some problem and I fix it by adding the == operator to my entity in your case skill
#override
bool operator ==(Object other) {
return other is Skill && this.id == other.id;
}
inside your Skill class
I tried this code and its working fine for the first time.
double a = 2, b = 3, c = 5;
var color;
Map<String, double> dataMap = Map();
List<Color> colorList = [
Colors.red,
Colors.green,
Colors.yellow,
];
void changeGraph() {
dataMap.putIfAbsent("Fat", () => c);
dataMap.putIfAbsent("Protein", () => b);
dataMap.putIfAbsent("Carbs", () => a);
}
void initState() {
super.initState();
changeGraph();
}
and
PieChart(
dataMap: dataMap,
animationDuration: Duration(milliseconds: 800),
chartLegendSpacing: 32.0,
chartRadius: MediaQuery.of(context).size.width / 2.7,
showChartValuesInPercentage: true,
showChartValues: true,
showChartValuesOutside: false,
chartValueBackgroundColor: Colors.grey[200],
colorList: colorList,
showLegends: true,
legendPosition: LegendPosition.right,
decimalPlaces: 1,
showChartValueLabel: true,
initialAngle: 0,
chartValueStyle: defaultChartValueStyle.copyWith(
color: Colors.blueGrey[900].withOpacity(0.9),
),
chartType: ChartType.disc,
)
then after getting values from user i tried this method for changing the graph
setState(() {
a = newA;
b = newB;
c = newC;
});
also i try to call changeGraph() method but the graph is not changing and its showing the value that it shows first time.
Is there any way to change the values ?
What your'e doing here is changing the values of the variables only and not the values inside the map.The map has the value of a = 2 and not a reference to a.
Meaning that when you say dataMap.putIfAbsent("Carbs", () => a); the value of "Carbs" is not a but it's actually 2 since the value here is an int and not a reference.
In order to change the value of Carbs in the map you need to directly change it from the map itself by doing for example datamap["Carbs"] = newA. Same goes to b and c
Let me know if that doesn't work