flutter标题栏Appbar的封装

设置标题栏高度PreferredSize

Flutter的标题栏通常使用AppBar, 但是AppBar并没有提供设置高度的参数, 需要使用PreferredSize包裹appbar, 并设置preferredSize属性

设置appbar中的控件

通过leading和actions属性设置左右的控件
通过title指定标题栏的控件
通过centerTitle指定默认标题栏的位置, 默认Android上是false, 默认偏左

  • backAppbar可以返回一个只有标题和返回键的标题栏
import 'package:bigtoe/style/qss_color.dart';
import 'package:flutter/material.dart';

/**
 * Created with IntelliJ IDEA.
 * Package: utils
 * Author: sirai
 * Create Time: 2019-06-24 16:10
 * QQ: 785716471
 * Email: [email protected]
 * Description:公共的titlebar
 */

class TitleBar {

  /**
   * 仅含 左侧返回按钮 及中间标题
   * appBar: TitleBar().backAppbar(context, '个人资料'),
   * appBar: TitleBar().backAppbar(context, '个人资料',(){}),
   */
  backAppbar(BuildContext context, String title,{VoidCallback onPressed}) {
    return PreferredSize(
        preferredSize: Size.fromHeight(48),
        child: AppBar(
          title: Text(
            title,
            style: TextStyle(color: QssColors.black, fontSize: 16),
          ),
          centerTitle: true,
          leading: _leading(context,onPressed),
          brightness: Brightness.light,
          backgroundColor: QssColors.white,
          elevation: 0,
          iconTheme: IconThemeData(color: QssColors.black),
        ));
  }
  
  /**
   * 设置左侧按钮
   */
  _leading(BuildContext context, VoidCallback onPressed) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 44,
          padding: EdgeInsets.all(0),
          child: new IconButton(
            padding: EdgeInsets.only(left: 16, right: 16),
//            icon: Image.asset(
//              'assets/images/ic_black_left_arrow.png',
//              fit: BoxFit.contain,
//              width: 16,
//              height: 16,
//            ),
            icon: Icon(Icons.chevron_left),
            onPressed: () {
              if (onPressed == null) {
                _popThis(context);
              } else {
                onPressed();
              }
            },
          ),
        ),
      ],
    );
  }


  /**
   * 关闭页面
   */
  _popThis(BuildContext context){
    Navigator.of(context).pop();
  }

}

转载 https://blog.csdn.net/u014449046/article/details/92242982

你可能感兴趣的:(flutter标题栏Appbar的封装)