Flutter:继承自抽象的无状态小部件 [英] Flutter: inherit from abstract stateless widget
本文介绍了Flutter:继承自抽象的无状态小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个必须自定义小部件的类.这个可以有两个不同的实现,所以我想有一个抽象类作为接口,并创建另外两个扩展抽象类的类.所以,我有:
I have a class that has to take a custom widget. This one can have two different implementations, so I would like to have an abstract class as interface and create two other classes those extend the abstract one. So, I have:
abstract class ICustomWidget extends StatelessWidget{}
class A extends ICustomWidget{
@override
Widget build(BuildContext context) =>
//Implementation
}
class B extends ICustomWidget {
@override
Widget build(BuildContext context) =>
//Implementation
}
我想问一下这是否是正确的方法,或者还有另一种方法.谢谢
I want to ask if this is the right way to do this or there is another one. Thanks
推荐答案
我会使用 implements
,而不是 extends
,因为 ICustomWidget
是接口,而不是类,除非您可以提供更多的上下文和/或代码示例.
Rather than extends
, I would use implements
, because ICustomWidget
is an interface, not a class, except if you can give more context and/or code sample.
这是界面的示例代码
abstract class ICustomWidget {
// or
// abstract class ICustomWidget extends StatelessWidget {
void myProtocal();
}
class A extends StatelessWidget implements ICustomWidget {
@override
void myProtocal() {
// TODO: implement myProtocal
}
@override
Widget build(BuildContext context) {
//Implementation
}
}
class B extends ICustomWidget {
// compilation error, `myProtocal` not implemented
@override
Widget build(BuildContext context) {
//Implementation
}
}
这篇关于Flutter:继承自抽象的无状态小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文