diff --git a/lib/pages/home/controller.dart b/lib/pages/home/controller.dart index 64510ee1..e6fafba6 100644 --- a/lib/pages/home/controller.dart +++ b/lib/pages/home/controller.dart @@ -5,6 +5,7 @@ import 'package:get/get.dart'; import 'package:hive/hive.dart'; import 'package:pilipala/models/common/tab_type.dart'; import 'package:pilipala/utils/storage.dart'; +import '../../http/index.dart'; class HomeController extends GetxController with GetTickerProviderStateMixin { bool flag = false; @@ -24,6 +25,7 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { late bool hideSearchBar; late List defaultTabs; late List tabbarSort; + RxString defaultSearch = ''.obs; @override void onInit() { @@ -35,6 +37,9 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { setTabConfig(); hideSearchBar = setting.get(SettingBoxKey.hideSearchBar, defaultValue: true); + if (setting.get(SettingBoxKey.enableSearchWord, defaultValue: true)) { + searchDefault(); + } } void onRefresh() { @@ -94,4 +99,11 @@ class HomeController extends GetxController with GetTickerProviderStateMixin { } }); } + + void searchDefault() async { + var res = await Request().get(Api.searchDefault); + if (res.data['code'] == 0) { + defaultSearch.value = res.data['data']['name']; + } + } } diff --git a/lib/pages/home/view.dart b/lib/pages/home/view.dart index c31460fb..ce7b46c6 100644 --- a/lib/pages/home/view.dart +++ b/lib/pages/home/view.dart @@ -5,7 +5,6 @@ import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/widgets/network_img_layer.dart'; import 'package:pilipala/pages/mine/index.dart'; -import 'package:pilipala/pages/search/index.dart'; import 'package:pilipala/utils/feed_back.dart'; import './controller.dart'; @@ -144,6 +143,7 @@ class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { padding: EdgeInsets.fromLTRB(14, top + 6, 14, 0), child: UserInfoWidget( top: top, + ctr: ctr, userLogin: isUserLoggedIn, userFace: ctr?.userFace.value, callback: () => callback!(), @@ -162,18 +162,20 @@ class UserInfoWidget extends StatelessWidget { required this.userLogin, required this.userFace, required this.callback, + required this.ctr, }) : super(key: key); final double top; final RxBool userLogin; final String? userFace; final VoidCallback? callback; + final HomeController? ctr; @override Widget build(BuildContext context) { return Row( children: [ - const SearchBar(), + SearchBar(ctr: ctr), if (userLogin.value) ...[ const SizedBox(width: 4), ClipRect( @@ -335,11 +337,15 @@ class CustomChip extends StatelessWidget { } class SearchBar extends StatelessWidget { - const SearchBar({super.key}); + const SearchBar({ + Key? key, + required this.ctr, + }) : super(key: key); + + final HomeController? ctr; @override Widget build(BuildContext context) { - final SSearchController searchController = Get.put(SSearchController()); final ColorScheme colorScheme = Theme.of(context).colorScheme; return Expanded( child: Container( @@ -353,7 +359,10 @@ class SearchBar extends StatelessWidget { color: colorScheme.onSecondaryContainer.withOpacity(0.05), child: InkWell( splashColor: colorScheme.primaryContainer.withOpacity(0.3), - onTap: () => Get.toNamed('/search'), + onTap: () => Get.toNamed( + '/search', + parameters: {'hintText': ctr!.defaultSearch.value}, + ), child: Row( children: [ const SizedBox(width: 14), @@ -362,14 +371,12 @@ class SearchBar extends StatelessWidget { color: colorScheme.onSecondaryContainer, ), const SizedBox(width: 10), - Expanded( - child: Obx( - () => Text( - searchController.defaultSearch.value, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle(color: colorScheme.outline), - ), + Obx( + () => Text( + ctr!.defaultSearch.value, + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: TextStyle(color: colorScheme.outline), ), ), ], diff --git a/lib/pages/search/controller.dart b/lib/pages/search/controller.dart index 52a521e2..5ec1710a 100644 --- a/lib/pages/search/controller.dart +++ b/lib/pages/search/controller.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:get/get_rx/src/rx_workers/utils/debouncer.dart'; import 'package:hive/hive.dart'; -import 'package:pilipala/http/index.dart'; import 'package:pilipala/http/search.dart'; import 'package:pilipala/models/search/hot.dart'; import 'package:pilipala/models/search/suggest.dart'; @@ -27,9 +26,6 @@ class SSearchController extends GetxController { @override void onInit() { super.onInit(); - if (setting.get(SettingBoxKey.enableSearchWord, defaultValue: true)) { - searchDefault(); - } // 其他页面跳转过来 if (Get.parameters.keys.isNotEmpty) { if (Get.parameters['keyword'] != null) { @@ -130,12 +126,4 @@ class SSearchController extends GetxController { historyList.refresh(); histiryWord.put('cacheList', []); } - - void searchDefault() async { - var res = await Request().get(Api.searchDefault); - if (res.data['code'] == 0) { - searchKeyWord.value = - hintText = defaultSearch.value = res.data['data']['name']; - } - } }