限制选择的可见性|ListView
Limiting the visibility of the selection|ListView
我在列中有一个 ListView:
SizedBox(width: 100, height: 100, child: ListView.builder(
shrinkWrap: true,
itemCount: _listHours.length,
itemBuilder: (context, index) {
return ListTile(
title: Center(child: Text(_listHours[index].toString())),
selected: index == _selectedIndexHours,
dense: true,
selectedTileColor: Colors.indigo.withOpacity(0.6),
onTap: () {
setState(() {
_selectedIndexHours = _listHours[index];
});
},
);
}
),),
选中某个项目后,滚动整个屏幕即可看到所选内容。
您遇到了类似于 this one 的问题,其中 ListTile 的装饰呈现在 ListView 之外。目前还没有解决方案,但您可以实施一些解决方法。在这部分代码中,我将 ListTile 的“onTap”参数设置为“null”,并使用 GestureDetector 小部件对其进行包装。像这样:
SizedBox(width: 100, height: 100, child: ListView.builder(
shrinkWrap: true,
itemCount: _listHours.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
_selectedIndexHours = _listHours[index];
});
}
child: ListTile(
title: Center(child: Text(_listHours[index].toString())),
selected: index == _selectedIndexHours,
dense: true,
selectedTileColor: Colors.indigo.withOpacity(0.6),
onTap: null
)
);
}
),
),
希望这对你有用。
我在列中有一个 ListView:
SizedBox(width: 100, height: 100, child: ListView.builder(
shrinkWrap: true,
itemCount: _listHours.length,
itemBuilder: (context, index) {
return ListTile(
title: Center(child: Text(_listHours[index].toString())),
selected: index == _selectedIndexHours,
dense: true,
selectedTileColor: Colors.indigo.withOpacity(0.6),
onTap: () {
setState(() {
_selectedIndexHours = _listHours[index];
});
},
);
}
),),
选中某个项目后,滚动整个屏幕即可看到所选内容。
您遇到了类似于 this one 的问题,其中 ListTile 的装饰呈现在 ListView 之外。目前还没有解决方案,但您可以实施一些解决方法。在这部分代码中,我将 ListTile 的“onTap”参数设置为“null”,并使用 GestureDetector 小部件对其进行包装。像这样:
SizedBox(width: 100, height: 100, child: ListView.builder(
shrinkWrap: true,
itemCount: _listHours.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
_selectedIndexHours = _listHours[index];
});
}
child: ListTile(
title: Center(child: Text(_listHours[index].toString())),
selected: index == _selectedIndexHours,
dense: true,
selectedTileColor: Colors.indigo.withOpacity(0.6),
onTap: null
)
);
}
),
),
希望这对你有用。