我有一个class InfoScreen
扩展了的BaseInfoScreen
。在其中BaseInfoScreen
有一个Widget body();
,所有子项都可以覆盖。还有一个在Widget banner();
中实现的BaseInfoScreen
,并且我继承了小部件以使用它,但不能覆盖它(因为它在所有屏幕上都必须相同)。有什么办法可以告诉我Widget banner();
不要被压倒吗?我尝试将final
和添加const
到函数中,但它不允许我这样做。
完整代码示例:
BaseInfoScreen
abstract class BaseInfoScreen extends StatefulWidget {
final String title;
BaseInfoScreen({
Key key,
this.title,
}) : super(key: key);
}
abstract class BaseInfoScreenState<Page extends BaseInfoScreen>
extends State<BaseInfoScreen> {
bool showFabMenu() => true;
}
mixin BaseScreen<Page extends BaseInfoScreen> on BaseInfoScreenState<Page> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: CoolAppBar(
title: widget.title,
),
drawer: Container(
width: 290,
child: Drawer(
child: DrawerMenu(),
),
),
body: body(),
floatingActionButton: showFabMenu() ? FabMenu() : null,
);
}
//This should be overridable
Widget body();
//This should be usable by widgets that inherit from BaseInfoScreen, but not override it.
Widget banner() {
return Container(
color: Colors.black12,
padding: const EdgeInsets.all(20),
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
widget.bannerIconAsset != null
? Container(
child: Image.asset(
widget.bannerIconAsset,
width: 36,
height: 36,
filterQuality: FilterQuality.high,
color: Colors.white,
),
)
: Container(
child: Icon(
widget.bannerIconData,
size: 36,
color: Colors.white,
),
),
SizedBox(width: 20),
Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.7),
child: FittedBox(
fit: widget.bannerTitle.length > 10
? BoxFit.fitWidth
: BoxFit.none,
child: Text(
widget.bannerTitle,
style: TextStyle(
fontSize: 28,
fontFamily: 'Raleway',
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
],
),
);
}
信息屏幕
class InfoScreen extends BaseInfoScreen {
final String title;
InfoScreen({
this.title,
}) : super(
title: title,
);
@override
_InfoScreenState createState() =>
_InfoScreenState();
}
class _InfoScreenState extends BaseInfoScreenState<InfoScreen> with BaseScreen
{
_InfoScreenState();
@override
Widget body() {
return Column(
children: [
//Here I call banner()
banner(),
//Here I declare other widgets that are part of the body
],
);
}
}
我试图尽可能地简化代码,以使其保持简单。也许答案只是“不,不可能”,但我在网上找不到任何东西。
您可以nonVirtual
通过meta包的注释来做到这一点:
import 'package:meta/meta.dart';
void main() {
B().p();
}
class A {
@nonVirtual
void p() => print('A');
}
class B extends A {
void p() => print('B');
}
默认情况下,invalid_override_of_non_virtual_member
这只是一个信息。您可能想通过修改analysis_options.yaml
项目根目录下的文件来强力实施它:
analyzer:
errors:
invalid_override_of_non_virtual_member: error
请点击这里了解有关自定义飞镖静态分析的详细信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句