美文网首页
自定义appbar

自定义appbar

作者: zZ_d205 | 来源:发表于2020-07-22 14:50 被阅读0次

原文地址:https://blog.csdn.net/JustBeauty/article/details/82110743

Flutter自定义标题栏之处理状态栏高度

94me 2018-08-27 12:57:14  13072  收藏 1

分类专栏: Flutter

版权

App在很多情况下由于各种需求需要自定义标题栏,而在能够构建Android和IOS应用的Flutter中,如果不在Scaffold中使用AppBar会发现默认是沉浸式。

猜想:我们使用自定义标题栏好像需要知道状态栏的高度,我看到网上很多人想要自定义标题栏,却老是去找怎么获取状态栏的高度

解惑:其实并不用获取状态栏的高度,你想AppBar也是状态栏,它也需要知道状态栏的高度,它需要说明Flutter已经帮我们获取到了

接下来一步一步来看

一、怎么自定义标题栏

轻车熟路的就直接看第二步

自定义MAppBar类

class MAppBar extends StatefulWidget implements PreferredSizeWidget {

  MAppBar({@required this.child}) : assert(child != null);

  final Widget child;

  @override

  Size get preferredSize {

    return new Size.fromHeight(56.0);

  }

  @override

  State createState() {

    return new MAppBarState();

  }

}

class MAppBarState extends State<MAppBar> {

  @override

  Widget build(BuildContext context) {

    return widget.child;

  }

使用

class GoodsPageState extends State<GoodsPage> {

  @override

  Widget build(BuildContext context) {

    return new Scaffold(

      appBar: new MAppBar(

        child: new Text("我是标题"),

      ),

      body: new Text("我是内容"),

    );

  }

}

效果

二、增加状态栏

修改代码

class MAppBarState extends State<MAppBar> {

  @override

  Widget build(BuildContext context) {

    return new SafeArea(

      top: true,

      child: widget.child,

    );

  }

}

说明

/// Whether to avoid system intrusions at the top of the screen, typically the

/// system status bar.

final bool top;

效果

效果已经很明显了

结论:通过SafeArea可以很容易的避免默认沉浸式,Flutter已经帮我们做了,多看源码学习Flutter组件

————————————————

版权声明:本文为CSDN博主「94me」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/JustBeauty/java/article/details/82110743

相关文章

网友评论

      本文标题:自定义appbar

      本文链接:https://www.haomeiwen.com/subject/dwzzkktx.html