简体   繁体   中英

How to Change background color of ListTile upon selection in Flutter

import 'package:JAPANESE/data/hiraganaall.dart';
import 'package:flutter/material.dart';

class HiraganaPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Hiragana",
          style: TextStyle(
            color: Colors.white,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.blueAccent,
      ),
      body: ListView.separated(
        separatorBuilder: (context, index) {
          return Divider(
            thickness: 3.0,
            height: 1.0,
          );
        },
        itemCount: data.length,
        itemBuilder: (context, index) {
          return Container(
            decoration: BoxDecoration(color: Colors.grey[300]),
            child: ListTile(
              leading: CircleAvatar(
                backgroundColor: Colors.white,
                radius: 28,
                child: Image.network(
                  data[index]["writing"],
                ),
              ),
              title: Text(
                "Text: " + data[index]["key"],
                style: TextStyle(
                  fontFamily: "Merienda",
                  fontSize: 18.22,
                ),
              ),
              subtitle: Text(
                "Reading: " + data[index]["reading"],
                style: TextStyle(
                  fontFamily: "Merienda",
                  fontSize: 18.22,
                ),
              ),
            ),
          );
         },
      ),
    );
  }
}

I've made a ListView in Flutter, but now I have some ListTiles in this ListView that can be selected. Upon selection, I want the background color to change to a color of my choice. I don't know how to do that. In the docs they mention that a ListTile has a property style. However, when I try to add that (as in third last line in the code below), this style property gets a squiggly red line underneath and the compiler tells me that The named parameter 'style' isn't defined.

You can just wrap the ListTile with a container and change it's color. Here's an example with that code and with a method to change the color:

class Issue66349460 extends StatefulWidget {
  @override
  _Issue66349460State createState() => _Issue66349460State();
}

class _Issue66349460State extends State<Issue66349460> {
  List<Item> items = [
    Item(title: 'Item 1', color: Colors.red),
    Item(title: 'Item 2', color: Colors.blue),
    Item(title: 'Item 3', color: Colors.green),
  ];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: items.length,
      itemBuilder: (context, index) {
        return Container(
          color: items[index].color,
          child: ListTile(
            title: Text(items[index].title),
            onTap: () => changeListTileColor(items[index]),
          ),
        );
      }
    );
  }

  void changeListTileColor(Item item){
    setState(() {
      item.color = Colors.deepPurple;
    });
  }
}

class Item {
  String title;
  Color color;

  Item({@required this.title, this.color});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM