class A extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return AState();
}
}
class AState extends State<A> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: button(onA),
);
}
onA() {
print('A');
}
}
class B extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return BState();
}
}
class BState extends State<B> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: button(onB),
);
}
onB() {
print('B');
}
}
Widget button(Function() action) {
return RaisedButton(
onPressed: action,
child: Text('点击'),
);
}
下面是携带参数方法
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: A());
}
}
class A extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return AState();
}
}
class AState extends State<A> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: button(onA),
);
}
onA(a) {
print(a);
print('A');
}
}
class B extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return BState();
}
}
class BState extends State<B> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: button(onB),
);
}
onB(v) {
print('B');
}
}
Widget button(action) {
return RaisedButton(
onPressed: () {
return action('13');
},
child: Text('点击'),
);
}
|