Flutter--自定义AppBar

Flutter--自定义AppBar,第1张

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {

  final Widget child;

  CustomAppBar(this.child);

  @override
  Widget build(BuildContext context) {
    return child;
  }

  @override
  Size get preferredSize => Size(100, 100);
}



//有特殊复杂的也可用StatefulWidget来解决

class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {

  final Widget child;

  CustomAppBar(this.child);

  @override
  State createState() => _CustomAppBarState();

  @override
  Size get preferredSize => Size(100, 50);  //写啥没差,自动采用子视图尺寸

}

class _CustomAppBarState extends State {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

实例

//使用自定义的AppBar要注意在背景装饰层里使用一个SafeArea来保证子元素别顶到信息栏



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(Container(
        color: Colors.red,
        child: SafeArea(child: Row(children: [
          BackButton()
        ], crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,)),
      )),
      body: Text("data")
    );
  }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存