Flutter AppBarDesign可以';不能分配给参数类型';首选尺寸边长';

Flutter AppBarDesign可以';不能分配给参数类型';首选尺寸边长';,flutter,dart,Flutter,Dart,任何人请提供一些信息,为什么会发生这种情况 当我尝试添加一个类AppBarDesign时,它实现了appBar,颤振给出了以下错误 错误:无法将参数类型“AppBarDesign”分配给参数类型“PreferredSizeWidget”。(参数类型不可在[ByRajath]lib\main.dart:27处赋值) Scaffold需要一个实现 将自定义appbar包装到 脚手架( appBar:首选大小( 首选大小:常量大小。从高度(100), 子:容器(颜色:Colors.red), ), )

任何人请提供一些信息,为什么会发生这种情况

当我尝试添加一个类AppBarDesign时,它实现了appBar,颤振给出了以下错误

错误:无法将参数类型“AppBarDesign”分配给参数类型“PreferredSizeWidget”。(参数类型不可在[ByRajath]lib\main.dart:27处赋值)


Scaffold需要一个实现

将自定义appbar包装到

脚手架(
appBar:首选大小(
首选大小:常量大小。从高度(100),
子:容器(颜色:Colors.red),
),
)
或实施:

脚手架(
appBar:MyAppBar(),
)
...
类MyAppBar扩展无状态小部件实现PreferredSizeWidget{
@凌驾
Size get preferredSize=>const Size.fromHeight(100);
@凌驾
小部件构建(构建上下文){
返回容器(颜色:Colors.red);
}
}

在不搜索任何其他主题的情况下实现该功能的有用提示:

class ApplicationToolbar extends StatelessWidget with PreferredSizeWidget{
  @override
  Widget build(BuildContext context) {
    return AppBar( ... );
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}

您还可以使用以下方法在单独的文件中设计appbar,然后在整个应用程序中使用它

Widget Custom_Appbar(){
  return AppBar(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(bottom: Radius.circular(20))),
        title: Text(
          'Decimal to Binary & Vice Versa',
          style: TextStyle(fontWeight: FontWeight.w400, fontSize: 19),
        ));
}
然后像这样使用它

return Scaffold(
      appBar: Custom_Appbar(),
)
如果你得到了错误

无法将参数类型x指定给参数类型 “首选大小边条”

只需在PreferredSize小部件中包装x

例如:

appBar: AppBar(
    bottom: Column(
              children: <Widget>[
                new TabBar(
                  tabs: [
                    new Tab(icon: new Icon(Icons.directions_car)),
                    new Tab(icon: new Icon(Icons.directions_transit)),
                    new Tab(
                      icon: new Icon(Icons.airplanemode_active),
                    )
                  ],
                ),
              ],
            ),
appBar:appBar(
底部:立柱(
儿童:[
新选项卡(
选项卡:[
新建选项卡(图标:新建图标(图标.方向\汽车)),
新建选项卡(图标:新建图标(图标.方向))),
新标签(
图标:新图标(图标。airplanemode_激活),
)
],
),
],
),
这里我得到了一个错误:参数类型“Column”无法分配给参数类型“PreferredSizeWidget”

解决方案:

点击专栏

点击灯泡

选择wrappwithwidget

用PreferredSize替换小部件

添加PreferredSize属性,例如PreferredSize:Size.fromHeight(100.0)

结果:

appBar: AppBar(
     bottom: PreferredSize(
              preferredSize: Size.fromHeight(100.0),
              child: Column(
                children: <Widget>[
                  new TabBar(
                    tabs: [
                      new Tab(icon: new Icon(Icons.directions_car)),
                      new Tab(icon: new Icon(Icons.directions_transit)),
                      new Tab(
                        icon: new Icon(Icons.airplanemode_active),
                      )
                    ],
                  ),
                ],
              ),
            ),
appBar:appBar(
底部:首选尺寸(
首选尺寸:尺寸。从高度(100.0),
子:列(
儿童:[
新选项卡(
选项卡:[
新建选项卡(图标:新建图标(图标.方向\汽车)),
新建选项卡(图标:新建图标(图标.方向))),
新标签(
图标:新图标(图标。airplanemode_激活),
)
],
),
],
),
),

屏幕截图:


创建此类:

class CustomAppBar扩展了PreferredSize{
@凌驾
最后一个孩子;
最终双倍高度;
自定义应用程序栏({
需要这个高度,
需要这个孩子,
}):super(child:child,首选大小:Size.fromHeight(height));
@凌驾
小部件构建(BuildContext上下文)=>child;
}
用法:

appBar:CustomAppBar(
身高:100,
子:列(
儿童:[
文本('0'),
文本(“1”),
文本('2'),
文本(“3”),
],
),
)

当我添加appBar:appBar()时,它工作正常。它只在我向其中添加类时才会抛出错误。with和implements都可以使用
类应用程序ToolBar扩展无状态Widget implements PreferredSizeWidget{…}
从何处获得
首选大小属性值?@NimishDavidMathew由此:
大小。fromHeight(kToolbarHeight)
这是正确的答案。请参阅下一个答案以获得“真实”答案。自从他们添加了
空安全性以来,这引发了各种各样的错误!:(@Anoop.P.A将代码更新为空安全。
appBar: AppBar(
     bottom: PreferredSize(
              preferredSize: Size.fromHeight(100.0),
              child: Column(
                children: <Widget>[
                  new TabBar(
                    tabs: [
                      new Tab(icon: new Icon(Icons.directions_car)),
                      new Tab(icon: new Icon(Icons.directions_transit)),
                      new Tab(
                        icon: new Icon(Icons.airplanemode_active),
                      )
                    ],
                  ),
                ],
              ),
            ),