Pranav
Pranav

Reputation: 451

How to print a variableName(String) of ListTile in flutter

How can i print String of a ListTile with onTap,
In this case "image_caption" is the string.

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

Upvotes: 0

Views: 400

Answers (1)

dev-aentgs
dev-aentgs

Reputation: 1288

Use string interpolation inside quotes print(' Tapped image is $image_caption'); or directly print(image_caption); without single quotes.

Upvotes: 1

Related Questions