[英]Custom widget not working as expected - GetX package Flutter
I have created a custom widget to create a button as in the image .我创建了一个自定义小部件来创建图像中的按钮。 The Stages option works fine but when I toggle to the Bids List option, I get an output like this . Stages 选项工作正常,但是当我切换到 Bids List 选项时,我得到一个 output 像这样。 Why is the stages coming in grey color when I select the Bids List Option but the vice versa works fine?为什么当我 select 投标列表选项时,阶段以灰色显示,反之亦然? Can anyone correct it?任何人都可以纠正它吗? I am using GetX in my project.我在我的项目中使用 GetX。
SelectionOptionButton.dart选择选项按钮.dart
class SelectOptionButton extends StatelessWidget {
SelectOptionButton({Key? key}) : super(key: key);
final grey = const Color.fromARGB(50, 158, 158, 158);
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(color: Color(0xffc8a2c8) // Color(0xffE0E0E0),
), // Color.fromARGB(165, 158, 158, 158)),
BoxShadow(
color: Colors.white, // Color.fromARGB(21, 158, 158, 158),
spreadRadius: -1.0,
blurRadius: 15.0)
], //color: grey,
borderRadius: BorderRadius.all(Radius.circular(5))),
padding:
const EdgeInsets.only(top: 0.05, bottom: 0.05, left: 7, right: 7),
child: Row(children: [
SpecificButton(
isFirstOptionSelected: true, buttonName: 'Stages', id: 1),
SpecificButton(
isFirstOptionSelected: false, buttonName: 'Bids List', id: 2),
]),
);
}
}
class SpecificButton extends StatelessWidget {
SpecificButton(
{Key? key,
required this.isFirstOptionSelected,
required this.buttonName,
required this.id})
: super(key: key);
int id;
bool isFirstOptionSelected;
String buttonName;
final SelectOptionButtonController _selectOptionButtonController =
Get.put(SelectOptionButtonController());
final grey = Color.fromARGB(23, 243, 243, 243);
@override
Widget build(BuildContext context) {
return Obx(
() => Expanded(
child: ElevatedButton(
style: TextButton.styleFrom(
elevation: isFirstOptionSelected ? 1.5 : 0, //1.5:0,
backgroundColor: isFirstOptionSelected
? _selectOptionButtonController.isFirstOptionSelected.value
? Colors.white
: Colors.transparent //grey
: !_selectOptionButtonController.isFirstOptionSelected.value
? Colors.white
: Colors.transparent, //grey,
primary: isFirstOptionSelected
? _selectOptionButtonController.isFirstOptionSelected.value
? MyColors.primaryGradient
: Colors.black
: !_selectOptionButtonController.isFirstOptionSelected.value
? MyColors.primaryGradient
: Colors.black,
),
onPressed: () {
(id == 1 &&
_selectOptionButtonController
.isFirstOptionSelected.value ==
true) ||
(id == 2 &&
_selectOptionButtonController
.isFirstOptionSelected.value ==
false)
? null
: _selectOptionButtonController.changeSelected();
},
child: Text(
buttonName,
style: TextStyle(fontFamily: 'HelveticaBold'),
))),
);
}
}
This is the respective controller这是各自的 controller
class SelectOptionButtonController extends GetxController {
var isFirstOptionSelected = true.obs;
void changeSelected() {
isFirstOptionSelected.value = !isFirstOptionSelected.value;
}
}
you have to use Obx()
for reflect changes您必须使用Obx()
来反映更改
Obx(() => Row(children: [
SpecificButton(
isFirstOptionSelected: true, buttonName: 'Stages', id: 1),
SpecificButton(
isFirstOptionSelected: false, buttonName: 'Bids List', id: 2),
]),)
their are two type to change value which is also derive from it's state sample它们是改变值的两种类型,这也来自它的 state 样本
class SelectOptionButtonController extends GetxController {
var isFirstOptionSelected = true.obs;
void changeSelected() {
/// this part lack below called update();
isFirstOptionSelected.value = !isFirstOptionSelected.value;
update(); /// try add this to change the value good to use on this is GetBuilder
/// or
/// isFirstOptionSelected(!isFirstOptionSelected.value);
/// changing value for Obx
}
}
to change it state改变它 state
return Obx(()
=> Container(
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(color: Color(0xffc8a2c8) // Color(0xffE0E0E0),
), // Color.fromARGB(165, 158, 158, 158)),
BoxShadow(
color: Colors.white, // Color.fromARGB(21, 158, 158, 158),
spreadRadius: -1.0,
blurRadius: 15.0)
], //color: grey,
borderRadius: BorderRadius.all(Radius.circular(5))),
padding:
const EdgeInsets.only(top: 0.05, bottom: 0.05, left: 7, right: 7),
child: Row(children: [
SpecificButton(
isFirstOptionSelected: controller.isFirstOptionSelected.isTrue, buttonName: 'Stages', id: 1),
SpecificButton(
isFirstOptionSelected: controller.isFirstOptionSelected.isFalse, buttonName: 'Bids List', id: 2),
]),
);
);
try it.试试看。
do just replace your method to following method,只需将您的方法替换为以下方法,
void changeSelected() {
isFirstOptionSelected.value =! isFirstOptionSelected.value; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.