简体   繁体   English

仅更新列表中点击的 ListTile 颜色

[英]Updating only the tapped ListTile color in a list

How do I update the color of only the ListTile that is tapped?如何仅更新被点击的ListTile的颜色?

Whatever I tried, it just changes the color of all tiles when tap.无论我尝试过什么,它都会在点击时更改所有瓷砖的颜色。 How can I retrieve the data and change the color?如何检索数据并更改颜色?

class _DesignState extends State<Design> {
   var status=0;
  var score =0;
 Color getContainerColor() {
    if (status == 0) {
      return Colors.white;
    } else if (status == 1) {
      return Colors.green;
    } else {
      return Colors.red;
    }


  Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
    final record = Record.fromSnapshot(data);
    final record1=  Firestore.instance.collection('creds').document('123').get().then((DocumentSnapshot ds) {
      var s =Record.fromSnapshot(ds);
      score= s.score;
    });

        child: ListTile(
          title: Text(record.name),
          trailing: Text(record.score.toString()),

          onTap: () { record.reference.updateData({'score': FieldValue.increment(score)}),
          setState(){
                status=1;


You're already on the right track, but it looks like you're managing the state of all of your list tile in the same StatefulWidget .您已经走上了正确的轨道,但看起来您正在同一个StatefulWidget 中管理所有列表磁贴的状态

Instead you'll just need to split them up, so that every of your custom ListTile has it's on state.相反,您只需要将它们分开,以便您的每个自定义ListTile都处于开启状态。 The loading of the data can happen in the parent component of your self build ListTile .数据的加载可以发生在您自己构建的ListTile的父组件中。


I'll provide you a short example application.我将为您提供一个简短的示例应用程序。 The following example is without firebase, but it should be no problem to apply these changes to your application.以下示例没有使用 firebase,但将这些更改应用于您的应用程序应该没有问题。

You'd simply have to do the data fetching inside the parent component and pass the score parameter of your example to MyListTile – just like the title in the example below.您只需在父组件中获取数据并将示例的score参数传递给MyListTile - 就像下面示例中的标题一样。

This is runnablbe on it's own, you can simply copy it into a empty Flutter project:这是它自己运行的,你可以简单地将它复制到一个空的 Flutter 项目中:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            MyListTile(title: 'First'),
            MyListTile(title: 'Second'),
            MyListTile(title: 'Third'),
          ],
        ),
      ),
    );
  }
}

class MyListTile extends StatefulWidget {
  final String title;

  MyListTile({this.title});

  @override
  _MyListTileState createState() => _MyListTileState();
}

class _MyListTileState extends State<MyListTile> {
  int status = 0;

  get tileColor {
    switch(status) {
      case 0: {
        return Colors.white;
      }
      case 1: {
        return Colors.green;
      }
      default: {
        return Colors.red;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: tileColor,
      child: ListTile(
        title: Text(widget.title),
        subtitle: Text('Status: $status'),
        onTap: () => setState(() {
          status++;
        }),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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