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. 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)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.