gpt4 book ai didi

flutter - 如何在flutter中打印ListTile的variableName(String)

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

如何使用ListTile打印onTap的字符串,
在这种情况下,“image_caption”是字符串。

import 'package:flutter/material.dart';

class HorizontalList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 85.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Catagory(
image_location: 'Images/Icons/smartphone.png',
image_caption: 'Electronics',
),
Catagory(
image_location: 'Images/Icons/car.png',
image_caption: 'Vehicles',
),
Catagory(
image_location: 'Images/Icons/car.png',
image_caption: 'Vehicles',
),
Catagory(
image_location: 'Images/Icons/car.png',
image_caption: 'Vehicles',
),
Catagory(
image_location: 'Images/Icons/car.png',
image_caption: 'Vehicles',
),
],
),
);
}
}

class Catagory extends StatelessWidget {
final String image_location;
final String image_caption;
Catagory({this.image_location, this.image_caption});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(0.0),
child: GestureDetector(
onTap: () {
print("image_caption"); //Print tapped image_caption
},
child: Container(
width: 101.0,
color: Color(0xFF051622),
child: ListTile(
title: CircleAvatar(
//Circle with gold border
radius: 32.0,
backgroundColor: Color(0xFFDEB992),
child: CircleAvatar(
//Circle which containes the icon
radius: 29.0,
backgroundColor: Colors.white,
child: Image.asset(image_location),
),
),
subtitle: Container(
alignment: Alignment.topCenter,
height: 15.0,
child: Text(
image_caption,
style: TextStyle(
color: Colors.white,
),
),
),
),
),
),
);
}
}

最佳答案

在引号print(' Tapped image is $image_caption');内使用字符串插值,或者在不带单引号的情况下直接使用print(image_caption);

关于flutter - 如何在flutter中打印ListTile的variableName(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62066309/

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