如何解决Flutter 和 GetX - 更新 RxObject 时小部件不会重绘
这里存在:
我的问题控制器
Rx<GitlabIssue> selectedissue = new GitlabIssue().obs;
RxList<GitlabIssue> issues = <GitlabIssue>[].obs;
void changeIssueState(GitlabIssueState state) async {
var stateValue = _getGitLabIssueStateValue(state);
bool isInternet = await Utils().isInternet();
if (isInternet) {
var resp = await _issueProvider.changeGitlabIssueState(
projectId,selectedissue.value.iid,stateValue);
if (resp.statusCode == 200) {
Get.snackbar(
'Issue state changed','Issue is $stateValue',);
var issueIndex = issues.indexOf(selectedissue.value);
selectedissue.value.state =
selectedissue.value.state == 'opened' ? 'closed' : 'opened';
issues[issueIndex] = selectedissue.value;
gitLabIssueBox.put(projectId,issues);
} else
Get.snackbar(
'Issue state not changed','Issue state was not correctly changed');
} else
print('No connectivity');
}
我的观点
Widget _getButtonStatus() {
return Obx(() {
Color stateColor = _issueController.selectedissue.value.state == 'closed'
? Colors.green
: Colors.red;
return OutlinedButton(
onpressed: () {
_issueController.selectedissue.value.state == 'closed'
? _issueController.changeIssueState(GitlabIssueState.reopen)
: _issueController.changeIssueState(GitlabIssueState.close);
},style: OutlinedButton.styleFrom(
//primary: Colors.white,//backgroundColor: Colors.teal,side: BorderSide(color: stateColor,width: 1),),child: Obx(
() => Text(
_issueController.selectedissue.value.state == 'closed'
? 'Reopen issue'
: 'Close issue',style: TextStyle(color: stateColor),);
});
}
我更新了 selectedissue.obs 的值,并将小部件放入 Obx() => 函数中。
我不明白为什么我的小部件没有重绘。
奇怪的是,我的 onpressed 函数行为正确;该按钮保持不变,但如果我再次单击它,我的问题状态会正确更改。
请帮帮我!! :D
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。