在Flutter中滚动重叠的瓷砖出现问题。

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

Problem in scrolling overlapping tiles in flutter

问题

我使用Flutter构建应用程序,其中有一个带有listView.builder的页面。我希望这些瓷砖具有一些重叠效果,因此我使用了Align。当我滚动时,它可以正常工作,但是当我向相反方向滚动时,超出屏幕的瓷砖的重叠效果会反转。这是发生的情况的视频。
请注意滚动回去时屏幕顶部的部分。

这是我的代码:

HomeScreen:

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: SizedFABWidget(),
      body: SafeArea(
        child: ListView.builder(
          itemCount: 5,
          itemBuilder: (context, index) {
            return const CardListViewWidget();
          },
        ),
      ),
    );
  }
}

CardListViewWidget:

class CardListViewWidget extends StatelessWidget {
  final List<String> test = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12'
  ];

  CardListViewWidget({Key? key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: Get.width,
      height: kBottomListHeight,
      child: ListView.builder(
          itemCount: test.length,
          itemBuilder: (context, index) {
            return AlignWidget(
              list: test,
              index: index,
            );
          }),
    );
  }
}

AlignWidget:

class AlignWidget extends StatelessWidget {
  final int index;
  final List list;

  const AlignWidget({required this.index, required this.list, Key? key});

  @override
  Widget build(BuildContext context) {
    return Align(
      heightFactor: 0.28,
      alignment: Alignment.topCenter,
      child: SizedBox(
        height: kHeight * 0.4,
        child: ListTile(
          shape: RoundedRectangleBorder(
            side: const BorderSide(color: Colors.black12),
            borderRadius: kBorderRadius45,
          ),
          tileColor: getColor(index),
        ),
      ),
    );
  }
}

如果需要其他信息,请告诉我,我会更新此帖子。
提前感谢您。

我几乎尝试了互联网和人工智能能找到的一切,但都无法解决这个问题,我也不太明白为什么会发生这种情况。我尝试了Stack和Positioned,Transform.translate以及其他一些方法,但它们都无法解决我的问题。我希望我的重叠瓷砖列表不改变位置,不会在后面或前面,只会保持与视频前几秒钟相同的位置。

英文:

I am building an application with flutter and I have a page with a listView.builder. I wanted the tiles to have some overlapping effect so I used Align. It works fine when I scroll but when I scroll in the opposite direction, the overlapping effect reverses for the tiles that have been out of the screen.
this is a video of what's happening.
pay attention to the top part of the screen when I scroll back.
在Flutter中滚动重叠的瓷砖出现问题。

this is my code:

HomeScreen:

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: SizedFABWidget(),
      body: SafeArea(
        child: ListView.builder(
          itemCount: 5,
          itemBuilder: (context, index) {
            return const CardListViewWidget();
          },
        ),
      ),
    );
  }
}

CardListViewWidget:


class CardListViewWidget extends StatelessWidget {
  final List&lt;String&gt; test = [
    &#39;1&#39;,
    &#39;2&#39;,
    &#39;3&#39;,
    &#39;4&#39;,
    &#39;5&#39;,
    &#39;6&#39;,
    &#39;7&#39;,
    &#39;8&#39;,
    &#39;9&#39;,
    &#39;10&#39;,
    &#39;11&#39;,
    &#39;12&#39;,
    &#39;2&#39;,
    &#39;3&#39;,
    &#39;4&#39;,
    &#39;5&#39;,
    &#39;6&#39;,
    &#39;7&#39;,
    &#39;8&#39;,
    &#39;9&#39;,
    &#39;10&#39;,
    &#39;11&#39;,
    &#39;12&#39;
  ];

  CardListViewWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: Get.width,
      height: kBottomListHeight,
      child: ListView.builder(
          itemCount: test.length,
          itemBuilder: (context, index) {
            return AlignWidget(
              list: test,
              index: index,
            );
          }),
    );
  }
}

AlignWidget:

class AlignWidget extends StatelessWidget {
  final int index;
  final List list;

  const AlignWidget({required this.index, required this.list, super.key});

  @override
  Widget build(BuildContext context) {
    return Align(
      heightFactor: 0.28,
      alignment: Alignment.topCenter,
      child: SizedBox(
        height: kHeight * 0.4,
        child: ListTile(
          shape: RoundedRectangleBorder(
            side: const BorderSide(color: Colors.black12),
            borderRadius: kBorderRadius45,
          ),
          tileColor: getColor(index),
        ),
      ),
    );
  }
}

If anything else is needed please let me know and I'll update this post.
thank you in advance.

I almost tried everything I could find on internet and AIs but couldn't solve it, I also don't really understand why this is happening.
I tried Stack and Positioned, Transform.translate and a couple other things but they couldn't solve my problem.
I want my list with overlapping tiles to not change position and don't get behind or come forward, just stay the same as the first few seconds of the video.

答案1

得分: 1

注意: 这只是一个临时解决方法,你遇到的问题是部件被重新构建。下面的解决方案对于大型列表来说并不是一个高效的解决方案。

  • 主屏幕
class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: SizedFABWidget(),
      body: SingleChildScrollView(
        child: Column(
          children: [for (int i = 0; i &lt; 6; i++) CardListViewWidget()],
        ),
      ),
    );
  }
}
  • CardListViewWidget
class CardListViewWidget extends StatelessWidget {
  final List&lt;String&gt; test = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '10',
    '11',
    '12'
  ];

  CardListViewWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: Get.width,
      height: kBottomListHeight,
      child: ListView.builder(
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemCount: test.length,
          itemBuilder: (context, index) {
            return AlignWidget(
              list: test,
              index: index,
            );
          }),
    );
  }
}

希望对你有所帮助:)

英文:

Note: This is just a hack and the problem you are facing is that the widgets are being rebuilt. The below solution is not an efficient solution for large lists.

  • Home Screen

     class HomeScreen extends StatelessWidget {
         const HomeScreen({super.key});
    
         @override
         Widget build(BuildContext context) {
           return Scaffold(
             floatingActionButton: SizedFABWidget(),
             body: SingleChildScrollView(
               child: Column(
                 children: [for (int i = 0; i &lt; 6; i++) CardListViewWidget()],
               ),
             ),
           );
         }
       }
    
  • CardListViewWidget

       class CardListViewWidget extends StatelessWidget {
         final List&lt;String&gt; test = [
           &#39;1&#39;,
           &#39;2&#39;,
           &#39;3&#39;,
           &#39;4&#39;,
           &#39;5&#39;,
           &#39;6&#39;,
           &#39;7&#39;,
           &#39;8&#39;,
           &#39;9&#39;,
           &#39;10&#39;,
           &#39;11&#39;,
           &#39;12&#39;,
           &#39;2&#39;,
           &#39;3&#39;,
           &#39;4&#39;,
           &#39;5&#39;,
           &#39;6&#39;,
           &#39;7&#39;,
           &#39;8&#39;,
           &#39;9&#39;,
           &#39;10&#39;,
           &#39;11&#39;,
           &#39;12&#39;
         ];
    
         CardListViewWidget({super.key});
    
         @override
         Widget build(BuildContext context) {
           return SizedBox(
             width: Get.width,
             height: kBottomListHeight,
             child: ListView.builder(
                 shrinkWrap: true,
                 physics: const NeverScrollableScrollPhysics(),
                 itemCount: test.length,
                 itemBuilder: (context, index) {
                   return AlignWidget(
                     list: test,
                     index: index,
                   );
                 }),
           );
         }
       }    
    

I hope this helps:)

huangapple
  • 本文由 发表于 2023年7月3日 15:19:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602614.html
匿名

发表评论

匿名网友

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

确定