Изменить значок в списке просмотра в указанной строке c - PullRequest
0 голосов
/ 25 марта 2020

У меня 4 значка в listView.

Когда я нажимаю первый значок, я хочу, чтобы первый значок изменился на другой значок. Но когда я нажимаю, четвертый значок тоже меняется.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'), 
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  List<String> litems = ["1", "0", "0", "1"];
  bool click = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView.builder(
            shrinkWrap: true,
            itemCount: litems.length,
            itemBuilder: (BuildContext context, int index) {
              String value = litems[index];

              Widget icon;
              if (value == "1") {
                icon = Container(
                    height: 25,
                    width: 25,
                    child: IconButton(
                        iconSize: 15,
                        onPressed: () {
                          click = true;
                          setState(() {});
                        },
                        icon: click == true
                            ? Icon(Icons.cloud_upload,
                                color: Colors.green, size: 15)
                            : Icon(Icons.cloud_off,
                                color: Colors.red, size: 15)));
              } else {
                icon = Container(
                    height: 25,
                    width: 25,
                    child: IconButton(
                        iconSize: 15,
                        onPressed: () {},
                        icon: Icon(Icons.cloud_upload,
                            color: Colors.green, size: 15)));
              }
              return icon;
            }));
  }
}

Как сделать только первое изменение значка?

1 Ответ

0 голосов
/ 25 марта 2020

Вы должны изменить litems значение:

     onPressed: () {
                    setState(() {
                      litems[index] = litems[index] == "0" ? "1" : "0";
                    });
                  },

              return Container(
                height: 25,
                width: 25,
                child: IconButton(
                  iconSize: 15,
                  onPressed: () {
                    setState(() {
                      litems[index] = litems[index] == "0" ? "1" : "0";
                    });
                  },
                  icon: value == "0"
                      ? Icon(Icons.cloud_upload, color: Colors.green, size: 15)
                      : Icon(Icons.cloud_off, color: Colors.red, size: 15),
                ),
              );

//               Widget icon;
//               if (value == "1") {
//                 icon = Container(
//                     height: 25,
//                     width: 25,
//                     child: IconButton(
//                         iconSize: 15,
//                         onPressed: () {
//                           click = true;
//                           setState(() {});
//                         },
//                         icon: click == true
//                             ? Icon(Icons.cloud_upload,
//                                 color: Colors.green, size: 15)
//                             : Icon(Icons.cloud_off,
//                                 color: Colors.red, size: 15)));
//               } else {
//                 icon = Container(
//                     height: 25,
//                     width: 25,
//                     child: IconButton(
//                         iconSize: 15,
//                         onPressed: () {},
//                         icon: Icon(Icons.cloud_upload,
//                             color: Colors.green, size: 15)));
//               }
//               return icon;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...