栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何获取列表Flutter中所选项目的索引/键?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何获取列表Flutter中所选项目的索引/键?

您可能希望

ListTile
在内构建的列表
ListView
,并使用
List.generate
构造函数来获取的索引,
children
这是一个简单的示例:

import "package:flutter/material.dart";class ListTest extends StatefulWidget {  @override  _ListTestState createState() => new _ListTestState();}class _ListTestState extends State<ListTest> {  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();  int _id;  @override  Widget build(BuildContext context) {    return new Scaffold(      key: _scaffoldKey,      appBar: new AppBar(title: new Text("List"),),      body: new ListView(          children: new List.generate(10, (int index){ return new ListTile(title: new Text("item#$index"), onTap:(){   setState((){     _id = index; //if you want to assign the index somewhere to check   });   _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("You clicked item number $_id"))); }, );          })      ),    );  }}

请记住,这种方法

List.generate
在小列表上也能很好地工作,如果您正在阅读可扩展列表(例如,用户列表),则需要使用
ListView.builder
而不是一个
ListView
带有
builder
参数的参数,该参数也允许您按索引遍历列表。

new ListView.builder(itemBuilder: (BuildContext context, int index) {        //return your list      },


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号