无法将AppBarDesign分配给参数类型'PreferredSizeWidget' [英] The AppBarDesign can't be assigned to the parameter type 'PreferredSizeWidget'

查看:706
本文介绍了无法将AppBarDesign分配给参数类型'PreferredSizeWidget'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都请提供一些为什么会发生这种情况?

Anyone please give some information why this is happening?

当我尝试添加实现 appBar flutter出现以下错误。

When I try to add a class AppBarDesign which implements appBar flutter is giving the below error.


错误:参数类型'AppBarDesign'无法分配给参数类型' PreferredSizeWidget。 (在[flutterbyrajath] lib\main.dart:27处为argument_type_not_assignable)

error: The argument type 'AppBarDesign' can't be assigned to the parameter type 'PreferredSizeWidget'. (argument_type_not_assignable at [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),
        );
      }
    }


推荐答案

脚手架需要一个作为应用栏的实现 PreferredSizeWidget

Scaffold requires as appbar a class that implements PreferredSizeWidget

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

Either wrap your custom appbar into a 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);
  }
}

这篇关于无法将AppBarDesign分配给参数类型'PreferredSizeWidget'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆