Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

flutter - What difference between stateless and stateful widgets?

I am learning Dart/flutter and trying to understand how Widgets system works. But I can't understand what difference between stateless and stateful widgets? For example I have button. What type it have?

Let's imagine two cases. I send text to button and it's display it. I send text to button and it's change color.

What will be if I will create not proper Widget type?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are 3 kind of widgets, not just 2.

  • Stateful widget
  • Stateless widget
  • Inherited widget

A stateless widget is like a constant. It is immutable. If you want to change what is displayed by a stateless widget, you'll have to create a new one.

Stateful widgets are the opposite. They are alive and can interact with the user. Stateful widgets have access to a method named setState, which basically says to the framework "Hello, I want to display something else. Can you redraw me please ?".

Finally, Inherited widget is a mixt of both worlds. It is immutable and stateless. But another widget (whatever it is) can subscribe to that inherited widget. Which means that when you replace your inherited widget by a new one, all the widgets that has subscribed to the old one will be redrawn.

In the end, a stateful widget will usually be used like a Controller. A stateless widget will be used like a View. And the inherited widget will be your configuration file or your Model.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...