如何解决 PreferredSize flutter 中的零参数构造函数错误?
How to solve zero Argument constructor error in PreferredSize flutter?
我正在开发一个项目,在该项目中我使用了自定义 AppBar,因此使用了 PreferredSize
,但我收到一条错误消息:
The superclass 'PreferredSize' doesn't have a zero argument constructor. Try declaring a zero argument constructor in 'PreferredSize', or explicitly invoking a different constructor in 'PreferredSize'.
这是我的部分代码:
class CustomAppBar extends PreferredSize {
final Widget child;
final double height;
CustomAppBar({required this.child, required this.height}); //getting error on this line
@override
Size get preferredSize => Size.fromHeight(height);
@override
Widget build(BuildContext context) {
return Container()
}
}
编辑:
删除 PreferredSize 后出现此错误:
The named parameter 'child' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'child'.
appBar:
PreferredSize(
preferredSize:Size.fromHeight(95),
child:
AppBar(
child: Column(
children: <Widget>[
SizedBox(height: 40),
Row(
children: <Widget>[
Builder(
builder: (context) => FlatButton.icon(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: Icon(Icons.menu),
label: Text('')),
),
SizedBox(width: 60),
Text('Times ',
style: GoogleFonts.blackHanSans(
textStyle:
TextStyle(color: Colors.black, fontSize: 20))),
Image.asset(
'assets/newss.png',
height: 50,
),
],
)
您甚至可能不必扩展 PreferredSize
,您可以将 appBar 或任何其他小部件包装在 PreferredSize
中并指定其高度。
PreferredSize(
preferredSize: Size.fromHeight(72),
child: AppBar(
title: Text(title, style: TextStyles.h1),
centerTitle: false,
elevation: 0,
brightness: Brightness.light,
backwardsCompatibility: false,
bottom: bottomAppBarWidget,
))
我正在开发一个项目,在该项目中我使用了自定义 AppBar,因此使用了 PreferredSize
,但我收到一条错误消息:
The superclass 'PreferredSize' doesn't have a zero argument constructor. Try declaring a zero argument constructor in 'PreferredSize', or explicitly invoking a different constructor in 'PreferredSize'.
这是我的部分代码:
class CustomAppBar extends PreferredSize {
final Widget child;
final double height;
CustomAppBar({required this.child, required this.height}); //getting error on this line
@override
Size get preferredSize => Size.fromHeight(height);
@override
Widget build(BuildContext context) {
return Container()
}
}
编辑: 删除 PreferredSize 后出现此错误:
The named parameter 'child' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'child'.
appBar:
PreferredSize(
preferredSize:Size.fromHeight(95),
child:
AppBar(
child: Column(
children: <Widget>[
SizedBox(height: 40),
Row(
children: <Widget>[
Builder(
builder: (context) => FlatButton.icon(
onPressed: () => Scaffold.of(context).openDrawer(),
icon: Icon(Icons.menu),
label: Text('')),
),
SizedBox(width: 60),
Text('Times ',
style: GoogleFonts.blackHanSans(
textStyle:
TextStyle(color: Colors.black, fontSize: 20))),
Image.asset(
'assets/newss.png',
height: 50,
),
],
)
您甚至可能不必扩展 PreferredSize
,您可以将 appBar 或任何其他小部件包装在 PreferredSize
中并指定其高度。
PreferredSize(
preferredSize: Size.fromHeight(72),
child: AppBar(
title: Text(title, style: TextStyles.h1),
centerTitle: false,
elevation: 0,
brightness: Brightness.light,
backwardsCompatibility: false,
bottom: bottomAppBarWidget,
))