簡體   English   中英

Stateless Widget和Widget function的區別

[英]Stateless widget and Widget function difference

我已經閱讀了無狀態小部件和返回小部件的函數之間的區別,並且我知道框架可以識別類但不能識別函數。 在下面的代碼中,我有一個浮動按鈕,我在其中調用 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 在 Widget 返回之前執行。 您最終得到的 Widget-Tree 是相同的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM