search_bar.dart
4.27 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:Parlando/res/resources.dart';
import 'package:Parlando/util/theme_utils.dart';
import 'package:Parlando/extension/int_extension.dart';
import 'load_image.dart';
import 'my_button.dart';
/// 搜索页的AppBar
class SearchBar extends StatefulWidget implements PreferredSizeWidget {
const SearchBar({
Key? key,
this.hintText = '',
this.backImg = 'assets/images/ic_back_black.png',
this.onPressed,
}) : super(key: key);
final String backImg;
final String hintText;
final Function(String)? onPressed;
@override
_SearchBarState createState() => _SearchBarState();
@override
Size get preferredSize => Size.fromHeight(48.px);
}
class _SearchBarState extends State<SearchBar> {
final TextEditingController _controller = TextEditingController();
final FocusNode _focus = FocusNode();
@override
void dispose() {
_focus.dispose();
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final bool isDark = context.isDark;
final Color iconColor =
isDark ? Colours.dark_text_gray : Colours.text_gray_c;
final Widget back = Semantics(
label: '返回',
child: SizedBox(
width: 48.px,
height: 48.px,
child: InkWell(
onTap: () {
_focus.unfocus();
Navigator.maybePop(context);
},
borderRadius: BorderRadius.circular(24.px),
child: Padding(
key: const Key('search_back'),
padding: EdgeInsets.all(12.px),
child: Image.asset(
widget.backImg,
color: isDark ? Colours.dark_text : Colours.text,
),
),
),
),
);
final Widget textField = Expanded(
child: Container(
height: 32.px,
decoration: BoxDecoration(
color: isDark ? Colours.dark_material_bg : Colours.bg_gray,
borderRadius: BorderRadius.circular(4.px),
),
child: TextField(
key: const Key('search_text_field'),
controller: _controller,
focusNode: _focus,
textInputAction: TextInputAction.search,
onSubmitted: (String val) {
_focus.unfocus();
// 点击软键盘的动作按钮时的回调
widget.onPressed?.call(val);
},
decoration: InputDecoration(
contentPadding:
EdgeInsets.only(left: -8.px, right: -16.px, bottom: 14.px),
border: InputBorder.none,
icon: Padding(
padding: EdgeInsets.only(top: 8.px, bottom: 8.px, left: 8.px),
child: LoadAssetImage(
'poem/poem_search',
color: iconColor,
),
),
hintText: widget.hintText,
suffixIcon: GestureDetector(
child: Semantics(
label: '清空',
child: Padding(
padding:
EdgeInsets.only(left: 16.px, top: 8.px, bottom: 8.px),
child: LoadAssetImage('poem/poem_delete', color: iconColor),
),
),
onTap: () {
/// https://github.com/flutter/flutter/issues/35848
SchedulerBinding.instance.addPostFrameCallback((_) {
_controller.text = '';
});
},
),
),
),
),
);
final Widget search = MyButton(
minHeight: 32.px,
minWidth: 44.px,
fontSize: Dimens.font_sp14,
radius: 4.px,
padding: EdgeInsets.symmetric(horizontal: 8.px),
text: '搜索',
onPressed: () {
_focus.unfocus();
widget.onPressed?.call(_controller.text);
},
);
return AnnotatedRegion<SystemUiOverlayStyle>(
value: isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
child: Material(
color: context.backgroundColor,
child: SafeArea(
child: Row(
children: <Widget>[
back,
textField,
Gaps.hGap8,
search,
Gaps.hGap16,
],
),
),
),
);
}
}