天天看點

Flutter 項目實戰 02 AppBar 的二次封裝使用

個人封裝的 AppBar ,使用比較簡單,不完善的地方,歡迎大家指點

廢話不多說,直接上代碼

import ‘package:flutter/material.dart’;

double abarHeight() {

return 48.0;

}

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,

));

}

_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();

}

}