扩展窗口小部件的最佳实践是什么?

时间:2018-10-28 21:52:34

标签: flutter

我想创建一个包含按钮的自定义脚手架小部件,以便我可以重用此 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);
  }

}

1 个答案:

答案 0 :(得分:2)

  

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

没有更好的方法。正在进行讨论以允许使用更简洁的语法,但尚不清楚该语法是否会实现。

实际上,最佳实践是不进行扩展,而是进行组合,但这并没有改变,您需要显式转发所有构造函数参数。