使用参数(Flutter)为每个网格视图增量计数器



我有一个带有参数的小部件,可以创建我想要喜欢的名称或我想要的梯度的网格,我添加了一个新参数来发送一个变量以在我按下按钮或减少它。有一个问题,当我调用它时,增量不起作用,并将一个数字更高,一个较低的数字,但是如果我更改窗口小部件中的"计数器",而不是参数,而是将其定为下面的任何变量,而是将其置于下面的任何变量中,而是可行在所有网格视图中,计数器都会更改为一个变量。

https://i.stack.imgur.com/xvmpt.jpg" screenshot"

这是代码:这是变量

 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

根据您的上述问题,您无法更新项目计数。我在此处添加了一个全局变量以增加和减少项目数量。

 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

最新更新