【问题标题】:What's the best practice to extend a widget?扩展小部件的最佳做法是什么?
【发布时间】:2019-04-01 20:22:35
【问题描述】:

我想创建一个包含按钮的自定义 Scaffold 小部件,以便我可以重复使用此 CustomScaffold。目前,我需要在 Scaffold 的构造函数中完全指定每个参数作为我的输入参数。有没有更好的方法来扩展小部件?

class CustomScaffold {
  // if I want to have fully customized Scaffold, I need to have the following (duplicate) parameters exactly the same what Scaffold defined 
  // Key key,
  // this.appBar,
  // this.body,
  // this.floatingActionButton,
  // this.floatingActionButtonLocation,
  // this.floatingActionButtonAnimator,
  // this.persistentFooterButtons,
  // this.drawer,
  // this.endDrawer,
  // this.bottomNavigationBar,
  // this.bottomSheet,
  // this.backgroundColor,
  // this.resizeToAvoidBottomPadding = true,
  // this.primary = true,

  static Scaffold createCustomScaffold({@required AppBar appBar, Widget body}) {
    final Widget cart = SafeArea(
      child: Align(
        alignment: Alignment.bottomCenter,
        child: RaisedButton(
          child: new Text("????????"),
          color: Colors.amber,
          onPressed: () {
            print("Hello");
          },
        )
      )
    );

    final Widget stack = Stack(
        children: <Widget>[
          body,
          cart
        ],
     );

    return Scaffold(appBar: appBar,
                    body: stack);
  }

}

【问题讨论】:

    标签: flutter flutter-layout flutter-widget


    【解决方案1】:

    我需要在 Scaffold 的构造函数中完全指定每个参数作为我的输入参数。有没有更好的方法来扩展小部件?

    没有更好的方法。有讨论允许更简洁的语法,但不清楚这是否会实现。

    实际上最好的做法是不扩展,而是组合,但这不会改变您需要显式转发所有构造函数参数。

    【讨论】:

      猜你喜欢
      • 2014-09-09
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 2021-05-02
      相关资源
      最近更新 更多