如何重叠Appbar抖动上的项目

时间:2020-10-26 15:57:38

标签: flutter

我想在个人资料图片上实现一个圆形图标,如下例所示:

enter image description here

但是对我不起作用,这是我到目前为止所取得的成就:

  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        ClipPath(
          clipper: _AppBarProfileClipper(childHeight),
          child: Container(
            padding: const EdgeInsets.only(top: 48.0),
            color: TheBaseColors.lightBlue,
            height: height,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                leading,
                FlatButton(
                  onPressed: null,
                  child: Text(
                    title,
                    style: TextStyle(
                      fontWeight: FontWeight.w600,
                      fontSize: 18.0,
                      color: Colors.white,
                    ),
                  ),
                ),
                trailing,
              ],
            ),
          ),
        ),
        Positioned(
          bottom: childHeight / 2, //50
          left: 0,
          right: 0,
          child: Align(
            alignment: Alignment.bottomCenter,
            child: child,
          ),
        ),
      ],
    );
  }
}

所有参数都在构造函数中表示,并在代码中使用。但是我想不出如何用四舍五入的图标使效果更好。 有人知道为什么不起作用吗?

1 个答案:

答案 0 :(得分:1)

您可以实现如下所示的示例:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final double appBarHeight = 100.0;
  final double spaceAroundRoundButton = 4.0;
  final double roundedButtonSize = 64.0;

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    return SafeArea(
      child: Stack(
        children: <Widget>[
          Scaffold(
            backgroundColor: Colors.white,
            appBar: PreferredSize(
                preferredSize:
                    Size(MediaQuery.of(context).size.width, appBarHeight),
                child: ClipPath(
                  clipper: BottomClipper(),
                  child: Container(
                    alignment: Alignment.topLeft,
                    height: appBarHeight,
                    color: Colors.black,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        IconButton(
                            icon: Icon(
                              Icons.menu,
                              color: Colors.white,
                            ),
                            onPressed: () {}),
                        Text(
                          'User Profile',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 18,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        IconButton(
                            icon: Icon(
                              Icons.settings,
                              color: Colors.white,
                            ),
                            onPressed: () {})
                      ],
                    ),
                  ),
                )),
            body: Container(
              alignment: Alignment.topCenter,
              margin: EdgeInsets.only(
                  top:
                      roundedButtonSize * 0.75 + spaceAroundRoundButton + 16.0),
              child: Text('Body'),
            ),
          ),
          Positioned(
            child: Container(
              padding: EdgeInsets.all(spaceAroundRoundButton),
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: RawMaterialButton(
                elevation: 0,
                constraints: BoxConstraints.tightFor(
                    width: roundedButtonSize, height: roundedButtonSize),
                shape: StadiumBorder(
                    side: BorderSide(width: 4, color: Colors.grey)),
                child: Icon(Icons.person),
                onPressed: () {},
                fillColor: Colors.white,
              ),
            ),
            left: (size.width / 2) -
                (roundedButtonSize / 2) -
                spaceAroundRoundButton,
            top: appBarHeight -
                (roundedButtonSize * 0.25) -
                spaceAroundRoundButton,
          )
        ],
      ),
    );
  }
}

class BottomClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    path.lineTo(0.0, size.height * 0.75);

    var firstControlPoint = Offset(size.width * 0.25, size.height);
    var firstEndPoint = Offset(size.width * 0.5, size.height);
    path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
        firstEndPoint.dx, firstEndPoint.dy);

    var secondControlPoint = Offset(size.width * 0.75, size.height);
    var secondEndPoint = Offset(size.width, size.height * 0.75);
    path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy,
        secondEndPoint.dx, secondEndPoint.dy);

    path.lineTo(size.width, 0.0);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

屏幕截图:Given example screenshot