I have listviewBuilder , I want sorting my listview based on DateCreate with this code
taskList.sort((a, b) => -a.dateCreate.compareTo(b.dateCreate));
Everything is fine, i can see my list Sort (Descending) based on DateCreate, But the problem is everytime i delete/update the list, the action is wrong index!
It's without Sort (Working)
It's using Sort (Failed !!!)
If you see in Failed gif , I want deleted index 0 , But index 1 is deleted. If without Sort list is fine.
I surely passing right index, because in Working gif, i can successfully delete right index.
How can i fix this ?
ListviewBuilder Source Code :
SingleChildScrollView(
child: Column(
children: <Widget>[
WatchBoxBuilder(
box: Hive.box("task_box"),
builder: (ctx, box) {
if (box.isEmpty) {
return Container(
margin: EdgeInsets.symmetric(
vertical: mqHeight / 8,
horizontal: 8,
),
height: mqHeight / 3.5,
width: double.infinity,
alignment: Alignment.topCenter,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: const AssetImage("assets/images/empty.png"),
),
),
child: Text(
'Task Empty , Add Someone',
style: textTheme.title
.copyWith(color: Colors.black.withOpacity(.5)),
textAlign: TextAlign.center,
),
);
} else {
final taskList = box.values.toList().cast<TaskModel>();
taskList
.sort((a, b) => -a.dateCreate.compareTo(b.dateCreate));
return Container(
margin: EdgeInsets.symmetric(
vertical: mqHeight / 8,
horizontal: 8,
),
child: ListView.builder(
shrinkWrap: true,
itemCount: taskList.length,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
final taskValue = taskList[index];
return CardOddCustom(
idTask: taskValue.idTask,
codeIcon: taskValue.codeIcon,
titleTask: taskValue.titleTask,
imagePath: taskValue.imageTask,
dateCreate: taskValue.dateCreate,
indexTask: index,
);
},
),
);
}
},
)
],
),
),
Deleting Code
void deleteCategory({@required int indexBox}) async {
final hiveBox = Hive.box(taskBox);
await hiveBox.deleteAt(indexBox);
notifyListeners();
}
Onpressed Function :
class CardOddCustom extends StatefulWidget {
final DateTime idTask;
final String titleTask;
final String imagePath;
final DateTime dateCreate;
final int codeIcon;
final int indexTask;
CardOddCustom({
this.idTask,
this.titleTask,
this.imagePath,
this.dateCreate,
this.codeIcon,
this.indexTask,
});
@override
_CardOddCustomState createState() => _CardOddCustomState();
}
class _CardOddCustomState extends State<CardOddCustom> {
return InkResponse(
onLongPress: () =>
taskProvider.deleteCategory(indexBox: widget.indexTask),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(80),
bottomRight: Radius.circular(80),
),
),

