无法将AppBarDesign分配给参数类型“ PreferredSizeWidget”
问题内容:
任何人都请提供一些信息为什么会这样?
当我尝试添加实现 appBar* flutter的 AppBarDesign 类 时 ,出现以下错误。 *
错误:参数类型’AppBarDesign’无法分配给参数类型’PreferredSizeWidget’。(位于[flutterbyrajath] lib
\ main.dart:27处的argument_type_not_assignable)
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的类作为appbar
将您的自定义应用栏包装到PreferredSize中
Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(100),
child: Container(color: Colors.red),
),
)
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);
}
}