flutter - Flutter:futurebuilder => item => dialog,按子项删除后刷新视图
问题描述
嗨,我做了一个未来构建器,它在收到一些日期后构建我一个包含一些项目的列表视图。
FutureBuilder(
future: getAllInstructors(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<Instructor> instructors = snapshot.data;
return Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
instructors.length.toString() + " Instructors",
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
),
const SizedBox(
height: 20,
),
Expanded(
child: ListView.separated(
itemBuilder: (context, int index) =>
InstructorItem(
instructor: instructors[index],
),
separatorBuilder: (context, index) =>
const Divider(
color: Colors.grey,
),
itemCount: instructors.length,
),
),
],
),
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
所以我现在的问题是,如果我滑动项目以调用它的功能delete()
但页面不刷新
class InstructorItem extends StatelessWidget {
const InstructorItem({
Key? key,
required this.instructor,
}) : super(key: key);
final Instructor instructor;
@override
Widget build(BuildContext context) {
return Slidable(
actionPane: const SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
child: ClipRRect(
borderRadius: BorderRadius.circular(45.0),
child: Image(
height: 35,
width: 35,
fit: BoxFit.cover,
image: NetworkImage(instructor.image),
),
),
foregroundColor: Colors.white,
),
title: Text(
instructor.getFullName(),
style: const TextStyle(
fontWeight: FontWeight.w600,
),
),
),
secondaryActions: <Widget>[
IconSlideAction(
color: Colors.transparent,
iconWidget: Container(
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: const Color(0xffFFECEC),
borderRadius: BorderRadius.circular(15),
),
child: const Icon(
Icons.delete,
color: Colors.red,
size: 30,
),
),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return const CustomDialogBox(
title: "Warning",
descriptions:
"Are you sure you want to delete this instructor?",
type: DialogType.warning,
);
},
).then((value){
if(value) delete(instructor);
});
},
),
],
);
}
}
所以我的问题是:如果对话框中的值是真的,我该如何正确编码它会删除讲师并刷新视图?
解决方案
我认为这是因为您在删除讲师模型时使用了不同的上下文。尝试这个:
如果要使用StatelessWidget
,请创建一个可在类外使用的函数回调。
class InstructorItem extends StatelessWidget {
final Function onDelete;
const InstructorItem({
Key? key,
required this.instructor,
}) : super(key: key);
final Instructor instructor;
@override
Widget build(BuildContext context) {
return Slidable(
actionPane: const SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: ListTile(
leading: CircleAvatar(
backgroundColor: Colors.white,
child: ClipRRect(
borderRadius: BorderRadius.circular(45.0),
child: Image(
height: 35,
width: 35,
fit: BoxFit.cover,
image: NetworkImage(instructor.image),
),
),
foregroundColor: Colors.white,
),
title: Text(
instructor.getFullName(),
style: const TextStyle(
fontWeight: FontWeight.w600,
),
),
),
secondaryActions: <Widget>[
IconSlideAction(
color: Colors.transparent,
iconWidget: Container(
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
color: const Color(0xffFFECEC),
borderRadius: BorderRadius.circular(15),
),
child: const Icon(
Icons.delete,
color: Colors.red,
size: 30,
),
),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return const CustomDialogBox(
title: "Warning",
descriptions:
"Are you sure you want to delete this instructor?",
type: DialogType.warning,
);
},
).then((value) {
if (value) onDelete;
});
},
),
],
);
}
}
在具有 FutureBuilder 的类中调用它。
FutureBuilder(
future: getAllInstructors(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<Instructor> instructors = snapshot.data;
return Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
instructors.length.toString() + " Instructors",
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
),
const SizedBox(
height: 20,
),
Expanded(
child: ListView.separated(
itemBuilder: (context, int index) =>
InstructorItem(
instructor: instructors[index],
onDelete: setState(() => onDelete(instructors[index])),
),
separatorBuilder: (context, index) =>
const Divider(
color: Colors.grey,
),
itemCount: instructors.length,
),
),
],
),
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
)
推荐阅读
- python - 图片更新后显示空白
- java - 找不到要发送 Tab 键的 HTML 元素
- ios - WatchOS 应用程序未检测到配套的 iOS 应用程序
- user-interface - Roblox Gui 仅在第一次单击时打开/关闭
- github - 当我在推送到所有常规存储库时尝试推送到 gist.github.com 时,是什么导致了这个错误?
- nginx - nginx - 禁用通过 IP 访问
- python - 如何在pygame中移动时射击
- xaml - 无法从 XAML 中的变量传递数据 - Xamarin Forms, map app
- bash - 将字符串添加到 Bash 中变量的每个成员
- asp.net-core-3.0 - asp.net core 3 rotativa startup.cs 环境问题