简体   繁体   English

如何使 ListTile 中的按钮更改颜色和图标

[英]How to make button change color and icon in the ListTile

I have ListTile in the ListView with RaisedButton as trailing, I want to change color and icon on btn clicked, trouble is if I change it on setstate method all listTile buttons change.我在 ListView 中有 ListTile,RaisedButton 作为尾随,我想在单击 btn 时更改颜色和图标,问题是如果我在 setstate 方法上更改它,所有 listTile 按钮都会更改。 So how to determine each one?那么如何确定每一个呢?

  Widget _getList(BuildContext context,int index,) {
    return Card(
      elevation: 3,
      child: Column(
        children: <Widget>[
          ListTile(
            leading: Image.asset(
              "assets/" + _allDevices[index].image,
              fit: BoxFit.cover,
            ),
            title: Text(_allDevices[index].name),
            subtitle: Text(_allDevices[index].desc),
            trailing: SizedBox.fromSize(
                size: Size(56, 56), // button width and height
                child: ClipOval(
                  child: RaisedButton(
                    elevation: 2,
                    splashColor: Colors.red,
                    color: Colors.blue,
                    onPressed: () {
                      setState(() {
                          //pro should do something here... switch index or something....
                      });
                    },
                    child: Icon(Icons.lock_open),
                  ),
                )),
            onTap: () {},
          )
        ],
      ),
    );
  }

Find this sample, All needed is bool flag in the model class which maintains the click status.找到这个样本,所有需要的是 model class 中的bool标志,它保持点击状态。 On click set it true, if it's already true then set it as false.单击时将其设置为 true,如果它已经为 true,则将其设置为 false。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class Devices {
  String name = '';
  bool isSelected = false;

  Devices(this.name, this.isSelected);
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MyWidget(),
    );
  }
}

class MyWidget extends StatefulWidget {
  @override
  MyWidgetState createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {

  var _allDevices = [
    Devices('Text', false),
    Devices('Text', false),
    Devices('Text', false),
    Devices('Text', false)
  ];

  Widget _getList(BuildContext context, int index) {
    return Card(
      elevation: 3,
      child: Column(
        children: <Widget>[
          ListTile(
            leading: Text('Text'),
            title: Text(_allDevices[index].name),
            subtitle: Text(_allDevices[index].name),
            trailing: SizedBox.fromSize(
                size: Size(56, 56), // button width and height
                child: ClipOval(
                  child: RaisedButton(
                    elevation: 2,
                    color: _allDevices[index].isSelected
                        ? Colors.green
                        : Colors.blue,
                    onPressed: () {
                      setState(() {
                        if (_allDevices[index].isSelected) {
                          _allDevices[index].isSelected = false;
                        } else{
                          _allDevices[index].isSelected = true;
                        }
                      });
                    },
                    child: Icon(Icons.lock_open),
                  ),
                )),
            onTap: () {},
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
          ListView.builder(
              shrinkWrap: true,
              itemCount: 4,
              itemBuilder: (context, index) {
                return _getList(context, index);
              })
        ]));
  }
}

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

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