如何在 Flutter 中创建自定义类型的 widget?

发布于 2025-01-21 01:13:31 字数 1240 浏览 0 评论 0原文

我正在尝试创建几个小部件 A,它们都应该属于另一种类型的小部件 B,以便最终所有它们都可以传递给构造函数仅接受 B 类型的小部件,但不接受其他自定义小部件,例如 ContainerText 等。

我尝试了这样的操作:

Parent class:

class ProDynamicWidget {

  const ProDynamicWidget({
    required this.height
  });

  final double height;
}

Child类:

class ProDynamicTitle extends ProDynamicWidget {

  final String title;

  ProDynamicTitle({
    required this.title
  }) : super(height: 50.0);

  // Here should be a build method for the title

}

############################################################

class ProDynamicImage extends ProDynamicWidget {

  final String imageUrl;

  ProDynamicImage({
    required this.imageUrl
  }) : super(height: 70.0);

  // Here should be a build method for the image

}

然后我想创建一个仅接受类型小部件的小部件ProDynamicWidget:

class TestOneWidget extends StatelessWidget {

  const TestOneWidget({ 
    Key? key,
    required this.widget
  }) : super(key: key);

  final ProDynamicWidget widget;

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

我真的不明白现在如何才能得到具有单独构建方法的子小部件,以及最后的构造器仅接受这些子小部件而不是每种类型的小部件的方式。

I am trying to create a couple of widgets A that all should belong to another type of widget B, so that in the end all of them could be passed to a constructor that accepts only widgets of type B, but not other custom widgets like Container, Text, etc.

I tried something like this:

Parent class:

class ProDynamicWidget {

  const ProDynamicWidget({
    required this.height
  });

  final double height;
}

Child classes:

class ProDynamicTitle extends ProDynamicWidget {

  final String title;

  ProDynamicTitle({
    required this.title
  }) : super(height: 50.0);

  // Here should be a build method for the title

}

############################################################

class ProDynamicImage extends ProDynamicWidget {

  final String imageUrl;

  ProDynamicImage({
    required this.imageUrl
  }) : super(height: 70.0);

  // Here should be a build method for the image

}

I then wanted to create a widget that only accept widgets of type ProDynamicWidget:

class TestOneWidget extends StatelessWidget {

  const TestOneWidget({ 
    Key? key,
    required this.widget
  }) : super(key: key);

  final ProDynamicWidget widget;

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I do not really get how can now end up with child widgets that have separate build methods and a way the constructur at the end only accepts these child widgets instead of every type of widget.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2025-01-28 01:13:31

使 ProDynamicWidget abstract 并让它扩展 StatelessWidget

abstract class ProDynamicWidget extends StatelessWidget{

  const ProDynamicWidget({
    required this.height
  });

  final double height;
}

接下来,简单地 ProDynamicTitleProDynamicImage扩展ProDynamicWidget,因此必须定义构建方法:

class ProDynamicTitle extends ProDynamicWidget {

  final String title;

  const ProDynamicTitle({
    required this.title
  }) : super(height: 50.0);

  @override
  Widget build(BuildContext context) {
    return Text(title);
  }
}

class ProDynamicImage extends ProDynamicWidget {

  final String imageUrl;

  const ProDynamicImage({
    required this.imageUrl
  }) : super(height: 70.0);

  @override
  Widget build(BuildContext context) {
    return Image(image: NetworkImage(imageUrl));
  }
}

您可以保持TestOneWidget不变。它只接受 ProDynamicWidget 的后代。

Make ProDynamicWidget abstract and let it extend StatelessWidget:

abstract class ProDynamicWidget extends StatelessWidget{

  const ProDynamicWidget({
    required this.height
  });

  final double height;
}

Next, ProDynamicTitle and ProDynamicImage simply extend ProDynamicWidget and will thus have to define the build method:

class ProDynamicTitle extends ProDynamicWidget {

  final String title;

  const ProDynamicTitle({
    required this.title
  }) : super(height: 50.0);

  @override
  Widget build(BuildContext context) {
    return Text(title);
  }
}

class ProDynamicImage extends ProDynamicWidget {

  final String imageUrl;

  const ProDynamicImage({
    required this.imageUrl
  }) : super(height: 70.0);

  @override
  Widget build(BuildContext context) {
    return Image(image: NetworkImage(imageUrl));
  }
}

You can keep TestOneWidget as is. It will only accept descendants of ProDynamicWidget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文