【问题标题】:how to overlay a circular image on the top of a card (half in the card and half outside the card)?如何在卡片顶部叠加圆形图像(一半在卡片内,一半在卡片外)?
【发布时间】:2018-10-18 19:16:51
【问题描述】:

我是 Flutter 的新手,最近我尝试设计一个页面,我必须将图像放在卡片顶部,其中一半在卡片上,另一半在卡片外,我尝试了 Stack,但无法不要设计我想要的! 这是我正在尝试设计的示例。

这里的代码没有给出想要的结果,如下图所示:

class ContainerWithCircle extends StatelessWidget {
final double circleRadius = 100.0;
final double circleBorderWidth = 8.0;
@override
Widget build(BuildContext context) {
   return Stack(
  alignment: Alignment.topCenter,
  children: <Widget>[

    Container(
      width: circleRadius,
      height: circleRadius,
      decoration:
          ShapeDecoration(shape: CircleBorder(), color: Colors.white),
      child: Padding(
        padding: EdgeInsets.all(circleBorderWidth),
        child: DecoratedBox(
          decoration: ShapeDecoration(
              shape: CircleBorder(),
              image: DecorationImage(
                  fit: BoxFit.cover,
                  image: NetworkImage(
                    'https://upload.wikimedia.org/wikipedia/commons/a/a0/Bill_Gates_2018.jpg',
                  ))),
        ),
      ),
    ),
    Padding(
      padding: EdgeInsets.only(top: circleRadius / 2.0),
      child: Container(
        // Some content
      ),
    ),
  ],
);

} }

【问题讨论】:

  • 分享您的代码,以便我们帮助您修复它
  • @RaoufRahiche 我在问如何做这样的事情!

标签: flutter


【解决方案1】:

为了创建与您指定的布局类似的布局,您可以简单地使用堆栈并将带有填充的卡片放在顶部。供您查找的资源:@​​987654321@、DecoratedBoxCircleBOrder. 以下代码显示了一个示例实现:

class ContainerWithCircle extends StatelessWidget {
  final double circleRadius = 100.0;
  final double circleBorderWidth = 8.0;

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.topCenter,
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(top: circleRadius / 2.0),
          child: Container(
            //replace this Container with your Card
            color: Colors.white,
            height: 200.0,
          ),
        ),
        Container(
          width: circleRadius,
          height: circleRadius,
          decoration:
              ShapeDecoration(shape: CircleBorder(), color: Colors.white),
          child: Padding(
            padding: EdgeInsets.all(circleBorderWidth),
            child: DecoratedBox(
              decoration: ShapeDecoration(
                  shape: CircleBorder(),
                  image: DecorationImage(
                      fit: BoxFit.cover,
                      image: NetworkImage(
                        'https://upload.wikimedia.org/wikipedia/commons/a/a0/Bill_Gates_2018.jpg',
                      ))),
            ),
          ),
        )
      ],
    );
  }
}

【讨论】:

  • 谢谢,虽然代码和我想要的不一样,但是我确实明白了我想要的怎么实现!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 2019-11-17
相关资源
最近更新 更多