app_bar.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:liftmanager/res/colors.dart';
  4. import 'package:liftmanager/res/dimens.dart';
  5. import 'package:liftmanager/res/gaps.dart';
  6. import 'package:liftmanager/res/iconfont.dart';
  7. import 'package:liftmanager/routers/fluro_navigator.dart';
  8. /// 自定义AppBar
  9. class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  10. const MyAppBar(
  11. {Key key,
  12. this.backgroundColor,
  13. this.title: "",
  14. this.centerTitle: "",
  15. this.actions,
  16. this.onPressed,
  17. this.fun,
  18. this.onBack,
  19. this.isFun: false,
  20. this.isBack: true,
  21. this.bgColors,
  22. this.titleColor = Colours.text_black,
  23. this.statubarStyle = SystemUiOverlayStyle.dark,
  24. this.isColorTopToBottom=false})
  25. : super(key: key);
  26. // SystemUiOverlayStyle.dark
  27. final Color backgroundColor;
  28. final String title;
  29. final String centerTitle;
  30. final List<Widget> actions;
  31. final VoidCallback onPressed;
  32. final bool isBack;
  33. final Function fun;
  34. final bool isFun;
  35. final VoidCallback onBack;
  36. final List<Color> bgColors;
  37. final Color titleColor;
  38. final SystemUiOverlayStyle statubarStyle;
  39. final bool isColorTopToBottom;
  40. @override
  41. Widget build(BuildContext context) {
  42. return AnnotatedRegion<SystemUiOverlayStyle>(
  43. value: statubarStyle,
  44. child: Container(
  45. color: bgColors == null
  46. ? backgroundColor == null
  47. ? Colors.white
  48. : backgroundColor
  49. : null,
  50. decoration: bgColors != null
  51. ? BoxDecoration(
  52. gradient: LinearGradient(
  53. begin: isColorTopToBottom?Alignment.topCenter:Alignment.centerLeft,
  54. end: isColorTopToBottom?Alignment.bottomCenter:Alignment.centerRight,
  55. colors: bgColors,
  56. ),
  57. )
  58. : null,
  59. child: SafeArea(
  60. child: Stack(
  61. alignment: Alignment.centerLeft,
  62. children: <Widget>[
  63. Column(
  64. mainAxisAlignment: MainAxisAlignment.center,
  65. children: <Widget>[
  66. Container(
  67. alignment: centerTitle.isEmpty
  68. ? Alignment.centerLeft
  69. : Alignment.center,
  70. width: double.infinity,
  71. child: Text(title.isEmpty ? centerTitle : title,
  72. style: TextStyle(
  73. fontSize: Dimens.font_sp18,
  74. color: titleColor,
  75. )),
  76. padding: const EdgeInsets.symmetric(horizontal: 48.0),
  77. )
  78. ],
  79. ),
  80. isBack
  81. ? IconButton(
  82. onPressed: () {
  83. // FocusScope.of(context).unfocus();
  84. // if(isFun){
  85. // fun();
  86. // }else {
  87. // Navigator.maybePop(context);
  88. // }
  89. if (onBack != null) {
  90. onBack();
  91. } else if (isFun) {
  92. fun();
  93. FocusScope.of(context).unfocus();
  94. } else {
  95. FocusScope.of(context).unfocus();
  96. NavigatorUtils.goBackWithParams(context, true);
  97. }
  98. },
  99. tooltip: 'Back',
  100. padding: const EdgeInsets.all(12.0),
  101. icon: Icon(
  102. Iconfont.fanhui,
  103. size: 17,
  104. color: titleColor,
  105. ),
  106. )
  107. : Gaps.empty,
  108. Positioned(
  109. right: 10,
  110. child: Theme(
  111. data: Theme.of(context).copyWith(
  112. buttonTheme: ButtonThemeData(
  113. padding: const EdgeInsets.symmetric(horizontal: 16.0),
  114. minWidth: 60.0,
  115. )),
  116. child: rightActions()),
  117. ),
  118. ],
  119. ),
  120. ),
  121. ),
  122. );
  123. }
  124. Widget rightActions() {
  125. if (this.actions != null && this.actions.isNotEmpty) {
  126. return Row(
  127. children: this.actions,
  128. );
  129. }
  130. return Container(child: null,);
  131. }
  132. @override
  133. Size get preferredSize => Size.fromHeight(48.0);
  134. }