// On this noteItem long press i want to show a snackbar
class NoteItem extends StatelessWidget {
NoteItem({Note note, this.removeNote}): note = note, super(key: ObjectKey(note));
final Note note;
final RemoveNote removeNote;
@override
Widget build(BuildContext context){
return ListTile(
onLongPress: () {
removeNote(note);
},
leading: CircleAvatar(
backgroundColor: Theme.of(context).primaryColor,
child: Text(note.title[0]),
),
title: Text(note.title),
);
}
}
// This class gives the state`enter code here`
class _NoteListState extends State<NoteList> {
void _addNote() {
setState(() {
widget.notes.add(Note(title: 'New Note', description: 'Successfully Added New Note'));
});
}
void _removeNote(Note note) {
setState(() {
widget.notes.remove(note);
Scaffold.of(context).showSnackBar( SnackBar( content: Text('Note Deleted!')));
});
}
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Notes List'),
),
body: ListView(
padding: EdgeInsets.symmetric(vertical: 8.0),
children: widget.notes.map((Note note) {
return NoteItem(
note: note,
removeNote: _removeNote,
);
}).toList(),
),
floatingActionButton: FloatingActionButton(
onPressed: _addNote,
tooltip: 'Add Note',
child: Icon(Icons.add),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想查看完整代码,请转到此链接:- https://pastebin.com/h5HJWwdg
我试图从 NoteItem 返回一个脚手架,但在收到错误后意识到你不能这样做。还尝试使用同样不起作用的构建器。如果可能的话,如果您直接访问某些文档,这将很有帮助,这样我将来就可以避免此类错误。
我正在学习 Flutter 并且几天前才开始使用它,所以如果你也告诉我我在笔记应用程序上的工作方式是否正确,那将会很有帮助。
_removeNote 方法(您尝试获取 Scaffold 的位置)是 NodeListState 的一个属性,它是 NodeList 的状态。NodeList 可能在树中没有位于其上方的 Scaffold。然而,它在小时候就有一个脚手架。
这就是我所做的。
在 NodeList 中创建属性
final _scaffoldKey = GlobalKey<ScaffoldState>();
Run Code Online (Sandbox Code Playgroud)
将其分配给我的 Scaffold 的关键属性。
Scaffold(
key: _scaffoldKey,
Run Code Online (Sandbox Code Playgroud)
然后像这样得到脚手架......
_scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text("Select player"),
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1285 次 |
| 最近记录: |