滚动时列出容器外的图块

发布于 2025-01-11 14:20:05 字数 1737 浏览 0 评论 0原文

当我滚动列表图块时,我的 UI 中的容器内有一个 ListViewBuilder,如下所示:

在此处输入图像描述

这是我的代码:


 return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
      child: Container(

        margin: const EdgeInsets.only(
            left: 30, right: 30, bottom: 20),
        width: getProportionateScreenWidth(600),
        height: getProportionateScreenHeight(300),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(17),
              topRight: Radius.circular(17),
              bottomLeft: Radius.circular(17),
              bottomRight: Radius.circular(17)),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,

          children: [


    

       Expanded(child: ListView.builder(
           shrinkWrap: true,
           physics: const ScrollPhysics(),
           itemCount: snapshot.data!.perimeters.length,
           itemBuilder: (context,index){
             return PerimListTile(
               perimID: snapshot.data!.perimeters[index].perimeterId.toString(),
               perimLabel: snapshot.data!.perimeters[index].label,



             );
           })),



      ],




        ),
      ),
    )

我希望列表图块即使在滚动时也保留在容器内,如果有人知道如何解决这个问题我将不胜感激,提前谢谢您。

I have a ListViewBuilder inside a container in my UI when I scroll the list tiles get out of the container like so :

enter image description here

This my code :


 return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),
      child: Container(

        margin: const EdgeInsets.only(
            left: 30, right: 30, bottom: 20),
        width: getProportionateScreenWidth(600),
        height: getProportionateScreenHeight(300),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(17),
              topRight: Radius.circular(17),
              bottomLeft: Radius.circular(17),
              bottomRight: Radius.circular(17)),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,

          children: [


    

       Expanded(child: ListView.builder(
           shrinkWrap: true,
           physics: const ScrollPhysics(),
           itemCount: snapshot.data!.perimeters.length,
           itemBuilder: (context,index){
             return PerimListTile(
               perimID: snapshot.data!.perimeters[index].perimeterId.toString(),
               perimLabel: snapshot.data!.perimeters[index].label,



             );
           })),



      ],




        ),
      ),
    )

I want the list tiles to stay inside the container even while scrolling , if anyone knows how to solve the issue I'd be grateful , thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2025-01-18 14:20:05

问题

我不知道您使用的 getProportionateScreenHeight 的详细信息,但我假设它返回一个双精度值。
Column 内的 ListView 受到容器高度的限制。

解决方案

删除容器的高度并在 Column 上尝试 mainAxisSize: MainAxisSize.min

Problem

I do not know the details about getProportionateScreenHeight you used but I assume that it returns a double value.
The ListView inside the Column is constrained by the height of the container through that.

Solution

Remove the height of the container and try mainAxisSize: MainAxisSize.min on Column.

初见 2025-01-18 14:20:05

解决

[1]在容器Widget中使用剪辑行为参数。并将其设置为hardEdge

clipBehavior:Clip.hardEdge,

[2] 用 Card 小部件包裹 ListTile 小部件,使卡片位于 Container 内。

卡片(子项:ListTile());

演示
import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(MyHome());
}

class MyHome extends StatelessWidget {
  const MyHome({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Ans(),
    );
  }
}

class Ans extends StatefulWidget {
  const Ans({super.key});

  @override
  State<Ans> createState() => _AnsState();
}

class _AnsState extends State<Ans> {
  List<String> charName = [
    'Rio',
    'Tokyo',
    'Berlin',
    'Stockhome',
    'Lisbon',
    'Sergio',
    'Martin'
  ];
  List<String> charGender = [
    'male',
    'female',
    'male',
    'female',
    'female',
    'male',
    'male'
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Tile Wrapper"),
      ),
      body: SizedBox(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 100.0),
              child: Container(
                height: 400,
                width: 300,

                /// Clipping the inner View Condition
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20.2),
                  border: Border.all(color: Colors.black),
                  color: Colors.amberAccent,
                ),
                child: ListView.builder(
                    itemCount: charGender.length,
                    itemBuilder: ((BuildContext context, int index) {
                      return Padding(
                        padding: EdgeInsets.only(top: 20.0),

                        //// Wrap the ListTile with Card Widget...
                        child: Card(
                          child: ListTile(
                            title: Text(
                              charName[index],
                              style: TextStyle(fontSize: 18.0),
                            ),
                            subtitle: Text(
                              charGender[index],
                              style: TextStyle(fontSize: 18.0),
                            ),
                            tileColor: Colors.brown,
                            leading: CircleAvatar(
                                radius: 20, child: Icon(Icons.person_rounded)),
                          ),
                        ),
                      );
                    })),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

示例

查看解决方案

Solve

[1] Use the clip behavior parameter in container Widget. And set it to hardEdge

clipBehavior: Clip.hardEdge,

[2] Wrap the ListTile Widget with Card widget to make the cards inside the Container.

Card(child: ListTile());

Demo
import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(MyHome());
}

class MyHome extends StatelessWidget {
  const MyHome({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Ans(),
    );
  }
}

class Ans extends StatefulWidget {
  const Ans({super.key});

  @override
  State<Ans> createState() => _AnsState();
}

class _AnsState extends State<Ans> {
  List<String> charName = [
    'Rio',
    'Tokyo',
    'Berlin',
    'Stockhome',
    'Lisbon',
    'Sergio',
    'Martin'
  ];
  List<String> charGender = [
    'male',
    'female',
    'male',
    'female',
    'female',
    'male',
    'male'
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Tile Wrapper"),
      ),
      body: SizedBox(
        height: double.infinity,
        width: double.infinity,
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 100.0),
              child: Container(
                height: 400,
                width: 300,

                /// Clipping the inner View Condition
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20.2),
                  border: Border.all(color: Colors.black),
                  color: Colors.amberAccent,
                ),
                child: ListView.builder(
                    itemCount: charGender.length,
                    itemBuilder: ((BuildContext context, int index) {
                      return Padding(
                        padding: EdgeInsets.only(top: 20.0),

                        //// Wrap the ListTile with Card Widget...
                        child: Card(
                          child: ListTile(
                            title: Text(
                              charName[index],
                              style: TextStyle(fontSize: 18.0),
                            ),
                            subtitle: Text(
                              charGender[index],
                              style: TextStyle(fontSize: 18.0),
                            ),
                            tileColor: Colors.brown,
                            leading: CircleAvatar(
                                radius: 20, child: Icon(Icons.person_rounded)),
                          ),
                        ),
                      );
                    })),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Sample

View Solution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文