【问题标题】:Stateless widget and Widget function difference无状态小部件和小部件功能区别
【发布时间】:2021-05-03 08:39:50
【问题描述】:

我已经阅读了无状态小部件和返回小部件的函数之间的区别,并且我知道框架可以识别类但不能识别函数。在下面的代码中,我有一个浮动按钮,我在其中调用 setState() 并且在这两种情况下 appbar 都会重建(无状态小部件和函数),所以在这种情况下,这两个有什么不同吗?

      appBar: 
       AppBarv1(title: widget.title,)
      // customAppBar(title: widget.title)
       ,
       floatingActionButton: FloatingActionButton(backgroundColor: Colors.blue,onPressed: (){
         setState(() {
                    
                  });
       },),
      body: 
      Center(
      ),
      
    );

PreferredSizeWidget customAppBar({String title}) {
  print('appbar is built');
  return AppBar(
    title: Text(title),
    actions: [],
  );
}

class AppBarv1 extends PreferredSize {

  const AppBarv1({this.title});
  final String title;

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    print('appbar is built');
    return AppBar(
    title: Text(title),
    actions: [],
  );
  }
}

提前致谢!

【问题讨论】:

    标签: function flutter widget


    【解决方案1】:

    它们没有什么不同,只是函数在 Widget 返回之前执行。您最终得到的 Widget-Tree 是相同的。

    【讨论】:

      猜你喜欢
      • 2020-01-17
      • 2021-12-15
      • 2018-02-06
      • 2021-06-11
      • 2018-09-01
      • 2020-10-19
      • 2021-09-21
      • 2019-10-06
      • 1970-01-01
      相关资源
      最近更新 更多