[英]How can I make each grid tile clickable to bring me to a new page in Flutter
I need help with the code below... I have created a grid with a specific number of tiles... I would like to be able to click individually on each tile and open a new page after clicking on it.我需要以下代码的帮助...我已经创建了一个具有特定数量的图块的网格...我希望能够单独单击每个图块并在单击后打开一个新页面。 How can I do this in android studio?
如何在 android 工作室中做到这一点?
class GridOne extends StatefulWidget {
@override
_GridOneState createState() => new _GridOneState();
}
class _GridOneState extends State<GridOne> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: GridView.count(
crossAxisCount: 6,
children: List.generate(
947,
(index) {
return Card(
elevation: 10.0,
color: Color(0xFF184946),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Color(0xFF184946),
child: Center(
child: Text(
'$index',
style: TextStyle(fontSize: 18, color: Color(0xFFFCF8F8)),
),
),
),
),
);
},
),
),
);
}
import 'package:flutter/material.dart';
class GridOne extends StatefulWidget {
@override
_GridOneState createState() => new _GridOneState();
}
class _GridOneState extends State<GridOne> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: GridView.count(
crossAxisCount: 6,
children: List.generate(
947,
(index) {
return RawMaterialButton(
onPressed: () {
/// Navigation code will come here
},
child: Card(
elevation: 10.0,
color: Color(0xFF184946),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Color(0xFF184946),
child: Center(
child: Text(
'$index',
style: TextStyle(fontSize: 18, color: Color(0xFFFCF8F8)),
),
),
),
),
),
);
},
),
),
);
}
We can wrap the Card
Widget
in a GestureDetector
to use the onTap()
method:我们可以将
Card
Widget
包装在GestureDetector
中以使用onTap()
方法:
return GestureDetector(
onTap: () {
// let's use the magic of Navigator to navigate to another screen
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()), // second screen widget
);
},
child: Card(
elevation: 10.0,
color: Color(0xFF184946),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Color(0xFF184946),
child: Center(
child: Text(
'$index',
style: TextStyle(fontSize: 18, color: Color(0xFFFCF8F8)),
),
),
),
),
),
);
Our full snippet would then be:我们的完整片段将是:
class GridOne extends StatefulWidget {
@override
_GridOneState createState() => new _GridOneState();
}
class _GridOneState extends State<GridOne> {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: GridView.count(
crossAxisCount: 6,
children: List.generate(
947,
(index) {
return GestureDetector(
onTap: () {
// let's use the magic of Navigator to navigate to another screen
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()), // second screen widget
);
},
child: Card(
elevation: 10.0,
color: Color(0xFF184946),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
color: Color(0xFF184946),
child: Center(
child: Text(
'$index',
style: TextStyle(fontSize: 18, color: Color(0xFFFCF8F8)),
),
),
),
),
),
);
},
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.