列表视图不刷新时,我删除最后剩余的项目颤振



我试图删除列表视图中最后剩余的项目。当有多个项目,我试图删除其中一个时,列表正在更新,但不更新最后一个剩余的项目。

这是我的代码!

//下面的代码用于显示listview

_showTaskList() {
return Expanded(
child: Obx(
() {
return ListView.builder(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),
itemCount: _taskController.taskList.length,
itemBuilder: (_, index) {
Task task = _taskController.taskList[index];
_taskController.getTask();
if (task.repeat == 'Daily') {
DateTime date =
DateFormat.jm().parse(task.startTime.toString());
var myTime = DateFormat('HH:mm').format(date);
print(myTime);
notifyHelper.scheduledNotification(
int.parse(myTime.toString().split(':')[0]),
int.parse(myTime.toString().split(':')[1]),
task,
);
return AnimationConfiguration.staggeredList(
duration: Duration(seconds: 1),
position: index,
child: SlideAnimation(
child: FadeInAnimation(
child: GestureDetector(
onTap: () => Get.to(
() => NotifiedScreen(task: task),
),
child: TaskTile(
task,
),
),
),
),
);
}
if (task.date == DateFormat.yMd().format(selectedDate)) {
return AnimationConfiguration.staggeredList(
duration: Duration(seconds: 1),
position: index,
child: SlideAnimation(
child: FadeInAnimation(
child: GestureDetector(
onTap: () {
_showBottomSheet(context, task);
// _taskController.getTask();
},
child: TaskTile(
task,
),
),
),
),
);
} else {
return Container();
}
},
);
},
),
);
}

这是delete bottomsheet.

_showBottomSheet(BuildContext context, Task task) {
Get.bottomSheet(
Container(
padding: EdgeInsets.only(top: 4),
height: task.isCompleted == 1
? MediaQuery.of(context).size.height * 0.24
: MediaQuery.of(context).size.height * 0.32,
color: Colors.white,
child: Column(
children: [
Container(
height: 6,
width: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey.shade300,
),
),
Spacer(),
task.isCompleted == 1
? Container()
: _bottomSheetButton(
label: 'Task Completed',
onTap: () {
_taskController.markTaskCompleted(task.id!);
Get.back();
},
clr: Color(0xff2702DC),
context: context,
),
_bottomSheetButton(
label: 'Delete Task',
onTap: () {
_taskController.delete(task);

Get.back();

},
clr: Colors.red.shade300,
context: context,
),
SizedBox(
height: 20,
),
_bottomSheetButton(
isClose: true,
label: 'Close',
onTap: () {
Get.back();
},
clr: Colors.red.shade300,
context: context,
),
],
),
),
);
}

这是一个删除方法。

void delete(Task task) {
DBHelper.delete(task);
getTask();
update();
}
void getTask() async {
List<Map<String, dynamic>>? tasks = await DBHelper.query();
if (tasks != null) {
taskList.assignAll(tasks.map((data) => Task.fromJson(data)).toList());
update();
} else {
null;
}
print(tasks?.length);
// it is show null when i try to delete the item.
}

您必须在删除项后刷新您的状态,这意味着在删除最后一项后调用setState()

相关内容

最新更新