一尘不染

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

flutter

任何人都请提供一些信息,为什么会这样?

当我尝试添加实现appBar flutter 的类AppBarDesign时, 出现以下错误。

错误:参数类型’AppBarDesign’无法分配给参数
类型’PreferredSizeWidget’。(argument_type_not_assignable位于
[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),
        );
      }
    }

阅读 506

收藏
2020-08-13

共1个答案

一尘不染

支架需要将实现
PreferredSizeWidget的类作为appbar

将您的自定义应用栏包装到
PreferredSize

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

or implement
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);
  }
}
2020-08-13