Custom flutter widget shape

两盒软妹~` 提交于 2021-02-19 04:43:47

问题


I'm attempting to build the following layout in Flutter.

I'm looking to achieve 2 things:

  • Render a background that draws a diagonal side (I'm guessing through a BoxDecoration)
  • Have the pink container clip children along the diagonal side - i.e. if the text is too large for one line it should wrap to a new line.

Any ideas?


回答1:


There are multiple ways you could do this. One would be to use a CustomPainter to use it as the background and have it draw the pink + picture.

Another way would be to use a stack, something like this:

container (with pink background)
  -> stack
     -> picture, clipped
     -> text, etc

You'd lay out the text however you normally would, and you'd align the picture to the right and define a ClipPath that clipped it as you want it.

To make the text wrap, put it within a ConstrainedBox or SizedBox and make sure it's set to wrap (softwrap property I believe).




回答2:


Here is my code:

Stack(
  children: <Widget>[
    Pic(),
    Info(),
  ],
)

For widget Pic:

Container(
  decoration: BoxDecoration(
    image: DecorationImage(
      alignment: Alignment.centerRight,
      fit: BoxFit.fitHeight,
      image: NetworkImage(
          'https://media.sproutsocial.com/uploads/2014/02/Facebook-Campaign-Article-Main-Image2.png'),
    ),
  ),
)

For widget Info:

ClipPath(
  clipper: TrapeziumClipper(),
  child: Container(
    color: Colors.white,
    padding: EdgeInsets.all(8.0),
    width: MediaQuery.of(context).size.width * 3/5,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        ConstrainedBox(
          constraints: BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width * 6/15
          ),
          child: Text(
            'Testing clipping with soft wrap',
            softWrap: true,
            style: Theme.of(context).textTheme.title,
          ),
        ),
      ],
    ),
  ),
)

For TrapeziumClipper:

class TrapeziumClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();
    path.lineTo(size.width * 2/3, 0.0);
    path.lineTo(size.width, size.height);
    path.lineTo(0.0, size.height);
    path.close();
    return path;
  }
  @override
  bool shouldReclip(TrapeziumClipper oldClipper) => false;
}


来源:https://stackoverflow.com/questions/49641287/custom-flutter-widget-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!