flutter - 扩展小部件的最佳做法是什么?
问题描述
我想创建一个包含按钮的自定义 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);
}
}
解决方案
我需要在 Scaffold 的构造函数中准确地指定每个参数作为我的输入参数。有没有更好的方法来扩展小部件?
没有更好的办法。有讨论允许更简洁的语法,但不清楚这是否会实现。
实际上最佳实践是不扩展,而是组合,但这并没有改变您需要显式转发所有构造函数参数。
推荐阅读
- python - openpyxl - 如何将图像添加到工作表?
- java - MySQLNonTransientConnectionException 无法创建到数据库服务器的连接
- e-commerce - SagePay:不接受测试卡详细信息
- php - 如何在 HTML 中查找数字?
- kubernetes - 如何在更改后强制 Kubernetes CoreDNS 重新加载其 Config Map?
- php - 即使数据正确且有效,validate 方法也总是返回 false [Yii2]
- tsql - TSQL:两个表的组合
- kubernetes - 使用 kubectl 命令从远程 url 传递清单文件
- javascript - 在浏览器中前进和前进时保持 Javascript 更改
- arrays - $lookup 嵌套文档