【问题标题】:Change particular ListTile Icon in ListView.builder更改 ListView.builder 中的特定 ListTile 图标
【发布时间】:2020-07-04 09:43:58
【问题描述】:

我对 Flutter 和 Dart 比较陌生,所以我有一个关于 ListView 操作的问题。目前,下面的代码可以工作并在 ListView 中显示一些基本信息。 我将如何实现一个解决方案,以便当我单击特定的 List Tile 时,其图标会更改为其他内容?以下是我目前的股票代码:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: HomePage(),
    ));


class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  Widget _getListItemTile(BuildContext context, int index) {
    return Card(
      child: GestureDetector(
        child: ListTile(
          leading: Icon(Icons.check_box_outline_blank),
          title: Text('My Item'),
          onTap: () {},
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Test App'),
        centerTitle: true,
      ),
      body: ListView.builder(
        itemCount: 7,
        itemBuilder: _getListItemTile,
      ),
    );
  }
}

【问题讨论】:

  • 欢迎来到 SO!告诉我们您的代码是否有效并且您想改进它,还是失败了?如果是这样,为什么,它有什么问题?请参阅“How to Ask”、“Stack Overflow question checklist”和“MCVE”,它们的所有链接页面都有助于解释我们需要了解的内容。目前你还没有告诉我们足够多,所以我们不得不猜测,这不是 SO 方式。

标签: android flutter dart mobile


【解决方案1】:

您可以在下面复制粘贴运行完整代码
您可以声明 List&lt;bool&gt; 并将 leading 图标更改为 boolList[index]
代码sn-p

List<bool> boolList = [true, true, true, true, true, true, true];
...
Card(
      child: GestureDetector(
        child: ListTile(
          leading: boolList[index]
              ? Icon(Icons.check_box_outline_blank)
              : Icon(Icons.place),
          title: Text('My Item'),
          onTap: () {
            boolList[index] = !boolList[index];
            setState(() {});
          },
        ),
      ),
    )

工作演示

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: HomePage(),
    ));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<bool> boolList = [true, true, true, true, true, true, true];

  Widget _getListItemTile(BuildContext context, int index) {
    return Card(
      child: GestureDetector(
        child: ListTile(
          leading: boolList[index]
              ? Icon(Icons.check_box_outline_blank)
              : Icon(Icons.place),
          title: Text('My Item'),
          onTap: () {
            boolList[index] = !boolList[index];
            setState(() {});
          },
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('My Test App'),
        centerTitle: true,
      ),
      body: ListView.builder(
        itemCount: boolList.length,
        itemBuilder: _getListItemTile,
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2021-10-01
    • 1970-01-01
    • 2021-03-18
    • 2020-10-25
    • 2014-05-25
    • 2013-02-04
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多