my_app_bar.dart
3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:one_poem/util/theme_utils.dart';
import 'package:one_poem/res/resources.dart';
import 'my_button.dart';
/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
const MyAppBar({
Key? key,
this.backgroundColor,
this.title = '',
this.centerTitle = '',
this.actionName = '',
this.backImg = 'assets/images/ic_back_black.png',
this.backImgColor,
this.onPressed,
this.isBack = true
}): super(key: key);
final Color? backgroundColor;
final String title;
final String centerTitle;
final String backImg;
final Color? backImgColor;
final String actionName;
final VoidCallback? onPressed;
final bool isBack;
@override
Widget build(BuildContext context) {
final Color _backgroundColor = backgroundColor ?? context.backgroundColor;
final SystemUiOverlayStyle _overlayStyle = ThemeData.estimateBrightnessForColor(_backgroundColor) == Brightness.dark
? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
final Widget action = actionName.isNotEmpty ? Positioned(
right: 0.0,
child: Theme(
data: Theme.of(context).copyWith(
buttonTheme: const ButtonThemeData(
padding: EdgeInsets.symmetric(horizontal: 16.0),
minWidth: 60.0,
),
),
child: MyButton(
key: const Key('actionName'),
fontSize: Dimens.font_sp14,
minWidth: null,
text: actionName,
textColor: context.isDark ? Colours.dark_text : Colours.text,
backgroundColor: Colors.transparent,
onPressed: onPressed,
),
),
) : Gaps.empty;
final Widget back = isBack ? IconButton(
onPressed: () async {
FocusManager.instance.primaryFocus?.unfocus();
final isBack = await Navigator.maybePop(context);
if (!isBack) {
await SystemNavigator.pop();
}
},
tooltip: 'Back',
padding: const EdgeInsets.all(12.0),
icon: Image.asset(
backImg,
color: backImgColor ?? ThemeUtils.getIconColor(context),
),
) : Gaps.empty;
final Widget titleWidget = Semantics(
namesRoute: true,
header: true,
child: Container(
alignment: centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
width: double.infinity,
margin: const EdgeInsets.symmetric(horizontal: 48.0),
child: Text(
title.isEmpty ? centerTitle : title,
style: const TextStyle(fontSize: Dimens.font_sp18,),
),
),
);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: _overlayStyle,
child: Material(
color: _backgroundColor,
child: SafeArea(
child: Stack(
alignment: Alignment.centerLeft,
children: <Widget>[
titleWidget,
back,
action,
],
),
),
),
);
}
@override
Size get preferredSize => const Size.fromHeight(48.0);
}