What is the difference between functions and classes to create reusable widgets?

后端 未结 5 2127
野的像风
野的像风 2020-11-22 10:20

I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this:

Widget functio         


        
5条回答
  •  鱼传尺愫
    2020-11-22 11:07

    Widgets returned by functions are rebuilt every time the widget tree is rebuilt, whether they contain a state or not.

    However, stateless or stateful widgets will only be rebuilt (just them) in that widget tree if the state they contain changes.

    It is advised to extract the widgets to their separate classes to improve the performance of your app. Minimize how many widgets are rebuilt...

提交回复
热议问题