[英]How to make a class for button widgets
I am going to have a bunch of buttons in my flutter app, and I want to make a class for it so I don't need to copy-paste it and make my code look long for the same type of code. 我的flutter应用程序中将有很多按钮,并且我想为其创建一个类,这样就无需复制粘贴该代码并使我的代码看起来像相同类型的代码。 I make the class, no problem there but I am struggling because I want every button to go to a different page when pressed.
我上课,没问题,但是我很努力,因为我希望每个按钮在按下时都转到不同的页面。 How to achieve this?
如何实现呢?
class thebuttonmaka extends StatelessWidget { final String texxt; final String buum; const thebuttonmaka(this.texxt); @override Widget build(BuildContext context) { // TODO: implement build return Padding( padding: EdgeInsets.only(top: 30), child: Material( elevation: 5, borderRadius: BorderRadius.circular(30), color: Colors.lightBlue, child: MaterialButton( minWidth: 250, padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0), onPressed: WHAT TO PUT HERE?, child: Text(texxt, textAlign: TextAlign.center, style: TextStyle(fontSize: 20) .copyWith(color: Colors.white, fontWeight: FontWeight.bold)), ), ), ); } }
So I basically need help where I put what to put here. 所以我基本上需要帮助,我该放在哪里。 I want to add this option to constructor so whenever I call the class I can put where the button navigates to.
我想将此选项添加到构造函数中,以便每当我调用该类时,都可以将按钮导航到该位置。 Example thebuttonmaka('signup',signuppage)
thebuttonmaka('signup',signuppage)示例
class thebuttonmaka extends StatelessWidget {
final String texxt;
final String buum;
final Function function; // add this
const thebuttonmaka(this.texxt, this.function, this.buum); // change this
@override
Widget build(BuildContext context) {
// TODO: implement build
return Padding(
padding: EdgeInsets.only(top: 30),
child: Material(
elevation: 5,
borderRadius: BorderRadius.circular(30),
color: Colors.lightBlue,
child: MaterialButton(
minWidth: 250,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
onPressed: function, // add this here
child: Text(texxt,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20)
.copyWith(color: Colors.white, fontWeight: FontWeight.bold)),
),
),
);
}
}
You can use it like 你可以像这样使用它
thebuttonmaka('signup',signuppage)
You can define your class like this and customize literally everything about your button (you can pass textalignment and style as well, I am just showing an example) - 您可以像这样定义您的类,并从字面上自定义按钮的所有内容(您也可以传递文本对齐方式和样式,我只是显示一个示例)-
class CommonButton {
static MaterialButton myButton(BuildContext context, int width, int l, int t, int r, int b, func, String text) {
return MaterialButton(
minWidth: width,
padding: EdgeInsets.fromLTRB(l, t, r, b),
onPressed: func,
child: Text(text,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20)
.copyWith(color: Colors.white, fontWeight: FontWeight.bold)),
);
}
}
When you want to use this somewhere, just call it like this - 如果您想在某个地方使用它,只需像这样称呼它-
CommonButton.myButton(...)
(You have to obviously pass the onPressed
function as well) (显然,您还必须传递
onPressed
函数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.