简体   繁体   English

是否可以创建从其他小部件继承的 flutter 小部件?

[英]Is it possible to create flutter widgets that inherit from other widgets?

I'm looking into how to write my own widgets.我正在研究如何编写自己的小部件。 I've seen a number of examples that have a CustomWidget that either inherits from StatefulWidget or StatelessWidget.我已经看到了许多具有继承自 StatefulWidget 或 StatelessWidget 的 CustomWidget 的示例。 Is it possible to inherit from Stack or TextBox or one of the widgets that I've seen returned from build?是否可以从 Stack 或 TextBox 或我看到的从构建返回的小部件之一继承? What I'm thinking of is taking a widget like TextBox, adding custom theming, and then exporting it as a new widget to use.我正在考虑采用像 TextBox 这样的小部件,添加自定义主题,然后将其导出为要使用的新小部件。

I've seen a lot of examples using Material UI.我见过很多使用 Material UI 的例子。 For my needs, I need something less cookie-cutter that let's me deside what things are meant to look like how they should style.为了我的需要,我需要一些不那么千篇一律的东西,让我决定事物的外观应该如何。

Thanks.谢谢。

Yes, you can create a widget extending some other widget but you will probably need to conform to its super constructor.是的,您可以创建一个扩展一些其他小部件的小部件,但您可能需要遵守它的超级构造函数。

You are talking about TextBox in your question but it is not a widget so I'm guessing that you were talking about the Text widget or a similar one.您在问题中谈论的是TextBox ,但它不是一个小部件,所以我猜您是在谈论Text小部件或类似的小部件。 Here's a code sample of how you could create your own widget:以下是如何创建自己的小部件的代码示例:

class MyText extends Text {
  const MyText() : super("my-text"); // Need to pass a data value to the super constructor.

  /// You can override the base build method to return a different widget,
  /// in this case I am returning the base super.build (which will render
  /// the Text widget) wrapped inside a red Container.
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: super.build(context),
    );
  }
}

Try the full test code on DartPad在 DartPad 上试用完整的测试代码

What I'm thinking of is taking a widget like TextBox, adding custom theming, and then exporting it as a new widget to use.我正在考虑采用像 TextBox 这样的小部件,添加自定义主题,然后将其导出为要使用的新小部件。

Then, creating a new custom widget by extending TextBox in not the way to go.然后,通过将TextBox扩展到 go 来创建一个新的自定义小部件。

If you do that, you will have to hardcode every properties of the constructor of TextBox in yours.如果你这样做,你将不得不硬编码你的 TextBox 构造函数的每个属性。 It will be a pain to do and to maintain.做和维护会很痛苦。

FLutter base principle in Composition over Inheritance. FLutter 基本原理在 Inheritance 上的组合。

So, in order to achieve what you need (Theming), you just have to use Theme capabilities:因此,为了实现您所需要的(主题),您只需要使用主题功能:

// global theming once and for all for all the app
MaterialApp(
  theme: ThemeData(inputDecorationTheme: InputDecorationTheme()),
  home: const MyHomePage(),
);

or或者

// located theme overload just for here
Theme(data: Theme.of(context).copyWith(inputDecorationTheme: InputDecorationTheme()), child: TextFormField());

Theming only may be not enough but you really should avoid inheriting complex native widgets, prefer composition of property factorisation仅主题化可能还不够,但您确实应该避免继承复杂的原生小部件,更喜欢属性分解的组合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM