Флаттер: Как получить индекс с карты (Список) - PullRequest
0 голосов
/ 19 марта 2020

В настоящее время я застрял в разработке флаттера, где мне нужно извлечь индекс с карты в другие части скаффолда. Можно ли извлечь значение индекса из карты ниже в другие части Scaffold (кнопка с плавающим действием)?

  Iterable<int> get positiveIntegers sync* {
    int i = articleList.length - 1;
    while (true) yield i--;
  }

  @override
  Widget build(BuildContext context) {
    int getLength = articleList.length;
    var list = positiveIntegers.take(getLength).toList();

    return Scaffold(
      appBar: AppBar(
        title: new Text(
          'Popular',
          textAlign: TextAlign.center,
        ),
        textTheme: TextTheme(
            title: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w700,
                color: Colors.black)),
        backgroundColor: Colors.white,
        centerTitle: true,
        elevation: 0.0,
      ),
      body: SwipeStack(
          children: list.map((index) {
              ...
          }
      ),
      floatingActionButton: Container(
        margin: EdgeInsets.only(top: 25, bottom: 25, left: 20),
        alignment: Alignment(0, 1),
        child: new Row(
          children: <Widget>[
            RawMaterialButton(
              onPressed: () {
                if (isOutOfCards == true) {
                  setState(() {
                    isOutOfCards = false;
                  });
                }
              },
              child: iconBuild(context),
              shape: new CircleBorder(),
              fillColor: Colors.red[700],
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              padding: const EdgeInsets.all(11),
            ),
            buttonTextBuild(context),
          ],
        ),
      ),
    );
  }

  Widget buttonTextBuild(BuildContext context) {
    if (isOutOfCards == false) {
      return Container(
          child: Text('$cardIndex/${articleList.length}',
              textAlign: TextAlign.left,
              style: TextStyle(
                  fontSize: 12.75,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 1)));
    } else {
      return Container(
          child: Text('Start Again',
              textAlign: TextAlign.left,
              style: TextStyle(
                  fontSize: 12.75,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 1)));
    }
  }

Как и в коде выше, я хочу получить индекс из списка.map и передать его на карточку index внутри buttonTextBuild.

Ответы [ 2 ]

0 голосов
/ 20 марта 2020

Вы можете использовать функцию enumerate из пакета quiver :

children: enumerate(list).map((indexedValue) {
  var index = indexedValue.index;
  var value = indexedValue.value;
  ...
}),
0 голосов
/ 20 марта 2020

Вы можете сделать это следующим образом:

            children: [
              for(int index = 0; index < list.length; index++)
                Text(list[index])
            ]

Текстовый виджет является просто примером, вы можете использовать любой виджет, который вам нужен, или вызывать функцию, передавая list [index] , чтобы получить элемент

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...