如何只编写一个函数来增加flutter中的不同计数器变量?

时间:2019-09-17 15:22:50

标签: flutter dart

我正在努力建立一个颤抖的屏幕, 我在一列中有三行,每行包括:

  1. 文本
  2. IconButton(添加图标)
  3. 文本

当我点击添加图标时,我需要增加一个计数器变量并将其显示为Text(第三个项目符号)

在此处签出屏幕:https://ibb.co/37v4rDj

但是我要为不同的计数器变量编写不同的函数,我不希望那样,我想编写一个可以递增不同计数器变量的函数

这是我的代码:

 import 'package:flutter/material.dart';

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

    class MyHomeApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              title: Text("ctr trial"),
            ),
            body: Body(),
          ),
        );
      }
    }

    class Body extends StatefulWidget {
      @override
      _BodyState createState() => _BodyState();
    }

    class _BodyState extends State<Body> {

      int upactr = 0;
      int ndactr = 0;
      int othctr = 0;

      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            //mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                height: 100,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("UPA"),
                  IconButton(
                    icon: Icon(
                      Icons.add,
                    ),
                    onPressed: () { 
            setState(){
            _incrementctr;  // this function should increment only the upactr variable 
            }
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("NDA"),
              IconButton(
                icon: Icon(
                  Icons.add,
                ),
                onPressed: () {
            setState(){
            _incrementctr;  // this function should increment only the ndactr variable 
                }
            },
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("others"),
              IconButton(
                icon: Icon(
                  Icons.add,
                ),
                onPressed: () {
            setState(){

            _incrementctr;  // this function should increment only the othctr variable 
            }}
},
              ),
            ],
          ),
        ],
      ),
    );
  }

  void _incrementctr() {
      // this is a single function which should incerement only one counter variable , any of the following :       
    //1.upactr++;
    //2.ndactr++;
    //3.othctr++;
      }
    }

2 个答案:

答案 0 :(得分:0)

  1. 为每个计数器添加一个文本字段,看起来好像丢失了。每个文本字段都将使用其变量来显示。

  2. 不需要外部功能。在每个onPressed()中,应通过调用upactr ++

  3. 来增加特定的计数器

答案 1 :(得分:0)

        import 'package:flutter/material.dart';

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

        class MyHomeApp extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
            // TODO: implement build
            return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Scaffold(
                appBar: AppBar(
                title: Text("ctr trial"),
                ),
                body: Body(),
            ),
            );
        }
        }

        class Body extends StatefulWidget {
        @override
        _BodyState createState() => _BodyState();
        }

        class _BodyState extends State<Body> {
        int upactr = 0;
        int ndactr = 0;
        int othctr = 0;

        void _incrementctr(int i) {
            print("pos: $i");
            setState(() {
            if (i == 1) {
                upactr++;
            } else if (i == 2) {
                ndactr++;
            } else {
                othctr++;
            }
            });
        }

        @override
        Widget build(BuildContext context) {
            return Center(
            child: Column(
                //mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                SizedBox(
                    height: 100,
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                    Text("UPA"),
                    IconButton(
                        icon: Icon(
                            Icons.add,
                        ),
                        onPressed: () {
                            _incrementctr(1);
                        }),
                    Text("$upactr")
                    ],
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                    Text("NDA"),
                    IconButton(
                        icon: Icon(
                        Icons.add,
                        ),
                        onPressed: () {
                        _incrementctr(2);
                        },
                    ),
                    Text("$ndactr")
                    ],
                ),
                Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                    Text("others"),
                    IconButton(
                        icon: Icon(
                            Icons.add,
                        ),
                        onPressed: () {
                            _incrementctr(
                                3); // this function should increment only the upactr variable
                        }),
                    Text("$othctr")
                    ],
                ),
                ],
            ),
            );
        }
        }

enter image description here