[英]How to make multiple reusable widgets in flutter in one class?
I know how to make one item-widget in a class and use it throughout app, something like this:我知道如何在课堂上制作一个项目小部件并在整个应用程序中使用它,如下所示:
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);
},
),
);
}
}
Usage would be like this:用法如下:
MenuButtons('Title',goTo: 'NextScreen',width: 350.0,),
Now if I want to have more widgets can I use the same class but maybe with different constructor or do I need a separate class for each widget I want to use in multiple places...现在,如果我想要更多的小部件,我可以使用相同的类,但可能具有不同的构造函数,或者我是否需要为我想在多个地方使用的每个小部件使用单独的类...
if the new class just has some params not related to build method, you can declare like this, for example new class IndexMenuButtons如果新类只有一些与构建方法无关的参数,您可以这样声明,例如新类 IndexMenuButtons
class IndexMenuButtons extends MenuButtons{
final int buttonIndex;
ColorMenuButtons(String titleName, this.buttonIndex, {String goTo, double width, double height}) : super(titleName, goTo, width, height);
}
Usage:用法:
var btn = IndexMenuButtons('Title', 100, goTo: 'NextScreen',width: 350.0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.