我有一个自定义按钮小部件:
class Button extends StatelessWidget {
final String text;
Button(this.text);
@override
Widget build(BuildContext context) {
return Container(
height: 50,
child: SizedBox(
width: double.infinity,
child: RaisedButton(
onPressed: () => {}, // Use the function from parent Widget
child: Padding(
padding: EdgeInsets.symmetric(vertical: 13),
child: Text(
text,
style: TextStyle(fontWeight: FontWeight.bold),
)),
color: COLOR_BLUE,
textColor: Colors.white,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在父小部件中,我想将一个onPressed方法传递给这个按钮小部件:
...
myMethod () => {
// do some stuff
}
...
Padding(
padding: EdgeInsets.only(bottom: 10),
child: Button("Log …Run Code Online (Sandbox Code Playgroud)