描述错误 我有两个控制器,第一个是 LoginController,它用于保存用户登录状态,第二个是 settingsController 来管理应用程序设置,我在设置小部件中有一个切换器按钮来更改用户状态(可用,不可用),以执行此操作我使用 Get.put ,它可以显示用户状态,但是当我尝试更改状态时出现问题,设置小部件没有改变它的状态
**复制代码
class AppPages {
static const INITIAL = AppRoutes.RootURL;
static final routes = [
//Simple GetPage
GetPage(
name: AppRoutes.HomePageURL,
page: () => HomePage(),
binding: BindingsBuilder(() => Get.lazyPut(() => HomeController())),
transition: Transition.fade,
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.LoginPageURL,
page: () => LoginPage(),
binding: BindingsBuilder (() => Get.put<LoginController>(LoginController(), permanent: true)),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.RegisterPageURL,
page: () => RegisterPage(),
binding: BindingsBuilder(() => Get.lazyPut(() => RegisterController())),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.SettingsPageURL,
page: () => SettingsPage(),
binding: BindingsBuilder (() => Get.put<SettingsController>(SettingsController(), permanent: true)
),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.NotifiesPageURL,
page: () => NotifiesPage(),
binding: BindingsBuilder(
() => Get.lazyPut(() => NotifiesController())
),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.OrdersPageUrl,
page: () => OrdersPage(),
binding: BindingsBuilder(
() => Get.lazyPut(() => OrdersController())
),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.OrdersPageUrl,
page: () => OrderContainerPage(),
binding: BindingsBuilder(
() => Get.lazyPut(() => OrdersController())
),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.OrderDetailsPageUrl,
page: () => OrderDetailsPage(),
binding: BindingsBuilder(() => Get.lazyPut(() => OrderDetailsController())),
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.ContactPageURL,
page: () => ContactPage(),
binding: BindingsBuilder(() => Get.lazyPut(() => ContactController())),
transition: Transition.fade,
parameter: {},
middlewares: [],
children: [],
),
GetPage(
name: AppRoutes.PagesUrl,
page: () => PostsPage(),
binding: BindingsBuilder(() => Get.lazyPut(() => PostsController())),
transition: Transition.fade,
parameter: {},
middlewares: [],
children: [],
),
];
}
class SettingsPage extends GetView<SettingsController> {
final user = Get.put(LoginController());
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Obx(
() => Container(
height: 70,
decoration: BoxDecoration(
color: AppColors.primaryColor,
backgroundBlendMode: BlendMode.softLight,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
"change status",
style: AppStyles.primaryMediumTextStyle,
),
_buildAvailableButton('Available', 1),
_buildAvailableButton('Not Available', 0),
],
),
),
)
);
}
_buildAvailableButton(text, btnStatus) {
return Container(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 100, height: 35),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: user.userData.value.status == btnStatus
? AppColors.primaryColor
: AppColors.secondaryColor,
// AppColors.primaryColor, AppColors.secondaryColor
shape: RoundedRectangleBorder(
side: BorderSide(
color: AppColors.primaryColor,
width: 1,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(65))),
child: Text(text,
style: user.userData.value.status == btnStatus
? AppStyles.primaryCardText
: AppStyles.secondaryCardText),
// AppStyles.primaryCardText AppStyles.secondaryCardText
// AppStyles.secondaryCardText
onPressed: () async {
user.userData.value.status = btnStatus ;
},
),
),
);
}
}
复制 重现行为的步骤:
预期行为 当 Change user status 时,Widget 应该重建以影响这个改变
Flutter 版本: 颤振 2.0.5 Getx 版本: 得到:^4.1.4
请描述您发现该错误的设备: 例如:小米安卓。
最少的复制代码
onPressed: () async {
user.userData.value.status = btnStatus ;
},
答案 0 :(得分:1)
只需用 Obx 包裹您的 ElevatedButton :
_buildAvailableButton(text, btnStatus) {
return Container(
alignment: Alignment.center,
child: ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 100, height: 35),
child: Obx(()=> ElevatedButton(
style: ElevatedButton.styleFrom(
primary: user.userData.value.status == btnStatus
? AppColors.primaryColor
: AppColors.secondaryColor,
// AppColors.primaryColor, AppColors.secondaryColor
shape: RoundedRectangleBorder(
side: BorderSide(
color: AppColors.primaryColor,
width: 1,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(65))),
child: Text(text,
style: user.userData.value.status == btnStatus
? AppStyles.primaryCardText
: AppStyles.secondaryCardText),
// AppStyles.primaryCardText AppStyles.secondaryCardText
// AppStyles.secondaryCardText
onPressed: () async {
user.userData.value.status = btnStatus ;
},
),
),
),
);
}