flutter - 从列表视图构建器中删除某些内容后如何刷新它?
问题描述
我已经设置了一个列表视图构建器,它显示日记帐分录日期,您可以删除它们,但每次我删除它们时,我都必须返回并再次打开页面以刷新它。当我删除一个项目时,有没有办法自动刷新它?
class ListOfEntries extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Your entries"),
toolbarHeight: 50,
),
body: FutureBuilder<List>(
future: Database_().entries(),
builder: (context, snapshot) {
return snapshot.hasData ?
new ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
padding: const EdgeInsets.all(10.0),
itemCount: snapshot.data.length,
itemBuilder: (context, i) {
return Container(
decoration: BoxDecoration(
color: Colors.grey[900],
boxShadow: [BoxShadow(
color: Colors.grey,
blurRadius: 3.0,
spreadRadius: 2,
)],
borderRadius: BorderRadius.only(
topLeft: Radius.zero,
topRight: Radius.zero,
bottomLeft: Radius.circular(40),
bottomRight: Radius.circular(40)
)
),
child: Padding(
padding: EdgeInsets.all(30),
child: Center(
child: Column(
children: [
Container(
child: Padding(
child: Text(
"${snapshot.data[snapshot.data.length - i - 1].date}",
style: TextStyle(
fontSize: 17
),
),
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
),
decoration: BoxDecoration(
color: Colors.purple[900],
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
),
Text(""),
Text(
"${snapshot.data[snapshot.data.length - i - 1].entry}",
style: TextStyle(fontSize: 23),
),
Text(""),
TextButton(
onPressed: ()=>{
Delete(snapshot.data[snapshot.data.length - i - 1].entry),
},
child: Text("Delete")
)
],
),
),
),
);
},
)
: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
Text(""),
Text("No entries yet:("),
],
)
);
}
),
);
}
}
Delete(String entry_)async{
await Database_().deleteEntry(entry_);
}
我正在使用可能并不理想的无状态小部件。
解决方案
将其更改为StatefulWidget
并使用setState
函数更新您的列表
推荐阅读
- java - AssetFile 描述符不适用于 SetDataSource
- c# - 当我们将默认浏览器设置为 chrome(IE 除外)时,无法从 Windows 应用程序打印 html 文件
- r - R中的绑定环境和函数范围
- c# - 当我尝试更新实体时,EF6 引发 EntityValidationErrors 异常
- python-3.x - 将字符串变量传递给 glob
- sql - 枚举参数不会从数据库返回值
- php - 节省到我在 laravel 中的数据库
- c# - 如何将 ConnectionString 从 appsettings.json 文件传递到 dot net core 2.2 中的类库项目
- javascript - 如何在满足条件之前禁用按钮/选项卡
- javascript - 如何从 MVC 中的模态弹出窗口调用 .js