# 21.Widget的生命周期

### StatelessWidget

* 构造方法
* Build方法

### StatefulWidget

* Widget 的构造方法
* Widget 的 createState 方法
* State 的构造方法
* State 的 initState 方法
* State 的 didChangeDependencies 方法
  * 依赖的 InheritedWidget （共享数据） 发生变化后 didChangeDependencies 才会调用
* State 的 build 方法
  * 当调用 setState ，会重新调用 Build 进行渲染
    * setState 内部主要是利用 \_element 调用 markNeedsBuild
* Widget 销毁的时候，调用 State 的 dispose 方法


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/flutter/21.widget-de-sheng-ming-zhou-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
