解决Flutter的ListView嵌套ListView滑动冲突以及无限高度问题

小灰灰 2021-04-19 14:19 1370阅读 0赞

关键代码

  1. shrinkWrap: true, //解决无限高度问题
  2. physics:NeverScrollableScrollPhysics(),//禁用滑动事件
  3. 复制代码

可滑动控件都有这两个属性,给子滑动部件设置上这两个参数,即可办到子ListView跟随父ListView滑动。

demo代码

  1. import 'package:flutter/material.dart';
  2. void main() => runApp(MyApp());
  3. class MyApp extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return MaterialApp(
  7. title: 'list嵌套',
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text('滚动嵌套'),
  11. ),
  12. body: ListView(
  13. children: <Widget>[
  14. ListList(10,Colors.deepPurpleAccent),
  15. ListList(20,Colors.red),
  16. ],
  17. )),
  18. );
  19. }
  20. }
  21. class ListList extends StatelessWidget {
  22. int length = 0; //列表长度
  23. Color color;//文字颜色
  24. ListList(this.length, this.color);
  25. @override
  26. Widget build(BuildContext context) {
  27. return ListView.separated(
  28. shrinkWrap: true,
  29. physics:NeverScrollableScrollPhysics(),
  30. itemBuilder: (context, index) => Text(
  31. 'item. $index',
  32. style: TextStyle(color: color),
  33. ),
  34. separatorBuilder: (context, index) => Divider(
  35. color: color,
  36. ),
  37. itemCount: length);
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,1370人围观)

还没有评论,来说两句吧...

相关阅读