gpt4 book ai didi

function - Flutter GestureDetector无法使用功能

转载 作者:行者123 更新时间:2023-12-03 04:45:32 25 4
gpt4 key购买 nike

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});
}

大家好
我正在构建一个Android应用程序。上面的代码是我代码的仪表板(菜单)部分。当我希望它转到另一个屏幕时,GestureDetector onTap函数不起作用。但是,如果我只想打印出某物,它会起作用。如果您知道如何解决此问题,可以在这里提供帮助吗?

谢谢。

最佳答案

您可以使用命名路由。

例如 :
routes添加到您的MaterialApp

void main() {
runApp(MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/books': (context) => Books(),
'/videos': (context) => Videos(),
},
));
}

更改 onTap转到特定页面
 onTap: () {
Navigator.pushNamed(context, '/books');
});

相应地更改小部件。

关于function - Flutter GestureDetector无法使用功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62408943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com