【问题标题】:Can't figure out how to delete the numbers that I have generated with onPressed无法弄清楚如何删除我用 onPressed 生成的数字
【发布时间】:2023-01-18 01:51:09
【问题描述】:

我用一个加号按钮和一个减号按钮制作了一个简单的程序,可以连续生成数字。使用加号按钮生成数字完全没问题,但我尝试使用减号按钮删除数字,但无法解决这个问题。

`import 'package:flutter/material.dart';

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

class App extends StatefulWidget {
  @override
  State<App> createState() => _AppState();
}

// the state of the stateful widget is the one that holds the data and the ui of the widget
class _AppState extends State<App> {
  List<int> numbers = [];

  int counter = 0;

  void onClickedPlus() {
    setState(() {
      numbers.add(numbers.length);
    }); //setState function is a function that we use to notify our state class that the data has changed
  }

  void onClickedMinus() {
    setState(() {
      numbers.remove(numbers.length);
    }); //setState function is a function that we use to notify our state class that the data has changed
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: const Color(
            0xFFF4EDDB), //Color.fromRGBO => red, green, blue, opacity
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                'Click Count',
                style: TextStyle(fontSize: 30),
              ),
              for (var n in numbers) Text('$n'),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    iconSize: 40,
                    onPressed: onClickedPlus,
                    icon: const Icon(Icons.add_box_rounded),
                  ),
                  IconButton(
                    iconSize: 40,
                    onPressed: onClickedMinus,
                    icon: const Icon(Icons.remove_circle_outline_rounded),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}`

Current App UI

我希望通过单击减号按钮连续删除数字。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    List.remove(item) 从此列表中删除第一次出现的值。

    所以 numbers.remove(numbers.length); 没有删除最后一个数字,因为 numbers.length 总是比列表中的最后一个数字大 1。相反,您应该使用List.removeLast()

    void onClickedMinus() {
      setState(() {
        numbers.removeLast();
      }); 
    }
    

    【讨论】:

      【解决方案2】:

      编写代码的方式 remove 案例总是会失败。让我解释一下为什么,

      您的列表一开始的长度为 0,第一个添加按钮会将 0 添加到列表中。 下一个添加按钮会将 1 添加到列表中。 现在,当您单击减号按钮时,长度为 2,但列表仅包含 [0,1]。 所以这永远行不通。 反而,

      你可以写

      void onClickedMinus() {
          setState(() {
            numbers.remove(numbers.length-1);
          }); //setState function is a function that we use to notify our state class that the data has changed
        }
      

      这将从列表中删除最后一个元素。

      【讨论】:

        猜你喜欢
        • 2018-08-11
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多