1

我试图在用户单击后添加到数组列表。我正在使用 InkWell,onTap 功能。

child: InkWell(
                  onTap: () {
                    if (tempArray.contains(entries[index].toString())) {
                      tempArray.remove(entries[index].toString());
                      print(tempArray.toList().toString());
                    } else {
                      tempArray.add(entries[index].toString());
                      print(tempArray.toList().toString());
                    }

我的调试控制台正在打印这些,

在此处输入图像描述

但是,当我将代码放入 setState 时,这就是打印出来的内容

                  child: InkWell(
                  onTap: () {
                    setState(() {
                      if (tempArray.contains(entries[index].toString())) {
                        tempArray.remove(entries[index].toString());
                        print(tempArray.toList().toString());
                      } else {
                        tempArray.add(entries[index].toString());
                        print(tempArray.toList().toString());
                      }
                    });

在此处输入图像描述

我想要做的是根据用户选择特定项目的任何内容来显示/隐藏 ListTile 中的“尾随”图标。

我的完整代码(没有 setState)如下,

import 'package:flutter/material.dart';
class Personal1Redo extends StatefulWidget {
@override
_Personal1RedoState createState() => _Personal1RedoState();
}
class _Personal1RedoState extends State<Personal1Redo> {
@override
Widget build(BuildContext context) {
final mediaQuery = MediaQuery.of(context);

List<String> entries = [
  'Residental Loan',
  'Personal Loan',
  'Car Loan',
  'Renovation Loan',
  'Savings Account',
];

List<String> tempArray = [];

final heading = Column(
  children: [
    const SizedBox(
      height: 40,
    ),
    SizedBox(
      height: (mediaQuery.size.height - mediaQuery.padding.top) * 0.1,
      child: const Align(
        alignment: Alignment.center,
        child: Text(
          'What do you aspire to do?',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 20,
          ),
        ),
      ),
    ),
    const SizedBox(
      height: 20,
    ),
  ],
);

return Scaffold(
  appBar: AppBar(
    title: Text('Borrower redo'),
  ),
  body: SingleChildScrollView(
    child: Column(
      children: <Widget>[
        heading,
        ListView.builder(
            shrinkWrap: true,
            itemCount: entries.length,
            itemBuilder: (ctx, index) {
              return Container(
                width: mediaQuery.size.width * 0.9,
                padding: const EdgeInsets.all(15),
                child: InkWell(
                  onTap: () {
                    if (tempArray.contains(entries[index].toString())) {
                      tempArray.remove(entries[index].toString());
                      print(tempArray.toList().toString());
                    } else {
                      tempArray.add(entries[index].toString());
                      print(tempArray.toList().toString());
                    }
                  },
                  child: Card(
                    margin: const EdgeInsets.all(10),
                    elevation: 8,
                    child: ListTile(
                      title: Text(
                        entries[index],
                        style: TextStyle(
                          color: Colors.grey.shade800,
                          fontSize: 20,
                        ),
                        textAlign: TextAlign.center,
                      ),
                      trailing: tempArray.contains(entries[index])
                          ? Icon(Icons.check_box_outline_blank_outlined)
                          : null,
                    ),
                  ),
                ),
              );
            })
      ],
    ),
  ),
);
}
}

非常感谢任何帮助和指导,谢谢!

4

1 回答 1

1

在方法之外定义变量和函数build。作为setState方法,调用build,每次调用。像这样 :

import 'package:flutter/material.dart';
class Personal1Redo extends StatefulWidget {
  @override
  _Personal1RedoState createState() => _Personal1RedoState();
}

class _Personal1RedoState extends State<Personal1Redo> {
  List<String> entries = [
    'Residental Loan',
    'Personal Loan',
    'Car Loan',
    'Renovation Loan',
    'Savings Account',
  ];

  List<String> tempArray = [];

  getHeadingWidget(BuildContext context) {
    return Column(
      children: [
        const SizedBox(
          height: 40,
        ),
        SizedBox(
          height: (mediaQuery!.size.height - mediaQuery.padding.top) * 0.1,
          child: const Align(
            alignment: Alignment.center,
            child: Text(
              'What do you aspire to do?',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 20,
              ),
            ),
          ),
        ),
        const SizedBox(
          height: 20,
        ),
      ],
    );
  }

  

  @override
  Widget build(BuildContext context) {
   final mediaQuery = MediaQuery.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Borrower redo'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            getHeadingWidget(context),
            ListView.builder(
                shrinkWrap: true,
                itemCount: entries.length,
                itemBuilder: (ctx, index) {
                  return Container(
                    width: mediaQuery.size.width * 0.9,
                    padding: const EdgeInsets.all(15),
                    child: InkWell(
                      onTap: () {
                        if (tempArray.contains(entries[index].toString())) {
                          tempArray.remove(entries[index].toString());
                          print(tempArray.toList().toString());
                        } else {
                          tempArray.add(entries[index].toString());
                          print(tempArray.toList().toString());
                        }
                      },
                      child: Card(
                        margin: const EdgeInsets.all(10),
                        elevation: 8,
                        child: ListTile(
                          title: Text(
                            entries[index],
                            style: TextStyle(
                              color: Colors.grey.shade800,
                              fontSize: 20,
                            ),
                            textAlign: TextAlign.center,
                          ),
                          trailing: tempArray.contains(entries[index])
                              ? Icon(Icons.check_box_outline_blank_outlined)
                              : null,
                        ),
                      ),
                    ),
                  );
                })
          ],
        ),
      ),
    );
  }
}

于 2022-02-28T08:35:57.967 回答