Как получить положение GridTile в флаттере GridView? - PullRequest
0 голосов
/ 30 июня 2018

Я использую GridTile для создания каждого элемента в GridView. Каждый элемент создается динамически из списка данных. Мне нужно выяснить, по какому элементу щелкнули, получив ключ или индекс. Пожалуйста, кто-нибудь, предложите метод?

Это мой код,

 new GridView.count(
                    shrinkWrap: true,
                    primary: false,
                    childAspectRatio: 2.0,
                    crossAxisCount: 3,
                    mainAxisSpacing: 10.0,
                    children: _kCategoryTiles.map((_FilterItemTile item) {
                      return new GridTile(
                        child: new Container(
                          alignment: Alignment.center,
                          decoration: new BoxDecoration(
                            shape: BoxShape.rectangle,
                            borderRadius: new BorderRadius.all(
                                new Radius.elliptical(10.0, 20.0)),
                            border: new Border.all(
                              color: const Color(0xFF33b17c),
                            ),
                          ),
                          margin: new EdgeInsets.all(10.0),
                          child: new Text(
                            item.itemName,
                            textAlign: TextAlign.center,
                            style: new TextStyle(
                              color: Colors.black,
                              fontSize: 12.0,
                              fontFamily: 'helvetica_neue_medium',
                              letterSpacing: 0.59,
                            ),
                          ),
                        ),
                      );
                    }).toList(),
                  )

Заранее спасибо.

1 Ответ

0 голосов
/ 30 июня 2018

Вы можете обернуть свой GridTile в FlatButton , который дает вам свойство onPressed. Там вы можете получить доступ к вашему _FilterItemTile item:

return new GridView.count(
    shrinkWrap: true,
    primary: false,
    childAspectRatio: 2.0,
    crossAxisCount: 3,
    mainAxisSpacing: 10.0,
    children: _kCategoryTiles.map((_FilterItemTile item) {
        return new FlatButton(
    onPressed: () {
        // add code here
        print(item);
        someFunction(item);
    },
    child: new GridTile(
        child: new Container(
            alignment: Alignment.center,
            decoration: new BoxDecoration(
            shape: BoxShape.rectangle,
            borderRadius: new BorderRadius.all(
                new Radius.elliptical(10.0, 20.0)),
            border: new Border.all(
                color: const Color(0xFF33b17c),
            ),
            ),
            margin: new EdgeInsets.all(10.0),
            child: new Text(
            item.itemName,
            textAlign: TextAlign.center,
            style: new TextStyle(
                color: Colors.black,
                fontSize: 12.0,
                fontFamily: 'helvetica_neue_medium',
                letterSpacing: 0.59,
            ),
            ),
        ),
        );
        )
    }).toList(),
),
...