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

标签 flutter dart

如何使用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/

相关文章:

flutter - Flutter:无法更新变量状态

flutter - ListView.builder 中的 TextFields 正在消失

dart - Dart 中的动态类方法调用

dart - If 语句在 flutter 中即使是 true 也不起作用

flutter - 为什么我的 TextField 和 List 在两者一起 flutter 时不显示

flutter - 如何 SliverAppBar 始终显示阴影 - 扩展模式

android - 如何为 64 位设备支持我的 flutter 应用程序?

Flutter 网络 flavor

dart - 如何计算Flutter中的触摸坐标?

ios - iOS 中是否有 INTERNET 权限?