Flutter 项目实战 02 AppBar 的二次封装使用

个人封装的 AppBar ,使用比较简单,不完善的地方,欢迎大家指点

废话不多说,直接上代码

import ‘package:flutter/material.dart’;

double abarHeight() {
return 48.0;
}

/*
*backAppbar可以返回一个只有标题和返回键的标题栏

  • context 当前页面的对象
  • title 当前页面的标题
  • isLeading 可选的方法,默认为true,是否展示返回箭头
  • tapFunc 可选方法,点击标题的时候触发
  • actions 可选widget 标签,自定义右侧标签栏
    */
    backAppBar(BuildContext context, String title, {bool isLeading = true,Function tapFunc,Widget actions}) {
    return PreferredSize(
    preferredSize: Size.fromHeight(abarHeight()),
    child: new AppBar(
    elevation: 0,
    backgroundColor: Colors.white,
    leading: isLeading ? _leading(context) : null,
    title: new InkWell(
    child: new Text(
    title,
    style: TextStyle(
    fontSize: 20.0,
    color: Color(0xFF141E32),
    ),
    ),
    onTap: () {
    tapFunc();
    },
    ),
    actions: actions != null ? [
    Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
    actions,
    ],
    )
    ] : null,
    centerTitle: true,
    ));
    }

/*
*自定义返回图标

  • context 当前对象
  • backFunc 重写返回事件
    */
    _leading(BuildContext context, {Function backFunc}) {
    return Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
    Container(
    width: 60,
    padding: EdgeInsets.all(0),
    child: new IconButton(
    // padding: EdgeInsets.only(left: 20.0, right: 20.0),
    icon: Image.asset(
    ‘images/back_left_arrow.png’,
    fit: BoxFit.contain,
    width: 20,
    height: 20,
    ),
    onPressed: () {
    if (backFunc == null) {
    _popNar(context);
    } else {
    backFunc();
    }
    }),
    ),
    ],
    );
    }

/*

  • 默认返回至上一页
    */

void _popNar(BuildContext context) {
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半身风雪

感谢打赏,你的鼓励,是我创作的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值