【问题标题】:The argument type 'Widget' can't be assigned to the parameter type 'PreferredSizeWidget?'参数类型“Widget”不能分配给参数类型“PreferredSizeWidget?”
【发布时间】:2021-08-23 16:17:04
【问题描述】:
class DetailChatPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget header() {
      return PreferredSize(
        preferredSize: Size.fromHeight(70),
        child: AppBar(
          backgroundColor: backgroundColor1,

        ),
      );
    }

    return Scaffold(
      backgroundColor: backgroundColor3,
      appBar: header(),
    );
  }
}

我的代码中有一个错误是 参数类型“Widget”不能设置为参数类型“PreferredSizeWidget?” 如何解决这个问题

【问题讨论】:

    标签: flutter widget flutter-layout


    【解决方案1】:

    发生这种情况是因为您使用 Widget 类型声明了您的 header 方法。您应该改为使用 PreferredSizeWidget 类型声明它。

    PreferredSizeWidget header() {
      return PreferredSize(
        preferredSize: Size.fromHeight(70),
        child: AppBar(
          backgroundColor: backgroundColor1,
        ),
      );
    }
    

    【讨论】:

      【解决方案2】:

      改变

      Widget header() {
        return PreferredSize(
          preferredSize: Size.fromHeight(70),
          child: AppBar(
            backgroundColor: backgroundColor1,
      
          ),
        );
      }
      

      PreferredSize header() {
        return PreferredSize(
          preferredSize: Size.fromHeight(70),
          child: AppBar(
            backgroundColor: backgroundColor1,
      
          ),
        );
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-11
        • 1970-01-01
        • 2021-12-13
        • 2019-12-06
        • 2020-01-10
        • 2021-08-05
        • 2021-10-04
        • 2021-09-13
        • 2021-08-22
        相关资源
        最近更新 更多