使用参数(Flutter)的每个Grid View的增量计数器

时间:2019-05-19 17:48:15

标签: dart flutter

我有一个带有参数的小部件,该参数可创建我想要的名称或所需的渐变网格,我添加了一个新参数以发送变量以在按下按钮或减小按钮时对其进行递增。有一个问题,当我调用增量并将其停留在一个数字上而降低一个数字时,增量将不起作用,但是如果我将小部件中的“计数器”更改为参数而不是onPressed而不是参数,而是将小部件中的“计数器”更改为,则此方法有效在所有网格视图中,计数器将变为一个变量。

https://imgur.com/h1C89dk“截屏”

这是代码: 这是变量

 int thoub = 0,
  shirt = 0,
  longPants = 0,
  shortPants = 0,
  shumgnGutr = 0,
  socks = 0,
  bsht = 0,
  tShirt = 0,
  miltUniform = 0,
  pakisUniform = 0,
  singleBlankets = 0,
  doubleBlankets = 0,
  abbya = 0;

当我调用窗口小部件时:

myGridItems(
        'ثياب', //Arabic
        Colors.deepPurple.withOpacity(0.2),
        Colors.grey[300].withOpacity(0.35),
        thoub,
      ),
myGridItems(
        'فنايل', //Arabic
        Colors.deepPurple.withOpacity(0.2),
        Colors.grey[300].withOpacity(0.35),
        shirt,
      ),

这是小部件本身:

Widget myGridItems(String gridName, Color color1, Color color2, int counter) {
return Container(
  decoration: BoxDecoration(
    gradient: LinearGradient(colors: [
      color1,
      color2,
    ], begin: Alignment.topLeft, end: new Alignment(1.0, 1.0)),
  ),
  child: Stack(
    children: <Widget>[
      Opacity(
        opacity: 0.3,
        child: Container(
          decoration: BoxDecoration(),
        ),
      ),
      Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              FlatButton(
                onPressed: () {
                  setState(() {
                    if (counter != 50) counter++;
                    debugPrint('$counter');
                    print('add on pressed');
                  });
                },
                child: Icon(
                  Icons.add,
                  color: Colors.green,
                  size: 17.0,
                ),
              ),
              Text('$counter',
                  style: TextStyle(fontSize: 18.0, color: Colors.blueGrey)),
              FlatButton(
                onPressed: () {
                  setState(() {
                    if (counter != 0) counter--;
                    debugPrint('$counter');
                    print('minus on pressed');
                  });
                },
                child: Icon(
                  const IconData(0xe15b, fontFamily: 'MaterialIcons'),
                  color: Colors.red,
                  size: 17.0,
                ),
              ),
              Column(
                children: <Widget>[
                  Container(
                    width: MediaQuery.of(context).size.width / 2,
                    child: Padding(
                      padding:
                          const EdgeInsets.fromLTRB(0.0, 10.0, 15.0, 0.0),
                      child: Text(
                        gridName,
                        style: TextStyle(
                          color: Colors.black54,
                          fontSize: 20.0,
                          fontWeight: FontWeight.bold,
                        ),
                        textAlign: TextAlign.right,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ],
      )
    ],
  ),
);
  }

这是终点站

I/flutter (17441): 1
I/flutter (17441): add on pressed
I/flutter (17441): 0
I/flutter (17441): minus on pressed
I/flutter (17441): 1
I/flutter (17441): add on pressed
I/flutter (17441): 1
I/flutter (17441): add on pressed

1 个答案:

答案 0 :(得分:0)

根据您上面的问题,您无法更新商品数。我在此处添加了全局变量以增加和减少商品数。

 import 'package:flutter/material.dart';

class Items extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ItemsState();
  }
}

class _ItemsState extends State<Items> {
  int thoub = 1;
  int counterMain = 0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.builder(
    itemCount: thoub,
    itemBuilder: (context, index) {
    return myListItems('ثياب', //Arabic
      Colors.deepPurple.withOpacity(0.2),
      Colors.grey[300].withOpacity(0.35),
      counterMain,);
    });
  }


  Widget myListItems(String gridName, Color color1, Color color2, int counter) {

    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
          color1,
          color2,
        ], begin: Alignment.topLeft, end: new Alignment(1.0, 1.0)),
      ),
      child: Stack(
        children: <Widget>[
          Opacity(
            opacity: 0.3,
            child: Container(
              decoration: BoxDecoration(),
            ),
          ),
          Column(
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  FlatButton(

                    onPressed: () {
                      setState(() {

                        if (counter <50)
                          {
                            counterMain = counter+1;

                          }else{
                          debugPrint('in add else');

                        }
                        debugPrint('$counterMain');
                        print('add on pressed');
                      });
                    },
                    child: Icon(
                      Icons.add,
                      color: Colors.green,
                      size: 17.0,
                    ),
                  ),
                  Text('$counterMain',
                      style: TextStyle(fontSize: 18.0, color: Colors.blueGrey)),
                  FlatButton(
                    onPressed: () {
                      setState(() {
                        if (counter > 0) {
                          counterMain= counter-1;
                        }
                        debugPrint('$counterMain');
                        print('minus on pressed');
                      });
                    },
                    child: Icon(
                      const IconData(0xe15b, fontFamily: 'MaterialIcons'),
                      color: Colors.red,
                      size: 17.0,
                    ),
                  ),
                  Column(
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width / 2,
                        child: Padding(
                          padding:
                          const EdgeInsets.fromLTRB(0.0, 10.0, 15.0, 0.0),
                          child: Text(
                            gridName,
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: 20.0,
                              fontWeight: FontWeight.bold,
                            ),
                            textAlign: TextAlign.right,
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ],
          )
        ],
      ),
    );
  }
}

输出-

I/flutter ( 4708): 1  
I/flutter ( 4708): add on pressed  
I/flutter ( 4708): 2  
I/flutter ( 4708): add on pressed  
I/flutter ( 4708): 3  
I/flutter ( 4708): add on pressed 
I/flutter ( 4708): 2  
I/flutter ( 4708): minus on pressed 
I/flutter ( 4708): 1 
I/flutter ( 4708): minus on pressed 
I/flutter ( 4708): 0 
I/flutter ( 4708): minus on pressed