使用按钮 class 导航到新页面
Navigating to a new page with button class
这是我的按钮 class。我想使用它的 on pressed 功能导航到另一个页面。
class DigerButtonu extends StatelessWidget {
final String butonyazisi;
final IconData butoniconu;
final Function butonfonksiyonu;
const DigerButtonu(this.butonyazisi,this.butonfonksiyonu,this.butoniconu);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(left: 20,right: 20),
width: 50,
height: 550,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Color(0xFF15182D).withOpacity(0.4),
blurRadius: 8,
spreadRadius: 2,
offset: Offset(4,4),
),
],
borderRadius: BorderRadius.all(Radius.circular(10)),
border: Border.all(
color: Colors.black,
width: 0.2,
)
),
child: ElevatedButton(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(butoniconu, color: Colors.white,size: 22,),
MyVerticalText(butonyazisi),
Icon(butoniconu, color: Colors.white,size: 22,),
],
),
style: ElevatedButton.styleFrom(
primary: Color(0xFF15182D),
onPrimary: Colors.white,
onSurface: Colors.grey,
//elevation: 0,
),
onPressed: (){
butonfonksiyonu;
},
),
);
}
}
但是当我尝试使用下面的代码时,它显示“无法将参数类型‘Future’分配给参数类型 'Function'”。我该如何使用它?
DigerButtonu('AYARLAR',Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)
您可以简单地使用 lambda 来传递函数。目前,您传递的是方法的结果,而不是方法本身。
这是带有相应 lambda 表达式的代码:
DigerButtonu('AYARLAR', () => Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)
这是我的按钮 class。我想使用它的 on pressed 功能导航到另一个页面。
class DigerButtonu extends StatelessWidget {
final String butonyazisi;
final IconData butoniconu;
final Function butonfonksiyonu;
const DigerButtonu(this.butonyazisi,this.butonfonksiyonu,this.butoniconu);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(left: 20,right: 20),
width: 50,
height: 550,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Color(0xFF15182D).withOpacity(0.4),
blurRadius: 8,
spreadRadius: 2,
offset: Offset(4,4),
),
],
borderRadius: BorderRadius.all(Radius.circular(10)),
border: Border.all(
color: Colors.black,
width: 0.2,
)
),
child: ElevatedButton(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(butoniconu, color: Colors.white,size: 22,),
MyVerticalText(butonyazisi),
Icon(butoniconu, color: Colors.white,size: 22,),
],
),
style: ElevatedButton.styleFrom(
primary: Color(0xFF15182D),
onPrimary: Colors.white,
onSurface: Colors.grey,
//elevation: 0,
),
onPressed: (){
butonfonksiyonu;
},
),
);
}
}
但是当我尝试使用下面的代码时,它显示“无法将参数类型‘Future’分配给参数类型 'Function'”。我该如何使用它?
DigerButtonu('AYARLAR',Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)
您可以简单地使用 lambda 来传递函数。目前,您传递的是方法的结果,而不是方法本身。 这是带有相应 lambda 表达式的代码:
DigerButtonu('AYARLAR', () => Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)