dart 无法将AppBarDesign分配给参数类型“PreferredSizeWidget”

ldxq2e6h  于 2022-12-16  发布在  其他
关注(0)|答案(6)|浏览(100)

任何人请给予一些信息,为什么会发生这种情况?
当我尝试添加实现appBar的类AppBarDesign时,flutter给出以下错误。
错误:参数类型“AppBarDesign”无法分配给参数类型“PreferredSizeWidget”。([flutterbyrajath] lib\main.dart处的参数类型不可分配:27)

import 'package:flutter/material.dart';

    main() {
      runApp(new MyApp());
    }

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Rajath\'s design ',
          debugShowCheckedModeBanner: false,
          theme: new ThemeData(primarySwatch: Colors.amber),
          home: new MyHomePage(key, 'App is Fun'),
        );
      }
    }

    class MyHomePage extends StatelessWidget {
      MyHomePage(Key key, this.title) : super(key: key);

      final title;

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBarDesign(key, title),
        );
      }
    }

    class AppBarDesign extends StatelessWidget {
      AppBarDesign(Key key, this.title) : super(key: key);

      final title;

      @override
      Widget build(BuildContext context) {
        return new AppBar(
          title: new Text(title),
        );
      }
    }
mftmpeh8

mftmpeh81#

无需搜索任何其他主题即可实现该功能的有用提示:

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

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

roejwanj2#

支架需要实现PreferredSizeWidget的类作为appbar
要么将您的自定义应用工具栏 Package 成PreferredSize

Scaffold(
  appBar: PreferredSize(
    preferredSize: const Size.fromHeight(100),
    child: Container(color: Colors.red),
  ),
)

或实现PreferredSizeWidget

Scaffold(
  appBar: MyAppBar(),
)

...

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Size get preferredSize => const Size.fromHeight(100);

  @override
  Widget build(BuildContext context) {
    return Container(color: Colors.red);
  }
}
sd2nnvve

sd2nnvve3#

截图:

创建此类:

class CustomAppBar extends PreferredSize {
  @override
  final Widget child;
  final double height;

  CustomAppBar({
    required this.height,
    required this.child,
  }) : super(child: child, preferredSize: Size.fromHeight(height));
}

用法:

appBar: CustomAppBar(
  height: 100,
  child: Container(
    color: Colors.red,
    child: Column(
      children: [
        Text('0'),
        Text('1'),
        Text('2'),
        Text('3'),
      ],
    ),
  ),
)
ocebsuys

ocebsuys4#

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

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(),
)
h43kikqp

h43kikqp5#

如果您收到错误
参数类型x不能分配给参数类型“PreferredSizeWidget”。
只需在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),
                    )
                  ],
                ),
              ],
            ),

这里我得到的错误:参数类型“Column”不能分配给参数类型“PreferredSizeWidget”。
解决方案:
单击列
点击灯泡
选择使用小部件换行
将小部件替换为首选大小
添加一个PreferredSize属性,如preferredSize:大小.从高度(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),
                      )
                    ],
                  ),
                ],
              ),
            ),
col17t5w

col17t5w6#

第一:实现您的类

implements PreferredSizeWidget

第二步:输入身高

@override
Size get preferredSize => Size.fromHeight(AppBar().preferredSize.height);

相关问题