如何创建一个占用所有可用空间的可滚动列表?

huangapple go评论95阅读模式
英文:

How can I create a scrollable list that takes all available space?

问题

以下是已翻译的部分代码:

  1. 我尝试创建一个可滚动的列表,它会自动扩展以占用可用空间。
  2. 以下是我的实际代码,它可以工作,但指定了固定高度为610
  3. import 'package:flutter/material.dart';
  4. class MyWidget extends StatelessWidget {
  5. final List<String> _myTable;
  6. const MyWidget(this._myTable);
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. appBar: AppBar(
  11. title: const Text('Stackoverflow示例'),
  12. ),
  13. body: Container(
  14. color: const Color.fromRGBO(33, 33, 33, 0.2),
  15. child: SingleChildScrollView(
  16. child: Column(
  17. crossAxisAlignment: CrossAxisAlignment.stretch,
  18. children: [
  19. Container(
  20. height: 610,
  21. color: const Color.fromRGBO(33, 33, 33, 0.2),
  22. child: _myTable.isEmpty
  23. ? const Center(
  24. child: Text('加载表格...'),
  25. )
  26. : ListView.builder(
  27. itemBuilder: (ctx, index) {
  28. return Text(_myTable[index]);
  29. },
  30. itemCount: _myTable.length,
  31. ),
  32. ),
  33. ],
  34. ),
  35. ),
  36. ),
  37. );
  38. }
  39. }

如果删除 height: 610, 这一行,您将收到以下错误:

以下是错误信息的一部分:

"The following RenderObject was being processed when the exception was fired: RenderViewport#827e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE..."

您的想法是,如果我创建一个具有固定高度610的列表,如果将其移至另一台设备,可能会崩溃或显示不正常... 那么如何使可滚动列表占用所有可用高度呢?

英文:

I'm trying to make a scrollable list that automatically expands taking the available space.

Here is my actual code which is working but specifying a fixed height of 610:

  1. import &#39;package:flutter/material.dart&#39;;
  2. class MyWidget extends StatelessWidget {
  3. final List&lt;String&gt; _myTable;
  4. const MyWidget(this._myTable);
  5. @override
  6. Widget build(BuildContext context) {
  7. return Scaffold(
  8. appBar: AppBar(
  9. title: const Text(&#39;Stackoverflow example&#39;),
  10. ),
  11. body: Container(
  12. color: const Color.fromRGBO(33, 33, 33, 0.2),
  13. child: SingleChildScrollView(
  14. child: Column(
  15. crossAxisAlignment: CrossAxisAlignment.stretch,
  16. children: [
  17. Container(
  18. height: 610,
  19. color: const Color.fromRGBO(33, 33, 33, 0.2),
  20. child: _myTable.isEmpty
  21. ? const Center(
  22. child: Text(&#39;Loading table...&#39;),
  23. )
  24. : ListView.builder(
  25. itemBuilder: (ctx, index) {
  26. return Text(_myTable[index]);
  27. },
  28. itemCount: _myTable.length,
  29. ),
  30. ),
  31. ],
  32. ),
  33. ),
  34. ),
  35. );
  36. }
  37. }

If I remove that height: 610, line I will get the following error:

  1. The following RenderObject was being processed when the exception was fired: RenderViewport#827e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  2. RenderObject: RenderViewport#827e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  3. needs compositing
  4. parentData: &lt;none&gt; (can use size)
  5. constraints: BoxConstraints(w=411.4, 0.0&lt;=h&lt;=Infinity)
  6. size: MISSING
  7. axisDirection: down
  8. crossAxisDirection: right
  9. offset: ScrollPositionWithSingleContext#2ab34(offset: 0.0, range: null..null, viewport: null, ScrollableState, AlwaysScrollableScrollPhysics -&gt; ClampingScrollPhysics -&gt; RangeMaintainingScrollPhysics, IdleScrollActivity#6a07b, ScrollDirection.idle)
  10. anchor: 0.0
  11. center child: RenderSliverPadding#4985e NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  12. parentData: paintOffset=Offset(0.0, 0.0)
  13. constraints: MISSING
  14. geometry: null
  15. padding: EdgeInsets.zero
  16. textDirection: ltr
  17. child: RenderSliverList#33a39 NEEDS-LAYOUT NEEDS-PAINT
  18. parentData: paintOffset=Offset(0.0, 0.0)
  19. constraints: MISSING
  20. geometry: null
  21. no children current live
  22. ════════════════════════════════════════════════════════════════════════════════
  23. ════════ Exception caught by rendering library ═════════════════════════════════
  24. RenderBox was not laid out: RenderViewport#827e1 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
  25. &#39;package:flutter/src/rendering/box.dart&#39;:
  26. box.dart:1
  27. Failed assertion: line 2009 pos 12: &#39;hasSize&#39;
  28. The relevant error-causing widget was
  29. ListView

And that error is pointing to this line of my code:

  1. : ListView.builder(

My idea is that if I create a list with a fixed height of 610 if I move it to another device it will crash or look bad... so how can I make the scrollable list to take all available height?

答案1

得分: 1

以下是代码的翻译部分:

  1. 考虑使用CustomScrollView,它可以帮助您解决问题,查看下面的代码
  2. main.dart
  3. import 'package:flutter/material.dart';
  4. void main() {
  5. runApp(const MyApp());
  6. }
  7. class MyApp extends StatelessWidget {
  8. const MyApp({super.key});
  9. @override
  10. Widget build(BuildContext context) {
  11. return const MaterialApp(
  12. title: "My App",
  13. home: HomePage(),
  14. );
  15. }
  16. }
  17. class HomePage extends StatelessWidget {
  18. const HomePage({super.key});
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. body: CustomScrollView(
  23. slivers: [
  24. SliverAppBar(
  25. title: const Text("My super App")
  26. ),
  27. SliverToBoxAdapter(
  28. child: Container(
  29. alignment: Alignment.center,
  30. height: 100,
  31. color: Colors.redAccent,
  32. child: Text("Header view"),
  33. ),
  34. ),
  35. SliverList(delegate: SliverChildListDelegate(
  36. List.generate(20, (idx) {
  37. return Padding(
  38. padding: const EdgeInsets.all(10),
  39. child: Card(
  40. child: ListTile(
  41. leading: const Icon(Icons.check_circle),
  42. title: Text("Item $idx"),
  43. ),
  44. ),
  45. );
  46. })
  47. ),
  48. ),
  49. ],
  50. ),
  51. );
  52. }
  53. }

希望这能帮助您理解代码的内容。如果有其他问题,请随时提出。

英文:

Think about using the CustomScrollView which can help you solve your problem, see the code below

main.dart

  1. import &#39;package:flutter/material.dart&#39;;
  2. void main() {
  3. runApp(const MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. @override
  8. Widget build(BuildContext context) {
  9. return const MaterialApp(
  10. title: &quot;My App&quot;,
  11. home: HomePage(),
  12. );
  13. }
  14. }
  15. class HomePage extends StatelessWidget {
  16. const HomePage({super.key});
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. body: CustomScrollView(
  21. slivers: [
  22. SliverAppBar(
  23. title: const Text(&quot;My super App&quot;)
  24. ),
  25. SliverToBoxAdapter(
  26. child: Container(
  27. alignment: Alignment.center,
  28. height: 100,
  29. color: Colors.redAccent,
  30. child: Text(&quot;Header view&quot;),
  31. ),
  32. ),
  33. SliverList(delegate: SliverChildListDelegate(
  34. List.generate(20, (idx) {
  35. return Padding(
  36. padding: const EdgeInsets.all(10),
  37. child: Card(
  38. child: ListTile(
  39. leading: const Icon(Icons.check_circle),
  40. title: Text(&quot;Item $idx&quot;),
  41. ),
  42. ),
  43. );
  44. })
  45. ),
  46. ),
  47. ],
  48. ),
  49. );
  50. }
  51. }

huangapple
  • 本文由 发表于 2023年4月10日 20:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定