簡體   English   中英

Flutter:小部件不是“ObstructingPreferredSizeWidget”類型的子類型

[英]Flutter: Widget is not a subtype of type 'ObstructingPreferredSizeWidget'

我正在嘗試將我的 AppBar 存儲在一個變量中以使用多個位置

我的 main.dart 文件包含 -

final PreferredSizeWidget appBar = NavigationAppBar(_actionCall)

並且 navigation_app_bar 包含 -

class NavigationAppBar extends StatelessWidget with PreferredSizeWidget {
  final Function actionCall;

  NavigationAppBar(this.actionCall);

  @override
  Widget build(BuildContext context) {
    return Platform.isIOS ? 
    CupertinoNavigationBar(
      middle: Text(
        'ABC'
      ),
      trailing: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
            GestureDetector(
              child: Icon(CupertinoIcons.add),
              onTap: () => actionCall,
            )
        ],
      ),
    ) : 
    AppBar(
      title: Text('ABC'),
      centerTitle: false,
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.add),
          onPressed: () => actionCall,
        )
      ],
    );
  }

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

但是當嘗試運行我的 flutter 應用程序時出現以下錯誤。

type 'NavigationAppBar' is not a subtype of type 'ObstructingPreferredSizeWidget'

我該如何解決這個問題? 任何幫助表示贊賞。

您是否嘗試在代碼中的所有適用位置將 NavigationAppBar 替換為 PreferredSizeWidget? 這對我有幫助。

我的問題有一點不同,因為我必須用 PreferredSizeWidget 替換 AppBar(不是 NavigationAppBar)。

這可能與最新的 Flutter 版本的一些變化有關。

暫無
暫無

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

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