【发布时间】:2020-05-10 04:17:42
【问题描述】:
我知道如何在课堂上制作一个 item-widget 并在整个应用程序中使用它,如下所示:
import 'package:flutter/material.dart';
class MenuButtons extends StatelessWidget {
final String titleName;
final String goTo;
final double width;
final double height;
MenuButtons(this.titleName,{this.goTo, this.width, this.height});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(10.0),
width: width,
height: height !=null ? height : 50.0,
child: RaisedButton(
color: Color.fromRGBO(121, 85, 72, 1.0),
elevation: 5.0,
child: Text(
titleName,
style: TextStyle(fontSize: 18.0, color: Colors.white),
),
onPressed: () {
Navigator.pushReplacementNamed(context, goTo);
},
),
);
}
}
用法如下:
MenuButtons('Title',goTo: 'NextScreen',width: 350.0,),
现在,如果我想拥有更多小部件,我可以使用相同的类,但可能使用不同的构造函数,或者我是否需要为每个要在多个地方使用的小部件单独的类...
【问题讨论】:
标签: flutter widget code-reuse