gpt4 book ai didi

Flutter Provider - 重建列表项而不是 ListView

转载 作者:行者123 更新时间:2023-12-02 16:45:53 28 4
gpt4 key购买 nike

我正在使用 Provider 包来管理我的应用程序业务逻辑,但我遇到了一个问题,即正在重建整个 ListView 而不是单个 ListTile。下面的用户界面可以让您更好地理解:

enter image description here

目前,如果我滚动到列表底部,点击最后一项的复选框,我看不到复选框切换的动画,并且滚动会跳转到屏幕顶部,因为整个小部件已重建。 如何使用 Provider 以便仅重建单个 ListTile 而不是列表中的每个项目?以下是一些相关代码:


class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Checklist',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Colors.indigo[500],
accentColor: Colors.amber[500],
),
home: ChecklistHomeScreen(),
),
providers: [
ChangeNotifierProvider(
create: (ctx) => ChecklistsProvider(),
),
],
);
}
}
class ChecklistHomeScreen extends StatefulWidget {
@override
_ChecklistHomeScreenState createState() => _ChecklistHomeScreenState();
}

class _ChecklistHomeScreenState extends State<ChecklistHomeScreen> {
void createList(BuildContext context, String listName) {
if (listName.isNotEmpty) {
Provider.of<ChecklistsProvider>(context).addChecklist(listName);
}
}

@override
Widget build(BuildContext context) {
final _checklists = Provider.of<ChecklistsProvider>(context).checklists;
final _scaffoldKey = GlobalKey<ScaffoldState>();
ScrollController _scrollController =
PrimaryScrollController.of(context) ?? ScrollController();

return Scaffold(
key: _scaffoldKey,
body: CustomScrollView(
controller: _scrollController,
slivers: <Widget>[
SliverAppBar(
floating: true,
pinned: false,
title: Text('Your Lists'),
centerTitle: true,
actions: <Widget>[
PopupMenuButton(
itemBuilder: (ctx) => null,
),
],
),
ReorderableSliverList(
delegate: ReorderableSliverChildBuilderDelegate(
(ctx, i) => _buildListItem(_checklists[i], i),
childCount: _checklists.length,
),
onReorder: (int oldIndex, int newIndex) {
setState(() {
final checklist = _checklists.removeAt(oldIndex);
_checklists.insert(newIndex, checklist);
});
},
),
],
),
drawer: Drawer(
child: null,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: null,
),
);
}

Widget _buildListItem(Checklist list, int listIndex) {
return Dismissible(
key: ObjectKey(list.id),
direction: DismissDirection.endToStart,
background: Card(
elevation: 0,
child: Container(
alignment: AlignmentDirectional.centerEnd,
color: Theme.of(context).accentColor,
child: Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
child: Icon(
Icons.delete,
color: Colors.white,
),
),
),
),
child: Card(
child: ListTile(
onTap: null,
title: Text(list.name),
leading: Checkbox(
value: list.completed,
onChanged: (value) {
Provider.of<ChecklistsProvider>(context)
.toggleCompletedStatus(list.id, list.completed);
},
),
trailing: IconButton(
icon: Icon(Icons.more_vert),
onPressed: null,
),
),
),
onDismissed: (direction) {
_onDeleteList(list, listIndex);
},
);
}

void _onDeleteList(Checklist list, int listIndex) {
Scaffold.of(context).removeCurrentSnackBar();
Scaffold.of(context).showSnackBar(
SnackBar(
action: SnackBarAction(
label: 'UNDO',
onPressed: () {
Provider.of<ChecklistsProvider>(context)
.undoDeleteChecklist(list, listIndex);
},
),
content: Text(
'List deleted',
style: TextStyle(color: Theme.of(context).accentColor),
),
),
);
}
}
class ChecklistsProvider with ChangeNotifier {
final ChecklistRepository _repository = ChecklistRepository(); //singleton

UnmodifiableListView<Checklist> get checklists => UnmodifiableListView(_repository.getChecklists());

void addChecklist(String name) {
_repository.addChecklist(name);
notifyListeners();
}

void deleteChecklist(int id) {
_repository.deleteChecklist(id);
notifyListeners();
}

void toggleCompletedStatus(int id, bool completed) {
final list = checklists.firstWhere((c) => c.id == id);
if(list != null) {
list.completed = completed;
_repository.updateChecklist(list);
notifyListeners();
}
}
}

我应该说我理解为什么这是当前的行为,我只是不确定确保仅重建我想要更新的列表项而不是整个屏幕的正确方法。我还阅读了有关 Consumer 的内容,但我不确定如何将其融入我的实现中。

最佳答案

消费者本质上允许您使用对更改通知程序所做的任何更改。最佳实践是将 Consumer 尽可能深入地嵌入到您的构建方法中。这样,只有包装的小部件才会被重新构建。这篇文档解释得很好:https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple

尝试将您的 CheckBox 小部件包装在 Consumer 小部件中。仅应重建复选框。

Consumer<ChecklistsProvider>(
builder: (context, provider, _) {
return Checkbox(
value: list.completed,
onChanged: (value) {
provider.toggleCompletedStatus(list.id, list.completed);
},
);
},
),

如果您希望重新构建 ListTile 和 CheckBox,只需将 ListTile 包装在 Consumer 中即可

关于Flutter Provider - 重建列表项而不是 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59215357/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com