[英]Flutter GestureDetector is not working on functions
class GridDashboard extends StatefulWidget {
final Function onTap;
const GridDashboard({Key key, this.onTap}) : super(key: key);
@override
_GridDashboardState createState() => _GridDashboardState();
}
class _GridDashboardState extends State<GridDashboard> {
Items item1 = new Items(
title: 'Books',
img: 'assets/images/open-book.png',
onTap: () {
Books();
});
Items item2 = new Items(
title: 'Audio',
img: 'assets/images/headphones.png',
onTap: () => print('Audio')); // it works when I just print smth
Items item3 = new Items(
title: 'Videos',
img: 'assets/images/play-button.png',
onTap: () {
Videos();
});
@override
Widget build(BuildContext context) {
List<Items> myList = [item1, item2, item3];
return Flexible(
child: GridView.count(
childAspectRatio: 1.0,
padding: EdgeInsets.only(left: 30, right: 20, top: 220),
crossAxisCount: 2,
crossAxisSpacing: 20,
mainAxisSpacing: 20,
children: myList.map((data) {
return GestureDetector(
onTap: data.onTap, // this line is not working
child: Container(
decoration: BoxDecoration(
color: Color(0xFFFFFFFF),
borderRadius: BorderRadius.circular(35),
boxShadow: [
BoxShadow(
color: Color(0xFF373234),
blurRadius: 6.0,
offset: Offset(0, 2),
),
],
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(data.img, width: 45),
SizedBox(height: 15),
Text(
data.title,
style: GoogleFonts.openSans(
textStyle: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
);
}).toList(),
),
);
}
}
class Items {
String title;
String img;
Function onTap;
Items({this.img, this.title, this.onTap});
}
Hi people.大家好。 I am building an android app.
我正在构建一个 android 应用程序。 The above code is for a dashboard (menu) section of my code.
上面的代码用于我的代码的仪表板(菜单)部分。 The GestureDetector onTap function is not working when I want it to go to another screen.
当我想要 go 到另一个屏幕时,GestureDetector onTap function 不起作用。 But it works if I just want to print out smth.
但如果我只是想打印出来,它就可以了。 If you know how to solve this issue can you please help here?
如果您知道如何解决此问题,您可以在这里提供帮助吗?
Thank you.谢谢你。
You can use named routes.您可以使用命名路由。
For Example: Add the routes
to you MaterialApp
例如:将
routes
添加到您的MaterialApp
void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/books': (context) => Books(),
'/videos': (context) => Videos(),
},
));
}
Change onTap
to go to the specific page将
onTap
改为go到具体页面
onTap: () {
Navigator.pushNamed(context, '/books');
});
Make changes to the widgets accordingly.相应地更改小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.