Flutter - 放入 setState 后代码无法正常工作

Flutter - codes not working correctly after putting inside setState

我正在尝试在用户单击后添加到数组列表。我正在使用 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());
                    }

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

enter image description here

但是,当我将代码放入 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());
                      }
                    });

enter image description here

我想做的是,根据用户选择的特定项目,show/hide ListTile 中的 'trailing' 图标。

我的完整代码(没有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,
                    ),
                  ),
                ),
              );
            })
      ],
    ),
  ),
);
}
}

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

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,
                        ),
                      ),
                    ),
                  );
                })
          ],
        ),
      ),
    );
  }
}