状态 (State) 管理参考
状态管理是一个相当复杂的话题。如果你在浏览后发现一些问题并未得到解答,或者并不适用于你的具体需求场景,自信些,你的实现就是对的。
通过下面的资源了解更多的信息,其中有很多内容都是由社区(第三方)提供。
总体概览
#在选择一个具体内容前,你可以先查看以下几项。
-
状态管理的介绍。这是本篇内容的总起。(当你直接跳转到了该页面,但错过了其他页面时,可以先从这里开始阅读)
-
Flutter 实用状态管理 (Pragmatic State Management in Flutter),来自 Google I/O 2019 的介绍视频。
-
Flutter 架构实例 (Flutter Architecture Samples), Brian Egan 著。
内置的方式
#setState
#用于 Widget 临时状态的基础方式。
-
为你的 Flutter 应用添加交互 (Adding interactivity to your Flutter app),一篇 Flutter 的教程。
-
Flutter 中的基础状态管理 (Basic state management in Google Flutter), Agung Surya 著。
ValueNotifier
和 InheritedNotifier
#
一种完全使用 Flutter 提供的 API 来更新状态并通知 UI 更新的方式。
- State Management using ValueNotifier and InheritedNotifier, by Tadas Petra
InheritedWidget
和 InheritedModel
#
Widget tree 中不同层级间的 widget 通信的基础方法。这是诸如 package:provider
等众多方法的底层实现。
以下讲师指导的视频 workshop 介绍了如何使用 InheritedWidget
:
其他有用的文档包括:
-
使用 InheritedWidgets 管理 Flutter 应用状态 (Managing Flutter Application State With InheritedWidgets), Hans Muller 著。
-
继承 Widgets (Inheriting Widgets), Mehmet Fidanboyly 著。
-
高效地使用 Flutter 继承 Widgets (Using Flutter Inherited Widgets Effectively),Eric Windmill 著。
-
Widget - State - Context - InheritedWidget,Didier Bolelens 著。
社区提供的 package
#Depending on the complexity of your app and preferences of your team, you might find adopting a state management package useful. State management packages often help reduce boilerplate code, provide specialized debugging tools, and can help enable a clearer and consistent application architecture.
The Flutter community offers a wide variety of state management packages. The best choice for your app often depends on the app's complexity, your team's preferences, and the specific problems you need to solve.
To begin exploring the available options,
check out the #state-management
topic on the pub.dev site and
refine the search to find packages that match your needs.
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2025-09-02。 查看文档源码 或者 为本页面内容提出建议.