
view
利用内置的Chip简化布局- import 'package:chenyanzhenxuan/common/index.dart';
- import 'package:ducafe_ui_core/ducafe_ui_core.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:tdesign_flutter/tdesign_flutter.dart';
- import 'index.dart';
- class SearchGoodsPage extends GetView<SearchGoodsController> {
- const SearchGoodsPage({super.key});
- // 搜索
- Widget _buildSearch() {
- return <Widget>[
- SearchWidget(
- type: 'input',
- inputBgColor: AppTheme.pageBgColor,
- controller: controller.searchController,
- onChange: (value) {
- controller.onSearchChange(value);
- },
- ),
- ].toRow()
- .paddingAll(30.w)
- .card(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)));
- }
- // 热门搜索
- Widget _buildHotSearch() {
- return <Widget>[
- <Widget>[
- TDImage(assetUrl: 'assets/img/hot.png',width: 34.w,height: 34.w,),
- SizedBox(width: 10.w,),
- TextWidget.body('热门搜索',size: 28.sp,),
- ].toRow().paddingHorizontal(30.w),
- <Widget>[
- for(var i = 0; i < 6; i++)
- Chip(
- label: TextWidget.body('热门搜索$i',size: 24.sp,color: AppTheme.color999,),
- backgroundColor: AppTheme.blockBgColor,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.w)),
- )
- ].toWrap(spacing: 20.w,runSpacing: 0,).paddingLeft(30.w),
- ].toColumn(crossAxisAlignment: CrossAxisAlignment.start);
- }
- // 历史记录
- Widget _buildHistory(BuildContext context) {
- return <Widget>[
- <Widget>[
- TextWidget.body('历史记录',size: 28.sp,),
- TDImage(assetUrl: 'assets/img/hot-del.png',width: 28.w,height: 32.w,).onTap(() {
- showGeneralDialog(
- context: context,
- pageBuilder: (BuildContext buildContext, Animation<double> animation,
- Animation<double> secondaryAnimation) {
- return TDAlertDialog(
- title: '清空搜索历史',
- content: '请确定清空搜索历史?',
- buttonWidget: <Widget>[
- <Widget>[const TextWidget.body('取消')]
- .toRow(mainAxisAlignment: MainAxisAlignment.center)
- .card(color: const Color(0xffeeeeee))
- .tight(width: 240.w, height: 80.w)
- .onTap(() => Navigator.of(context).pop()),
- <Widget>[
- const TextWidget.body(
- '确定',
- color: Colors.white,
- )
- ]
- .toRow(mainAxisAlignment: MainAxisAlignment.center)
- .card(color: const Color(0xffE93323))
- .tight(width: 240.w, height: 80.w)
- .onTap(() {
- controller.clearSearchHistory();
- Navigator.of(context).pop();
- }),
- ]
- .toRow(mainAxisAlignment: MainAxisAlignment.spaceBetween)
- .paddingOnly(left: 30.w, right: 30.w, bottom: 40.w),
- );
- },
- );
- }),
- ].toRow(mainAxisAlignment: MainAxisAlignment.spaceBetween).paddingHorizontal(30.w),
- <Widget>[
- for(var i = 0; i < controller.searchHistory.length; i++)
- Chip(
- label: TextWidget.body(controller.searchHistory[i],size: 24.sp,color: AppTheme.color999,),
- backgroundColor: AppTheme.blockBgColor,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.w)),
- deleteIconColor: AppTheme.color999,
- onDeleted: () {
- controller.deleteSearchHistory(controller.searchHistory[i]);
- },
- )
- ].toWrap(spacing: 20.w,runSpacing: 0,).paddingLeft(30.w),
- ].toColumn(crossAxisAlignment: CrossAxisAlignment.start);
- }
- // 主视图
- Widget _buildView(BuildContext context) {
- return <Widget>[
- _buildSearch(),
- // SizedBox(height: 30.w,),
- // _buildHotSearch(),
- SizedBox(height: 30.w,),
- _buildHistory(context),
- controller.searchHistory.isEmpty ? const EmptyState() : const SizedBox()
- ].toColumn();
- }
- @override
- Widget build(BuildContext context) {
- return GetBuilder<SearchGoodsController>(
- init: SearchGoodsController(),
- id: "search_goods",
- builder: (_) {
- return Scaffold(
- backgroundColor: AppTheme.pageBgColor, // 自定义颜色
- appBar: const TDNavBar(
- height: 44,
- title: "搜索",
- titleFontWeight: FontWeight.w600,
- backgroundColor: Colors.white,
- screenAdaptation: true,
- useDefaultBack: true,
- ),
- body: SingleChildScrollView(
- child: _buildView(context),
- ),
- );
- },
- );
- }
- }
复制代码 controller- import 'package:chenyanzhenxuan/common/index.dart';
- import 'package:get/get.dart';
- import 'package:flutter/material.dart';
- class SearchGoodsController extends GetxController {
- SearchGoodsController();
- // 搜索框
- final searchController = TextEditingController();
- // 搜索关键词
- String searchKey = '';
- // 搜索历史
- List<String> searchHistory = [];
- @override
- void onReady() {
- super.onReady();
- _initData();
- }
- @override
- void onClose() {
- super.onClose();
- searchController.dispose();
- }
- // 初始化数据
- void _initData() {
- searchHistory = Storage().getList('search_history');
- update(["search_goods"]);
- }
- // 搜索框发送改变
- void onSearchChange(String value) {
- if(value.isEmpty) return Loading.toast("请输入搜索内容");
- searchController.text = value;
- // 查找是否存在该搜索词
- final index = searchHistory.indexOf(value);
- // 如果已存在,删除旧值
- if (index != -1) {
- searchHistory.removeAt(index);
- }
- // 将新搜索词添加到数组开头
- searchHistory.insert(0, value);
- // 限制历史记录条数
- if (searchHistory.length > 20) {
- searchHistory = searchHistory.sublist(0, 20);
- }
- // 保存更新后的搜索历史
- Storage().setList('search_history', searchHistory);
- // 使用 Get.toNamed 的返回回调来刷新数据
- Get.toNamed('/goods_list_page', arguments: {
- 'searchValue': value,
- 'title': '搜索',
- 'type': 1,
- })?.then((_) {
- searchController.text = '';
- _initData(); // 返回时重新加载数据
- });
- update(["search_goods"]);
- }
- // 清空搜索历史
- void clearSearchHistory() {
- searchHistory = [];
- Storage().setList('search_history', searchHistory);
- update(["search_goods"]);
- }
- // 删除单个历史记录
- void deleteSearchHistory(String value) {
- searchHistory.remove(value);
- Storage().setList('search_history', searchHistory);
- update(["search_goods"]);
- }
-
- }
复制代码 SearchWidget- import 'package:ducafe_ui_core/ducafe_ui_core.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:tdesign_flutter/tdesign_flutter.dart';
- import 'package:chenyanzhenxuan/common/index.dart';
- class SearchWidget extends StatelessWidget {
- // 类型
- final String type;
- // 搜索框控制器
- final TextEditingController? controller;
- // 输入框背景色
- final Color? inputBgColor;
- // 搜索框发送改变
- final Function(String value)? onChange;
- const SearchWidget({
- super.key,
- required this.type,
- this.controller,
- this.inputBgColor,
- this.onChange,
- });
- @override
- Widget build(BuildContext context) {
- return <Widget>[
- <Widget>[
- TDImage(
- assetUrl: type == 'text' ? 'assets/img/search.png' : 'assets/img/search.png',
- width: 32.w,
- height: 32.w,
- ),
- SizedBox(width: 20.w),
- type == 'text' ? TextWidget.body('搜索您要找的商品名称', size: 28.sp, color: AppTheme.color999)
- : InputWidget(
- placeholder: "搜索您要找的商品名称",
- controller: controller,
- ).width(450.w),
- ].toRow(),
- <Widget>[
- TextWidget.body('搜索', size: 26.sp, color: AppTheme.colorfff),
- ].toRow(mainAxisAlignment: MainAxisAlignment.center)
- .card(color: AppTheme.error,shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.w)))
- .tight(width: 120.w,height: 60.w)
- .onTap(() {
- if(type == 'text'){
- Get.toNamed('/search_goods_page');
- }else{
- if(type == 'input' && controller?.text != null){
- onChange!(controller!.text);
- }
- }
- }),
- ]
- .toRow(mainAxisAlignment: MainAxisAlignment.spaceBetween)
- .paddingOnly(left: 30.w,right: 4.w)
- .card(
- color: inputBgColor,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(30.w)
- )
- )
- .tight(width: 690.w, height: 68.w)
- .onTap(() {
- if(type == 'text'){
- Get.toNamed('/search_goods_page');
- }
- });
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|