Flutter自定义Appbar

作者: Eason_0cce | 来源:发表于2020-01-20 10:58 被阅读0次

起初想到这个需求,是因项目中有头部定制化的功能。于是硬着头皮去搜索,最终实现了,我的效果图展示只是一个我做成的样子。实际上这是一个通用解决方案,有了定制化代码方案,你可以实现更多适合你自己的appBar。
实现效果图:


image.png

页面代码:

import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
  @override
  _HomePage createState()=>_HomePage();
}
class _HomePage extends State<HomePage> {
  @override
  initState() {
    super.initState();
  }
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size(null, 100),
        child: Container(
          decoration: BoxDecoration(

          ),
          width: MediaQuery.of(context).size.width,
          height: 88,
          child: ClipRRect(
            child: Container(
              color: Colors.blueAccent,
              child: Container(
                margin: EdgeInsets.fromLTRB(20, 20, 0, 0),
                child: Flex(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  direction:Axis.horizontal,
                  children: [
                    Container(
                      alignment: Alignment.center,
                      child: Text("LOGO占位", style: TextStyle(color:Colors.white),),
                      margin: EdgeInsets.only(top: 20.0, bottom: 15.0, right: 10.0 ),
                      width: 120,
                      height: double.infinity,
                      decoration: BoxDecoration(
                        border: Border.all( color: Colors.white.withOpacity(0.8)),
                      ),
                    ),
                    Expanded(
                      child: Container(
                        height: double.infinity,
                        margin: EdgeInsets.fromLTRB(0, 15, 0, 10),
                        padding: EdgeInsets.only(left: 10.0),
                        decoration: BoxDecoration(
                            color:Colors.white.withOpacity(0.4),
                            borderRadius: BorderRadius.all(Radius.circular(30.0))
//                          borderRadius:
                        ),
                        child: Opacity(
                          opacity: 0.8,
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Icon(Icons.search, color: Colors.white,size: 20.0,),
                              Padding(
                                padding: EdgeInsets.only(bottom: 4.0),
                                child: Text("商品名、厂家、条形码", style: TextStyle(color: Colors.white, fontSize: 16.0),),
                              )
                            ],
                          ),
                        )
                      ),
                    ),
                    SizedBox(
                      width: 50,
                      child: Icon(Icons.message,color: Colors.white),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
      body:Container()
    );
  }
}

如果有很多页面公用自定义头部怎么办?
提取头部:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return PreferredSize(
      preferredSize: Size(null, 100),
      child: Container(
        decoration: BoxDecoration(),
        width: MediaQuery.of(context).size.width,
        height: 88,
        child: ClipRRect(
          child: Container(
            color: Colors.blueAccent,
            child: Container(
              margin: EdgeInsets.fromLTRB(20, 20, 0, 0),
              child: Flex(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                direction: Axis.horizontal,
                children: [
                  Container(
                    alignment: Alignment.center,
                    child: Text(
                      "LOGO占位",
                      style: TextStyle(color: Colors.white),
                    ),
                    margin:
                        EdgeInsets.only(top: 20.0, bottom: 15.0, right: 10.0),
                    width: 120,
                    height: double.infinity,
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.white.withOpacity(0.8)),
                    ),
                  ),
                  Expanded(
                    child: Container(
                        height: double.infinity,
                        margin: EdgeInsets.fromLTRB(0, 15, 0, 10),
                        padding: EdgeInsets.only(left: 10.0),
                        decoration: BoxDecoration(
                            color: Colors.white.withOpacity(0.4),
                            borderRadius:
                                BorderRadius.all(Radius.circular(30.0))
//                          borderRadius:
                            ),
                        child: Opacity(
                          opacity: 0.8,
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Icon(
                                Icons.search,
                                color: Colors.white,
                                size: 20.0,
                              ),
                              Padding(
                                padding: EdgeInsets.only(bottom: 4.0),
                                child: Text(
                                  "商品名、厂家、条形码",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 16.0),
                                ),
                              )
                            ],
                          ),
                        )),
                  ),
                  SizedBox(
                    width: 50,
                    child: Icon(Icons.message, color: Colors.white),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize =>
      new Size(ScreenUtil().setWidth(750), ScreenUtil().setHeight(105));
}

重点代码,头部高度:
Size get preferredSize => new Size(ScreenUtil().setWidth(750),ScreenUtil().setHeight(105));

相关文章

  • flutter封装AppBar

    flutter封装AppBar 最近做flutter项目,为了更简洁,封装了AppBar组件,不过不是自定义组件哈...

  • 2020-09-04

    Flutter AppBar 上方空间多余问题 自定义 使用MediaQuery.removePadding 的r...

  • Flutter 自定义AppBar实现滚动渐变

    通过 Flutter 携程网实战项目,实现自定义AppBar实现滚动渐变。 项目地址 效果如下: 代码实现(注意注释):

  • Flutter自定义Appbar

    起初想到这个需求,是因项目中有头部定制化的功能。于是硬着头皮去搜索,最终实现了,我的效果图展示只是一个我做成的样子...

  • Flutter 自定义 AppBar

    import 'package:flutter/cupertino.dart'; import 'package:...

  • Flutter自定义带搜索框的AppBar

    刚起步学习Flutter,还是比较容易上手的。下面介绍一下自定义的带搜索框的AppBar。 项目地址(github...

  • flutter标题栏Appbar的封装

    设置标题栏高度PreferredSize Flutter的标题栏通常使用AppBar, 但是AppBar并没有提供...

  • flutter appbar

    有以下常用属性: leading → Widget - 在标题前面显示的一个控件,在首页通常显示应用的 logo;...

  • flutter AppBar

    AppBar 显示在app的顶部。AppBar包含5大部分,如下图: image

  • Flutter基础widgets教程-AppBar篇

    Flutter基础widgets教程-AppBar篇[https://developer.aliyun.com/a...

网友评论

    本文标题:Flutter自定义Appbar

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