flutter 没有为类型“Widget”定义getter“preferredSize”

6ojccjat  于 2023-02-13  发布在  Flutter
关注(0)|答案(5)|浏览(193)

@override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final isLandscape = mediaQuery.orientation == Orientation.landscape;
    final appBar = Platform.isIOS
        ? CupertinoNavigationBar(
            middle: Text(
              'Personal Expenses',
            ),
            trailing: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                GestureDetector(
                  child: Icon(CupertinoIcons.add),
                  onTap: () => _startAddNewTransaction(context),
                ),
              ],
            ),
          )
        : AppBar(
            title: Text(
              'Personal Expenses',
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.add),
                onPressed: () => _startAddNewTransaction(context),
              ),
            ],
          );

没有为类型“Widget”定义getter“preferredSize”。

final txListWidget = Container(
      height: (mediaQuery.size.height -
              appBar.preferredSize.height -
              mediaQuery.padding.top) *
          0.7,

参数类型“Widget”不能分配给参数类型“ObstructingPreferredSizeWidget?”

return Platform.isIOS
    ? CupertinoPageScaffold(
        child: pageBody,
        navigationBar: appBar,
      )

参数类型“Widget”无法分配给参数类型“PreferredSizeWidget?”

: Scaffold(
        appBar: appBar,
        body: pageBody,
igsr9ssn

igsr9ssn1#

将代码更改为以下格式。

final PreferredSizeWidget appBar = (Platform.isIOS ? CupertinoNavigationBar(...) : AppBar(...)) as PreferredSizeWidget;
7qhs6swi

7qhs6swi2#

请使用变量appBar的数据类型

final PreferredSizeWidge appBar = Platform.isIOS ? ......
gwbalxhn

gwbalxhn3#

appbar类型从PreferredSizeWidgetWidget更改为dynamic

slhcrj9b

slhcrj9b4#

将appbar的类型添加为dynamic。出现此错误是因为dart无法理解为cupertionoappwidget和appbar都定义了preferredsize类。
解决方案:最终动态appbar

5jdjgkvh

5jdjgkvh5#

您的错误的完整解决方案

final appBar = (Platform.isIOS ? CupertinoNavigationBar(...) : AppBar(...));

final txListWidget = Container(
        height: (mediaQuery.size.height -
                (appBar as PreferredSizeWidget).preferredSize.height -
                mediaQuery.padding.top) *
            0.77,
        child: TransactionList(_userTransactions, _deleteTransaction));

return Platform.isIOS
        ? CupertinoPageScaffold(
            navigationBar: appBar as ObstructingPreferredSizeWidget,
            child: pageBody,
          )
        : Scaffold(
            appBar: appBar,
            body: SingleChildScrollView(child: pageBody), .....

相关问题