diff --git a/lib/pages/home/view.dart b/lib/pages/home/view.dart index a4f0b73f..49982c7d 100644 --- a/lib/pages/home/view.dart +++ b/lib/pages/home/view.dart @@ -161,7 +161,7 @@ class UserInfoWidget extends StatelessWidget { Widget build(BuildContext context) { return Row( children: [ - const Expanded(child: SearchPage()), + const SearchBar(), if (userLogin.value) ...[ const SizedBox(width: 4), ClipRect( @@ -199,7 +199,7 @@ class UserInfoWidget extends StatelessWidget { ) ], ) - : DefaultUser(callback: () => callback), + : DefaultUser(callback: () => callback!()), ), ], ); @@ -336,3 +336,50 @@ class CustomChip extends StatelessWidget { ); } } + +class SearchBar extends StatelessWidget { + const SearchBar({super.key}); + + @override + Widget build(BuildContext context) { + final SSearchController searchController = Get.put(SSearchController()); + final ColorScheme colorScheme = Theme.of(context).colorScheme; + return Expanded( + child: Container( + width: 250, + height: 44, + clipBehavior: Clip.hardEdge, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(25), + ), + child: Material( + color: colorScheme.onSecondaryContainer.withOpacity(0.05), + child: InkWell( + splashColor: colorScheme.primaryContainer.withOpacity(0.3), + onTap: () => Get.toNamed('/search'), + child: Row( + children: [ + const SizedBox(width: 14), + Icon( + Icons.search_outlined, + color: colorScheme.onSecondaryContainer, + ), + const SizedBox(width: 10), + Expanded( + child: Obx( + () => Text( + searchController.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 59d51a41..52a521e2 100644 --- a/lib/pages/search/controller.dart +++ b/lib/pages/search/controller.dart @@ -20,7 +20,7 @@ class SSearchController extends GetxController { final _debouncer = Debouncer(delay: const Duration(milliseconds: 200)); // 设置延迟时间 String hintText = '搜索'; - RxString defaultSearch = '输入关键词搜索'.obs; + RxString defaultSearch = ''.obs; Box setting = GStrorage.setting; bool enableHotKey = true; diff --git a/lib/pages/search/view.dart b/lib/pages/search/view.dart index e219e36a..c262df38 100644 --- a/lib/pages/search/view.dart +++ b/lib/pages/search/view.dart @@ -1,6 +1,5 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:animations/animations.dart'; import 'package:get/get.dart'; import 'package:pilipala/common/widgets/http_error.dart'; import 'controller.dart'; @@ -42,122 +41,62 @@ class _SearchPageState extends State with RouteAware { @override Widget build(BuildContext context) { - return OpenContainer( - closedElevation: 0, - openElevation: 0, - onClosed: (_) => _searchController.onClear(), - openColor: Theme.of(context).colorScheme.background, - middleColor: Theme.of(context).colorScheme.background, - closedColor: Colors.transparent, - closedShape: const RoundedRectangleBorder( - borderRadius: BorderRadius.all(Radius.circular(30.0))), - openShape: const RoundedRectangleBorder( - borderRadius: BorderRadius.all(Radius.circular(30.0))), - closedBuilder: (BuildContext context, VoidCallback openContainer) { - return Container( - width: 250, - height: 44, - clipBehavior: Clip.hardEdge, - decoration: const BoxDecoration( - borderRadius: BorderRadius.all(Radius.circular(25)), + return Scaffold( + resizeToAvoidBottomInset: false, + appBar: AppBar( + shape: Border( + bottom: BorderSide( + color: Theme.of(context).dividerColor.withOpacity(0.08), + width: 1, ), - child: Material( - color: Theme.of(context) - .colorScheme - .onSecondaryContainer - .withOpacity(0.05), - child: InkWell( - splashColor: Theme.of(context) - .colorScheme - .primaryContainer - .withOpacity(0.3), - onTap: openContainer, - child: Row( - children: [ - const SizedBox(width: 14), - Icon( - Icons.search_outlined, - color: Theme.of(context).colorScheme.onSecondaryContainer, - ), - const SizedBox(width: 10), - Expanded( - child: Obx( - () => Text( - _searchController.defaultSearch.value, - maxLines: 1, - overflow: TextOverflow.ellipsis, - style: TextStyle( - color: Theme.of(context).colorScheme.outline, - ), - ), - ), - ), - ], - ), - ), + ), + titleSpacing: 0, + actions: [ + IconButton( + onPressed: () => _searchController.submit(), + icon: const Icon(CupertinoIcons.search, size: 22), ), - ); - }, - openBuilder: (BuildContext context, VoidCallback _) { - return Scaffold( - resizeToAvoidBottomInset: false, - appBar: AppBar( - shape: Border( - bottom: BorderSide( - color: Theme.of(context).dividerColor.withOpacity(0.08), - width: 1, - ), - ), - titleSpacing: 0, - actions: [ - Hero( - tag: 'searchTag', - child: IconButton( - onPressed: () => _searchController.submit(), - icon: const Icon(CupertinoIcons.search, size: 22)), - ), - const SizedBox(width: 10) - ], - title: Obx( - () => TextField( - autofocus: true, - focusNode: _searchController.searchFocusNode, - controller: _searchController.controller.value, - textInputAction: TextInputAction.search, - onChanged: (value) => _searchController.onChange(value), - decoration: InputDecoration( - hintText: _searchController.hintText, - border: InputBorder.none, - suffixIcon: IconButton( - icon: Icon( - Icons.clear, - size: 22, - color: Theme.of(context).colorScheme.outline, - ), - onPressed: () => _searchController.onClear(), - ), + const SizedBox(width: 10) + ], + title: Obx( + () => TextField( + autofocus: true, + focusNode: _searchController.searchFocusNode, + controller: _searchController.controller.value, + textInputAction: TextInputAction.search, + onChanged: (value) => _searchController.onChange(value), + decoration: InputDecoration( + hintText: _searchController.hintText, + border: InputBorder.none, + suffixIcon: IconButton( + icon: Icon( + Icons.clear, + size: 22, + color: Theme.of(context).colorScheme.outline, ), - onSubmitted: (String value) => _searchController.submit(), + onPressed: () => _searchController.onClear(), ), ), + onSubmitted: (String value) => _searchController.submit(), ), - body: SingleChildScrollView( - child: Column( - children: [ - const SizedBox(height: 12), - // 搜索建议 - _searchSuggest(), - // 热搜 - Visibility( - visible: _searchController.enableHotKey, - child: hotSearch(_searchController)), - // 搜索历史 - _history() - ], + ), + ), + body: SingleChildScrollView( + child: Column( + children: [ + const SizedBox(height: 12), + // 搜索建议 + _searchSuggest(), + // 热搜 + Visibility( + visible: _searchController.enableHotKey, + child: hotSearch(_searchController), ), - ), - ); - }, + // 搜索历史 + _history() + ], + ), + ), ); } @@ -301,25 +240,24 @@ class _SearchPageState extends State with RouteAware { ], ), ), - // if (_searchController.historyList.isNotEmpty) - Obx(() => Wrap( - spacing: 8, - runSpacing: 8, - direction: Axis.horizontal, - textDirection: TextDirection.ltr, - children: [ - for (int i = 0; - i < _searchController.historyList.length; - i++) - SearchText( - searchText: _searchController.historyList[i], - searchTextIdx: i, - onSelect: (value) => _searchController.onSelect(value), - onLongSelect: (value) => - _searchController.onLongSelect(value), - ) - ], - )), + Obx( + () => Wrap( + spacing: 8, + runSpacing: 8, + direction: Axis.horizontal, + textDirection: TextDirection.ltr, + children: [ + for (int i = 0; i < _searchController.historyList.length; i++) + SearchText( + searchText: _searchController.historyList[i], + searchTextIdx: i, + onSelect: (value) => _searchController.onSelect(value), + onLongSelect: (value) => + _searchController.onLongSelect(value), + ) + ], + ), + ), ], ), ),