首页 > 解决方案 > 参数类型“Widget”不能分配给参数类型“PreferredSizeWidget?”

问题描述

一旦我尝试将 appBar 添加到提要页面,它就会引发错误。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wecare_u/constants/Constantcolors.dart';

class FeedHelpers with ChangeNotifier {
ConstantColors constantColors = ConstantColors();
Widget appBar(BuildContext context) {
return AppBar(
  backgroundColor: constantColors.darkColor,
  centerTitle: true,
  actions: [
    IconButton(
        icon: Icon(Icons.camera_enhance_rounded,
            color: constantColors.greenColor),
        onPressed: () {})
  ],
  title: RichText(
    text: TextSpan(
        text: 'Social ',
        style: TextStyle(
          color: constantColors.whiteColor,
          fontWeight: FontWeight.bold,
          fontSize: 20.0,
        ),
        children: <TextSpan>[
          TextSpan(
              text: 'Feed',
              style: TextStyle(
                color: constantColors.blueColor,
                fontWeight: FontWeight.bold,
                fontSize: 20.0,
              ))
        ]),
  ),
  );
  }
  }

以上是我的 Feed_helpers.dart 文件,其中包含页面的代​​码。

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wecare_u/Designs/Feed_helpers.dart';

class Feed extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: Provider.of<FeedHelpers>(context, listen: false).appBar(context),
  );}}

这是我的 feed.dart 文件,当我尝试在此页面中添加 appBar 时,它会引发错误,因为“参数类型 'Widget' 无法分配给参数类型 'PreferredSizeWidget?'。”

标签: flutterdartappbar

解决方案


尝试使用 PreferredSize Widget 包装 AppBar 小部件。

PreferredSize(
  preferredSize: Size.fromHeight(20.0), // height of appbar
  child:  AppBar(
             ..........
          ),
  ),

推荐阅读