Przeglądaj źródła

新增搜索框控件,并调整布局

刘清 1 miesiąc temu
rodzic
commit
949b571ba5

+ 4
- 1
lib/presentation/screens/home/home_screen.dart Wyświetl plik

@@ -17,7 +17,10 @@ class HomeScreen extends StatelessWidget {
17 17
           if (authProvider.isAuthenticated)
18 18
             IconButton(
19 19
               icon: const Icon(Icons.notifications_none),
20
-              onPressed: () {},
20
+              onPressed: () {
21
+                // 进入消息中心界面
22
+                // Navigator.of(context).pushNamed('/login');
23
+              },
21 24
             ),
22 25
         ],
23 26
       ),

+ 35
- 12
lib/presentation/screens/news/news_screen.dart Wyświetl plik

@@ -1,5 +1,6 @@
1 1
 import 'package:flutter/material.dart';
2 2
 import '../../navigation/bottom_nav_bar.dart';
3
+import '../../widgets/common/app_search_bar.dart';
3 4
 
4 5
 class NewsScreen extends StatelessWidget {
5 6
   const NewsScreen({super.key});
@@ -9,19 +10,41 @@ class NewsScreen extends StatelessWidget {
9 10
     return Scaffold(
10 11
       appBar: AppBar(
11 12
         title: const Text('推荐'),
12
-        actions: [
13
-          IconButton(
14
-            icon: const Icon(Icons.search),
15
-            onPressed: () {},
16
-          ),
17
-        ],
18 13
       ),
19
-      body: ListView.builder(
20
-        padding: const EdgeInsets.all(16),
21
-        itemCount: 10,
22
-        itemBuilder: (context, index) {
23
-          return _buildNewsCard(index);
24
-        },
14
+      body: SingleChildScrollView(
15
+        padding: const EdgeInsets.all(20),
16
+        child: Column(
17
+          crossAxisAlignment: CrossAxisAlignment.start,
18
+          children: [
19
+            // 使用 StatefulWidget 搜索框
20
+            SearchBarCustom(
21
+              hintText: '搜索...',
22
+              onSearch: (query) {
23
+                // 处理搜索逻辑
24
+                // print('搜索: $query');
25
+              },
26
+              onChanged: (query) {
27
+                // 实时搜索
28
+                // print('输入变化: $query');
29
+              },
30
+              onClear: () {
31
+                // print('清除搜索');
32
+              },
33
+            ),
34
+            const SizedBox(height: 30),
35
+
36
+            // 推荐列表
37
+            ListView.builder(
38
+              // padding: const EdgeInsets.all(16),
39
+              shrinkWrap: true,
40
+              physics: const NeverScrollableScrollPhysics(),
41
+              itemCount: 10,
42
+              itemBuilder: (context, index) {
43
+                return _buildNewsCard(index);
44
+              },
45
+            ),
46
+          ],
47
+        ),
25 48
       ),
26 49
       bottomNavigationBar: const BottomNavBar(initialIndex: 1),
27 50
     );

+ 0
- 20
lib/presentation/screens/services/services_screen.dart Wyświetl plik

@@ -15,26 +15,6 @@ class ServicesScreen extends StatelessWidget {
15 15
         child: Column(
16 16
           crossAxisAlignment: CrossAxisAlignment.start,
17 17
           children: [
18
-            // 搜索框
19
-            Container(
20
-              decoration: BoxDecoration(
21
-                color: Colors.grey[100],
22
-                borderRadius: BorderRadius.circular(25),
23
-              ),
24
-              child: TextField(
25
-                decoration: InputDecoration(
26
-                  hintText: '搜索服务...',
27
-                  border: InputBorder.none,
28
-                  prefixIcon: const Icon(Icons.search),
29
-                  contentPadding: const EdgeInsets.symmetric(
30
-                    vertical: 15,
31
-                    horizontal: 20,
32
-                  ),
33
-                ),
34
-              ),
35
-            ),
36
-            const SizedBox(height: 30),
37
-            
38 18
             // 服务分类
39 19
             const Text(
40 20
               '服务分类',

+ 72
- 0
lib/presentation/widgets/common/app_search_bar.dart Wyświetl plik

@@ -0,0 +1,72 @@
1
+// 独立的搜索框组件
2
+import 'package:flutter/material.dart';
3
+
4
+class SearchBarCustom extends StatefulWidget {
5
+  final ValueChanged<String>? onSearch;
6
+  final ValueChanged<String>? onChanged;
7
+  final VoidCallback? onClear;
8
+  final String hintText;
9
+  
10
+  const SearchBarCustom({
11
+    super.key,
12
+    this.onSearch,
13
+    this.onChanged,
14
+    this.onClear,
15
+    this.hintText = '搜索...',
16
+  });
17
+  
18
+  @override
19
+  State<SearchBarCustom> createState() => _SearchBarCustomState();
20
+}
21
+
22
+class _SearchBarCustomState extends State<SearchBarCustom> {
23
+  final TextEditingController _controller = TextEditingController();
24
+  final FocusNode _focusNode = FocusNode();
25
+  
26
+  @override
27
+  void dispose() {
28
+    _controller.dispose();
29
+    _focusNode.dispose();
30
+    super.dispose();
31
+  }
32
+  
33
+  @override
34
+  Widget build(BuildContext context) {
35
+    return Container(
36
+      decoration: BoxDecoration(
37
+        color: Colors.grey[100],
38
+        borderRadius: BorderRadius.circular(15),
39
+      ),
40
+      child: TextField(
41
+        controller: _controller,
42
+        focusNode: _focusNode,
43
+        decoration: InputDecoration(
44
+          hintText: widget.hintText,
45
+          border: InputBorder.none,
46
+          prefixIcon: const Icon(Icons.search),
47
+          suffixIcon: _controller.text.isNotEmpty
48
+              ? IconButton(
49
+                  icon: const Icon(Icons.clear),
50
+                  onPressed: () {
51
+                    _controller.clear();
52
+                    widget.onClear?.call();
53
+                  },
54
+                )
55
+              : null,
56
+          contentPadding: const EdgeInsets.symmetric(
57
+            vertical: 15,
58
+            horizontal: 20,
59
+          ),
60
+        ),
61
+        onChanged: (value) {
62
+          setState(() {}); // 更新清除按钮显示
63
+          widget.onChanged?.call(value);
64
+        },
65
+        onSubmitted: (value) {
66
+          widget.onSearch?.call(value);
67
+          _focusNode.unfocus();
68
+        },
69
+      ),
70
+    );
71
+  }
72
+}