flutter - Flutter:如何从 map 检索索引(列表)

标签 flutter dart flutter-layout

我目前停留在flutter开发中,需要从map检索索引到Scaffold的其他部分。是否可以从下面的 map 中将索引值检索到脚手架的其他部分( float 操作按钮)?

  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)));
    }
  }

像上面的代码一样,我想从list.map中检索“索引”,并将其传递给buttonTextBuild中的cardIndex。

最佳答案

您可以这样做:

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

文本小部件仅是示例,您可以使用所需的任何小部件或调用传递 list [index] 的函数来获取项目

关于flutter - Flutter:如何从 map 检索索引(列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60754711/

相关文章:

dart - 在 Flutter 中将 Widget 置于 ListView 之上

flutter - 有什么办法可以将大量 Assets 图像显示为 ListView ?

android - 如何解决flutter_local_notifications PlatformException错误?

flutter - Dart Flutter Uint8List

flutter :参数格式不正确

flutter - 如何在 Flutter 中实现 CheckBox?

flutter - 如何解决 Flutter 中的 "No implementation found for method showToast"?

macos - zsh:找不到命令:pub

android - 如何更改 Flutter 应用程序的名称?

flutter - 将状态变量引用传递给函数