Widget _myListView(BuildContext context) {
final titles = ['bike', 'boat', 'bus', 'car',
'railway', 'run', 'subway', 'transit', 'walk'];
final icons = [Icons.directions_bike, Icons.directions_boat,
Icons.directions_bus, Icons.directions_car, Icons.directions_railway,
Icons.directions_run, Icons.directions_subway, Icons.directions_transit,
Icons.directions_walk];
final ontaps = [ ,] ```how can i add multiple onTap events ```
return ListView.builder(
itemCount: titles.length,
itemBuilder: (context, index) {
return Card( // <-- Card widget
child: ListTile(
leading: Icon(icons[index]),
title: Text(titles[index]),
onTap: ,
),
);
},
);
}
请帮助我如何轻松地为长列表轻松添加onTap事件
1。如何使用使用字符串添加图标,标题,子标题的方式添加onTap事件 2.我是新手,请原谅我的错误。谢谢
答案 0 :(得分:0)
要为列表对象中的所有小部件添加onTap事件,可以使用以下代码,
return ListView.builder(
itemCount: titles.length,
itemBuilder: (context, index) {
return Card( // <-- Card widget
child: ListTile(
leading: IconButton(icon: Icon(icons[index]),onPressed: (){
// add your code on tap for your iconButton
},),
title: Text(titles[index]),
onTap: (){
// add your code on tap for your listview row tile
},
),
);
},
);
这将为您工作。
答案 1 :(得分:0)
更新的答案,现在,如果您选择“骑自行车”,它将导航到其他页面:
main.dart
import 'package:flutter/material.dart';
import 'package:prova/page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
routes: {
'other_page': (BuildContext context) => Example(),
},
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: _myListView(context),
);
}
}
Widget _myListView(BuildContext context) {
final titles = [
'bike',
'boat',
'bus',
'car',
'railway',
'run',
'subway',
'transit',
'walk'
];
final icons = [
Icons.directions_bike,
Icons.directions_boat,
Icons.directions_bus,
Icons.directions_car,
Icons.directions_railway,
Icons.directions_run,
Icons.directions_subway,
Icons.directions_transit,
Icons.directions_walk
];
functionBike() {
Navigator.pushNamed(context, 'other_page');
print('directions_bike');
}
functionBoat() {
print('directions_boat');
}
functionBus() {
print('directions_bus');
}
functionCar() {
print('directions_car');
}
functionRailway() {
print('directions_railway');
}
functionRun() {
print('directions_run');
}
functionSubway() {
print('directions_subway');
}
functionTransit() {
print('directions_transit');
}
functionWalk() {
print('directions_walk');
}
final List<Function> ontaps = [
functionBike,
functionBoat,
functionBus,
functionCar,
functionRailway,
functionRun,
functionSubway,
functionTransit,
functionWalk
];
return ListView.builder(
itemCount: titles.length,
itemBuilder: (context, index) {
return Card(
// <-- Card widget
child: ListTile(
leading: Icon(icons[index]),
title: Text(titles[index]),
onTap: () {
ontaps[index]();
},
),
);
},
);
}
希望有帮助。