on未定义按下的呼叫



开发需要多个屏幕的应用程序。 下面现在只显示两个图标,稍后会更多,我需要它们能够在按下时进入相应的屏幕。 除了 onPressed 功能外,一切都有效。 我得到的错误是

未定义命名参数"onPressed">

我是否在错误的位置使用了 onPressed 功能? 我尝试在其他功能之间移动它,但出现相同的错误。

任何帮助不胜感激

主飞镖

import 'package:flutter/material.dart';
import './food_screen.dart';
void main(List<String> args) {
runApp(new MaterialApp(
home : MyApp(),
));
}
class MyApp extends StatefulWidget {
@override 
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override 
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title :Text('Main Title'),
backgroundColor: Colors.blue,
),
backgroundColor: Colors.blue[100],
body: Container(
padding: EdgeInsets.all(30.0),
child: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){
Navigator.push(context,
MaterialPageRoute(builder: (context)=>FoodScreen())
);
},
splashColor: Colors.blue,
child: Center(
child: Column(
children: <Widget>[
Icon(Icons.fastfood, size: 70.0),
Text("FOOD", style: new TextStyle(fontSize: 28.0))
]
)
),
),
),
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){},
splashColor: Colors.blue,
child: Center(
child: Column(
children: <Widget>[
Icon(Icons.directions_car, size: 70.0),
Text("VEHILCES", style: new TextStyle(fontSize: 28.0))
],
),
),
),
),
]
)
)
);
}
}

food_screen.飞镖

import 'package:flutter/material.dart';
import './main.dart';

class FoodScreen extends StatelessWidget {
@override 
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
);
}
}
Card

不支持onPressed属性,你已经有InkWell它有onTap,你可以把onPressed方法操作放在里面。

onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context)=>FoodScreen())
);
}

Card 没有任何 onpressed(( 的属性 您可以添加一个浮动按钮并将其路由到第二页,即 food_screen.dart

https://api.flutter.dev/flutter/material/Card-class.html

如果您想在卡片小部件上添加点击,只需使用手势检测器包装卡片即可。

最新更新