如何最好地扩展小部件?

3

我希望创建一个自定义的Scaffold Widget,其中包含一个按钮,以便我可以重用这个CustomScaffold。目前,我需要在输入参数中将每个参数与Scaffold构造函数中完全指定。有没有更好的方法来扩展一个widget?

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);
  }

}
1个回答

4

我需要在Scaffold构造函数中像我的输入参数一样明确指定每个参数。有更好的方法来扩展widget吗?

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

实际上最佳实践是不要继承,而是组合,但这并不改变您需要明确转发所有构造函数参数的事实。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接