简体   繁体   中英

Custom flutter widget shape

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 - ie if the text is too large for one line it should wrap to a new line.

Any ideas?

对角线布局形状

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).

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM