【Flutter 实战】各种各样形状的组件

移动开发 作者: 2024-08-25 08:45:01
老孟导读:Flutter中很多组件都有一个叫做shape的属性,类型是ShapeBorder,比如Button类、Card等组件,shape表示控件的形状,系统已经为我们提供了很多形状,对于没有此属性
RaisedButton(
  shape: BeveledRectangleBorder(
      side: BorderSide(width: 1,color: Colors.red),borderRadius: BorderRadius.circular(10)),child: Text('老孟'),onPressed: () {},)
 3RaisedButton(
  shape: BeveledRectangleBorder(
      side: BorderSide(width: 1,borderRadius: BorderRadius.circular(100)),)
RaisedButton(
  shape: BeveledRectangleBorder(
      side: BorderSide(width: 1,borderRadius: BorderRadius.circular(0)),)
RaisedButton(
  shape: Border(
    top: BorderSide(color: Colors.red,width: 2)
  ),)
RaisedButton(
        shape: Border(
          top: BorderSide(color: Colors.red,width: 10),right: BorderSide(color: Colors.blue,bottom: BorderSide(color: Colors.yellow,left: BorderSide(color: Colors.green,),)
RaisedButton(
  shape: BorderDirectional(
    start: BorderSide(color: Colors.red,width: 2),end: BorderSide(color: Colors.blue,)
RaisedButton(
  shape: CircleBorder(side: BorderSide(color: Colors.red)),)
RaisedButton(
  shape: ContinuousRectangleBorder(
      side: BorderSide(color: Colors.red),borderRadius: BorderRadius.circular(20)),)
RaisedButton(
  shape: RoundedRectangleBorder(
      side: BorderSide(color: Colors.red),)
RaisedButton(
  shape: StadiumBorder(
      side: BorderSide(color: Colors.red),)
RaisedButton(
  shape: OutlineInputBorder(
    borderSide: BorderSide(color: Colors.red),borderRadius: BorderRadius.circular(10),)
RaisedButton(
  shape: UnderlineInputBorder(
    borderSide: BorderSide(color: Colors.red),)
ClipRect(
  child: Align(
    alignment: Alignment.topCenter,heightFactor: 0.5,child: Container(
      height: 150,width: 150,child: Image.asset(
        'images/1.png',fit: BoxFit.cover,)
裁剪效果:
  • none:不裁剪,系统默认值,如果子组件不超出边界,此值没有任何性能消耗。
  • hardEdge:裁剪但不应用抗锯齿,速度比none慢一点,但比其他方式快。
  • antiAlias:裁剪而且抗锯齿,此方式看起来更平滑,比antiAliasWithSaveLayer快,比hardEdge慢,通常用于处理圆形和弧形裁剪。
  • antiAliasWithSaveLayer:裁剪、抗锯齿而且有一个缓冲区,此方式很慢,用到的情况比较少。
ClipRRect(
  borderRadius: BorderRadius.circular(20),child: Container(
    height: 150,child: Image.asset(
      'images/1.png',)
ClipOval(
  child: Container(
    height: 150,width: 250,)
ClipPath.shape(
  shape: StadiumBorder(),)
@override
Widget build(BuildContext context) {
  return Center(
    child: ClipPath(
      clipper: TrianglePath(),child: Container(
        height: 150,child: Image.asset(
          'images/1.png',);
}
class TrianglePath extends CustomClipper<Path>{
  @override
  Path getClip(Size size) {
    var path = Path();
    path.moveTo(size.width/2,0);
    path.lineTo(0,size.height);
    path.lineTo(size.width,size.height);
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return true;
  }
}
class StarPath extends CustomClipper<Path> {
  StarPath({this.scale = 2.5});

  final double scale;

  double perDegree = 36;

  /// 角度转弧度公式
  double degree2Radian(double degree) {
    return (pi * degree / 180);
  }

  @override
  Path getClip(Size size) {
    var R = min(size.width / 2,size.height / 2);
    var r = R / scale;
    var x = size.width / 2;
    var y = size.height / 2;

    var path = Path();
    path.moveTo(x,y - R);
    path.lineTo(x - sin(degree2Radian(perDegree)) * r,y - cos(degree2Radian(perDegree)) * r);
    path.lineTo(x - sin(degree2Radian(perDegree * 2)) * R,y - cos(degree2Radian(perDegree * 2)) * R);
    path.lineTo(x - sin(degree2Radian(perDegree * 3)) * r,y - cos(degree2Radian(perDegree * 3)) * r);
    path.lineTo(x - sin(degree2Radian(perDegree * 4)) * R,y - cos(degree2Radian(perDegree * 4)) * R);
    path.lineTo(x - sin(degree2Radian(perDegree * 5)) * r,y - cos(degree2Radian(perDegree * 5)) * r);
    path.lineTo(x - sin(degree2Radian(perDegree * 6)) * R,y - cos(degree2Radian(perDegree * 6)) * R);
    path.lineTo(x - sin(degree2Radian(perDegree * 7)) * r,y - cos(degree2Radian(perDegree * 7)) * r);
    path.lineTo(x - sin(degree2Radian(perDegree * 8)) * R,y - cos(degree2Radian(perDegree * 8)) * R);
    path.lineTo(x - sin(degree2Radian(perDegree * 9)) * r,y - cos(degree2Radian(perDegree * 9)) * r);
    path.lineTo(x - sin(degree2Radian(perDegree * 10)) * R,y - cos(degree2Radian(perDegree * 10)) * R);
    return path;
  }

  @override
  bool shouldReclip(StarPath oldClipper) {
    return oldClipper.scale != this.scale;
  }
}
class StartClip extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _StartClipState();
}

class _StartClipState extends State<StartClip>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;

  @override
  void initState() {
    _controller =
        AnimationController(duration: Duration(seconds: 2),vsync: this)
          ..addStatusListener((status) {
            if (status == AnimationStatus.completed) {
              _controller.reverse();
            } else if (status == AnimationStatus.dismissed) {
              _controller.forward();
            }
          });
    _animation = Tween(begin: 1.0,end: 4.0).animate(_controller);
    _controller.forward();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: AnimatedBuilder(
          animation: _animation,builder: (context,child) {
            return ClipPath(
              clipper: StarPath(scale: _animation.value),child: Container(
                height: 150,color: Colors.red,);
          }),);
  }
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68304.html