Flutter виджет Slidable не работает callback
Учусь Flutter и пытаюсь воспроизвести пример из книги. Но так как в книге версия Slidable старее, а сейчас новее, то у меня не получается. Вот ошибка: "The argument type 'Future' can't be assigned to the parameter type 'void Function(BuildContext)?'."
import "package:flutter/material.dart";
import "package:scoped_model/scoped_model.dart";
import "package:flutter_slidable/flutter_slidable.dart";
import "notes_db_worker.dart";
import "notes_model.dart" show Note, NotesModel, notesModel;
class NotesList extends StatelessWidget {
const NotesList({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
print("## NotesList.build()");
// Return widget.
return ScopedModel<NotesModel>(
model : notesModel,
child : ScopedModelDescendant<NotesModel>(
builder : (inContext, inChild, inModel) {
return Scaffold(
// Add note.
floatingActionButton : FloatingActionButton(
child : const Icon(Icons.add, color : Colors.white),
onPressed : () async {
notesModel.entityBeingEdited = Note();
notesModel.setColor('');
notesModel.setStackIndex(1);
}
),
body : ListView.builder(
itemCount : notesModel.entityList.length,
itemBuilder : (BuildContext inBuildContext, int inIndex) {
Note note = notesModel.entityList[inIndex];
// Determine note background color (default to white if none was selected).
Color color = Colors.white;
switch (note.color) {
case "red" : color = Colors.red; break;
case "green" : color = Colors.green; break;
case "blue" : color = Colors.blue; break;
case "yellow" : color = Colors.yellow; break;
case "grey" : color = Colors.grey; break;
case "purple" : color = Colors.purple; break;
}
return Container(
padding : const EdgeInsets.fromLTRB(20, 20, 20, 0),
child : Slidable(
startActionPane: ActionPane(
// A motion is a widget used to control how the pane animates.
motion: const ScrollMotion(),
// A pane can dismiss the Slidable.
dismissible: DismissiblePane(onDismissed: () {}),
// All actions are defined in the children parameter.
children: [
// A SlidableAction can have an icon and/or a label.
SlidableAction(
onPressed: _deleteNote(inContext, note),
backgroundColor: Color(0xFFFE4A49),
foregroundColor: Colors.white,
icon: Icons.delete,
label: 'Delete',
),
],
),
child : Card(
elevation : 8,
color : color,
child : ListTile(
title : Text("${note.title}"),
subtitle : Text("${note.content}"),
// Edit existing note.
onTap : () async {
// Get the data from the database and send to the edit view.
notesModel.entityBeingEdited = (await NotesDBWorker.db.get(note.id)) as int;
notesModel.setColor(notesModel.entityBeingEdited.color);
notesModel.setStackIndex(1);
}
)
) /* End Card. */
) /* End Slidable. */
); /* End Container. */
} /* End itemBuilder. */
) /* End End ListView.builder. */
); /* End Scaffold. */
} /* End ScopedModelDescendant builder. */
) /* End ScopedModelDescendant. */
); /* End ScopedModel. */
} /* End build(). */
Future _deleteNote(BuildContext inContext, Note inNote) async {
print("## NotestList._deleteNote(): inNote = $inNote");
showDialog(
context : inContext,
barrierDismissible : false,
builder : (BuildContext inAlertContext) {
return AlertDialog(
title : const Text("Delete Note"),
content : Text("Are you sure you want to delete ${inNote.title}?"),
actions : [
TextButton(child : const Text("Cancel"),
onPressed: () {
// Just hide dialog.
Navigator.of(inAlertContext).pop();
}
),
TextButton(child : const Text("Delete"),
onPressed : () async {
// Delete from database, then hide dialog, show SnackBar, then re-load data for the list.
await NotesDBWorker.db.delete(inNote.id);
Navigator.of(inAlertContext).pop();
Scaffold.of(inContext).showSnackBar(
const SnackBar(
backgroundColor : Colors.red,
duration : Duration(seconds : 2),
content : Text("Note deleted")
)
);
// Reload data from database to update list.
notesModel.loadData("notes", NotesDBWorker.db);
}
)
]
);
}
);
} /* End _deleteNote(). */
} /* End class. */
Ответы (1 шт):
Автор решения: brun0
→ Ссылка
Вы передаёте некорректное выражение в параметр onPressed у SlidableAction. Вам следует указать анонимную функцию или метод, вызывающий действие _deleteNote(), например, вот так:
SlidableAction(
onPressed: (context) => _deleteNote(context, note),
backgroundColor: Color(0xFFFE4A49),
foregroundColor: Colors.white,
icon: Icons.delete,
label: 'Delete',
)