我在一个单独的类中创建了一个按钮,并且想知道是否可以使用与我创建的相同的按钮类来导航到不同的屏幕?计划是使用onPress()函数导航到不同页面。
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:senior_design/View/Helm.dart';
class Button extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return SizedBox(
width: 100,
height: 100,
child:
RaisedButton(
//alignment: Alignment(100.0, 100.0),
child: Row(
children: <Widget>[
Text("Button"),
]
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Helm()),
);
}
),
);
}
}
最佳答案
下午好,
我相信,如果您可以将方法作为参数传递给您,则可以在不同的屏幕上进行导航。
// Creating a custom buttom with a method passed by parameter
class Button extends StatelessWidget {
final VoidCallback onPress;
final double width;
final double height;
Button({this.onPress, this.width, this.height});
@override
Widget build(BuildContext context) {
return SizedBox(
width: width,
height: height,
child: RaisedButton(
color: Colors.pink,
textColor: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Button"),
],),
onPressed: onPress,
),
);
}
}
我为您做了一个关于https://dartpad.dev/42c49f8cd0c8dbd834525eb41630d599的示例。
希望对您有帮助。
拥抱。
关于flutter - 如何使用相同的按钮导航到Flutter中的不同路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884976/