flutter - 参数类型“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?'。”
解决方案
尝试使用 PreferredSize Widget 包装 AppBar 小部件。
PreferredSize(
preferredSize: Size.fromHeight(20.0), // height of appbar
child: AppBar(
..........
),
),
推荐阅读
- java - Java中的增量会改变它的直接价值吗?
- c++ - 长度转换程序无法在 C++ 中转换十进制数
- php - XAMPP WordPress PHP VSCode Xdebug 没有本地人
- docker - 从 Dockerfile 访问 `docker run` 参数
- javascript - 在greasemonkey脚本中打印“事件”对象会静默终止执行,如何查看“事件”?
- android - 意图和配置正确清单
- sql - 如果不为空,则更新列
- laravel - Laravel FileSystem 创建 CloudFront 签名 URL
- gdb - 尝试使用 ROP 进行利用时 gdb 和直接执行的不同行为
- react-native - window.open 弹出窗口在本机 webview 中不起作用