mod: 搜索页跳转

This commit is contained in:
guozhigq
2024-01-07 20:50:15 +08:00
parent 042a0a848d
commit e8f7995b32
3 changed files with 117 additions and 132 deletions

View File

@ -161,7 +161,7 @@ class UserInfoWidget extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Row( return Row(
children: [ children: [
const Expanded(child: SearchPage()), const SearchBar(),
if (userLogin.value) ...[ if (userLogin.value) ...[
const SizedBox(width: 4), const SizedBox(width: 4),
ClipRect( 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),
),
),
),
],
),
),
),
),
);
}
}

View File

@ -20,7 +20,7 @@ class SSearchController extends GetxController {
final _debouncer = final _debouncer =
Debouncer(delay: const Duration(milliseconds: 200)); // 设置延迟时间 Debouncer(delay: const Duration(milliseconds: 200)); // 设置延迟时间
String hintText = '搜索'; String hintText = '搜索';
RxString defaultSearch = '输入关键词搜索'.obs; RxString defaultSearch = ''.obs;
Box setting = GStrorage.setting; Box setting = GStrorage.setting;
bool enableHotKey = true; bool enableHotKey = true;

View File

@ -1,6 +1,5 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:pilipala/common/widgets/http_error.dart'; import 'package:pilipala/common/widgets/http_error.dart';
import 'controller.dart'; import 'controller.dart';
@ -42,122 +41,62 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return OpenContainer( return Scaffold(
closedElevation: 0, resizeToAvoidBottomInset: false,
openElevation: 0, appBar: AppBar(
onClosed: (_) => _searchController.onClear(), shape: Border(
openColor: Theme.of(context).colorScheme.background, bottom: BorderSide(
middleColor: Theme.of(context).colorScheme.background, color: Theme.of(context).dividerColor.withOpacity(0.08),
closedColor: Colors.transparent, width: 1,
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)),
), ),
child: Material( ),
color: Theme.of(context) titleSpacing: 0,
.colorScheme actions: [
.onSecondaryContainer IconButton(
.withOpacity(0.05), onPressed: () => _searchController.submit(),
child: InkWell( icon: const Icon(CupertinoIcons.search, size: 22),
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,
),
),
),
),
],
),
),
), ),
); const SizedBox(width: 10)
}, ],
openBuilder: (BuildContext context, VoidCallback _) { title: Obx(
return Scaffold( () => TextField(
resizeToAvoidBottomInset: false, autofocus: true,
appBar: AppBar( focusNode: _searchController.searchFocusNode,
shape: Border( controller: _searchController.controller.value,
bottom: BorderSide( textInputAction: TextInputAction.search,
color: Theme.of(context).dividerColor.withOpacity(0.08), onChanged: (value) => _searchController.onChange(value),
width: 1, decoration: InputDecoration(
), hintText: _searchController.hintText,
), border: InputBorder.none,
titleSpacing: 0, suffixIcon: IconButton(
actions: [ icon: Icon(
Hero( Icons.clear,
tag: 'searchTag', size: 22,
child: IconButton( color: Theme.of(context).colorScheme.outline,
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(),
),
), ),
onSubmitted: (String value) => _searchController.submit(), onPressed: () => _searchController.onClear(),
), ),
), ),
onSubmitted: (String value) => _searchController.submit(),
), ),
body: SingleChildScrollView( ),
child: Column( ),
children: [ body: SingleChildScrollView(
const SizedBox(height: 12), child: Column(
// 搜索建议 children: [
_searchSuggest(), const SizedBox(height: 12),
// 热搜 // 搜索建议
Visibility( _searchSuggest(),
visible: _searchController.enableHotKey, // 热搜
child: hotSearch(_searchController)), Visibility(
// 搜索历史 visible: _searchController.enableHotKey,
_history() child: hotSearch(_searchController),
],
), ),
), // 搜索历史
); _history()
}, ],
),
),
); );
} }
@ -301,25 +240,24 @@ class _SearchPageState extends State<SearchPage> with RouteAware {
], ],
), ),
), ),
// if (_searchController.historyList.isNotEmpty) Obx(
Obx(() => Wrap( () => Wrap(
spacing: 8, spacing: 8,
runSpacing: 8, runSpacing: 8,
direction: Axis.horizontal, direction: Axis.horizontal,
textDirection: TextDirection.ltr, textDirection: TextDirection.ltr,
children: [ children: [
for (int i = 0; for (int i = 0; i < _searchController.historyList.length; i++)
i < _searchController.historyList.length; SearchText(
i++) searchText: _searchController.historyList[i],
SearchText( searchTextIdx: i,
searchText: _searchController.historyList[i], onSelect: (value) => _searchController.onSelect(value),
searchTextIdx: i, onLongSelect: (value) =>
onSelect: (value) => _searchController.onSelect(value), _searchController.onLongSelect(value),
onLongSelect: (value) => )
_searchController.onLongSelect(value), ],
) ),
], ),
)),
], ],
), ),
), ),