| 1 | import 'package:flutter/cupertino.dart'; |
| 2 | |
| 3 | class NavBar extends StatelessWidget implements ObstructingPreferredSizeWidget { |
| 4 | factory NavBar({Widget? leading, Widget? middle, Widget? trailing, Color? backgroundColor}) { |
| 5 | return NavBar._internal( |
| 6 | leading: leading, |
| 7 | middle: middle, |
| 8 | trailing: trailing, |
| 9 | height: _height, |
| 10 | backgroundColor: backgroundColor); |
| 11 | } |
| 12 | |
| 13 | factory NavBar.withShadow( |
| 14 | {Widget? leading, Widget? middle, Widget? trailing, Color? backgroundColor}) { |
| 15 | return NavBar._internal( |
| 16 | leading: leading, |
| 17 | middle: middle, |
| 18 | trailing: trailing, |
| 19 | height: 80, |
| 20 | backgroundColor: backgroundColor, |
| 21 | decoration: BoxDecoration( |
| 22 | color: backgroundColor, |
| 23 | boxShadow: [ |
| 24 | BoxShadow( |
| 25 | color: Color.fromRGBO(132, 141, 198, 0.11), |
| 26 | blurRadius: 8, |
| 27 | offset: Offset(0, 2), |
| 28 | ), |
| 29 | ], |
| 30 | ), |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | NavBar._internal( |
| 35 | {this.leading, |
| 36 | this.middle, |
| 37 | this.trailing, |
| 38 | this.backgroundColor, |
| 39 | this.decoration, |
| 40 | this.height = _height}); |
| 41 | |
| 42 | static const _originalHeight = 44.0; // iOS nav bar height |
| 43 | static const _height = 60.0; |
| 44 | |
| 45 | final Widget? leading; |
| 46 | final Widget? middle; |
| 47 | final Widget? trailing; |
| 48 | final Color? backgroundColor; |
| 49 | final BoxDecoration? decoration; |
| 50 | final double height; |
| 51 | |
| 52 | @override |
| 53 | Widget build(BuildContext context) { |
| 54 | if (leading == null && middle == null && trailing == null) { |
| 55 | return const SizedBox(); |
| 56 | } |
| 57 | |
| 58 | final pad = height - _originalHeight; |
| 59 | final paddingTop = pad / 2; |
| 60 | final _paddingBottom = (pad / 2); |
| 61 | |
| 62 | return Container( |
| 63 | decoration: decoration ?? BoxDecoration(color: backgroundColor), |
| 64 | padding: EdgeInsetsDirectional.only(bottom: _paddingBottom, top: paddingTop), |
| 65 | child: CupertinoNavigationBar( |
| 66 | leading: leading, |
| 67 | automaticallyImplyLeading: false, |
| 68 | automaticallyImplyMiddle: false, |
| 69 | transitionBetweenRoutes: false, |
| 70 | middle: middle, |
| 71 | trailing: trailing, |
| 72 | backgroundColor: backgroundColor, |
| 73 | border: null, |
| 74 | ), |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | @override |
| 79 | Size get preferredSize => Size.fromHeight(height); |
| 80 | |
| 81 | @override |
| 82 | bool shouldFullyObstruct(BuildContext context) { |
| 83 | return false; |
| 84 | } |
| 85 | } |