【问题标题】:Column of Listtile with rounded corner at beginning and endListtile 的列,开头和结尾都带有圆角
【发布时间】:2023-03-29 05:46:01
【问题描述】:

我正在尝试创建一列顶部和底部角为圆角的列表图块。有些瓷砖在悬停时会改变颜色,而另一些则不会。因此,我尝试了两件事:

  • 创建一个带圆角的容器,在里面添加一个带有我的 listTiles 的 Column。
  • 使用我的列表图块创建一列,并为第一个和最后一个元素添加特定的圆角计数器。
  • 为容纳所有元素的容器着色。那么问题是listtiles的悬停颜色被绘制在容器的颜色之下。

遗憾的是,在这两种情况下,列表图块的背景颜色都从圆形容器中溢出,如下图所示:

这是第二次尝试的示例代码:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  final children = List<Widget>.generate(
      5,
      (i) => ListTile(
          tileColor: Colors.green, hoverColor: Colors.red, title: Text('$i')));

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
        children.length,
        (index) {
          if (index == 0 && index == children.length - 1) {
            return Ink(
              // clipBehavior: Clip.antiAlias,
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: BorderRadius.circular(20),
                color: Colors.blue,
              ),
              child: children[index],
            );
          }
          if (index == 0) {
            return Container(
              clipBehavior: Clip.antiAlias,
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20)),
                color: Colors.blue,
              ),
              child: children[index],
            );
          } else if (index == children.length - 1) {
            return Container(
              clipBehavior: Clip.antiAlias,
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: const BorderRadius.only(
                    bottomLeft: Radius.circular(20),
                    bottomRight: Radius.circular(20)),
                color: Colors.blue,
              ),
              child: children[index],
            );
          }

          return children[index];
        },
      ),
    );
  }
}

其中children 是listile 和其他元素的列表。

如何强制children 中的元素遵守父容器设置的边界限制?

【问题讨论】:

  • 你想要以上类型的小部件红色容器
  • 我希望列表图块的一角(灰色)不在顶部和底部的红色边框之外
  • 不是拐弯第一个和最后一个项目,而是用带有拐角边框的 ClipRRect 包围列
  • 用 ClipRRect 测试过,还是不行。我在 github repo 上填写了一个问题:github.com/flutter/flutter/issues/94785

标签: flutter border listtile


【解决方案1】:

只需将clipBehavior: Clip.antiAlias 添加到您的Container

代码示例

class MyWidget extends StatelessWidget {
  final children = List<Widget>.generate(5, (i) => ListTile(title: Text('$i')));
  
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(
        children.length,
        (index) {
          if (index == 0 && index == children.length - 1) {
            return Container(
              clipBehavior: Clip.antiAlias, // here
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: BorderRadius.circular(20),
                color: Colors.blue,
              ),
              child: children[index],
            );
          }
          if (index == 0) {
            return Container(
              clipBehavior: Clip.antiAlias, // here
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20)),
                color: Colors.blue,
              ),
              child: children[index],
            );
          } else if (index == children.length - 1) {
            return Container(
              clipBehavior: Clip.antiAlias, // and here
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.red,
                ),
                borderRadius: const BorderRadius.only(
                    bottomLeft: Radius.circular(20),
                    bottomRight: Radius.circular(20)),
                color: Colors.blue,
              ),
              child: children[index],
            );
          }

          return children[index];
        },
      ),
    );
  }
}

截图

Try the full code on DartPad

【讨论】:

  • 感谢您的回答!可悲的是,它并没有解决我的用例。我已使用您的代码示例更新了我的原始帖子以重现该问题。
【解决方案2】:

我最终在 Github 上发布了 Issue

CardclipBehavior: Clip.antiAlias 的解决方案。

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2012-06-09
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多