123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- import 'package:flutter/material.dart';
- import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
- import 'package:get/get.dart';
- import 'package:cs_resources/generated/assets.dart';
- import 'package:widgets/ext/ex_widget.dart';
- import '../utils/dark_theme_util.dart';
- import 'my_load_image.dart';
- import 'package:cs_resources/constants/color_constants.dart';
- class SearchAppBar extends StatefulWidget {
- SearchAppBar({
- Key? key,
-
- this.borderRadius = 5,
- this.autoFocus = false,
- this.focusNode,
- this.controller,
- this.height = 35,
- this.value,
-
- this.backgroundColor,
- this.suffix,
- this.actions = const [],
- this.hintText,
- this.onTap,
- this.onClear,
- this.onCancel,
- this.onChanged,
- this.onSearch,
-
- }) : super(key: key);
- final double? borderRadius;
- final bool? autoFocus;
- final FocusNode? focusNode;
- final TextEditingController? controller;
-
- final double height;
-
- final String? value;
-
-
-
-
-
- final Color? backgroundColor;
-
- final Widget? suffix;
-
- final List<Widget> actions;
-
- final String? hintText;
-
- final VoidCallback? onTap;
-
- final VoidCallback? onClear;
-
- final VoidCallback? onCancel;
-
- final ValueChanged<String>? onChanged;
-
- final ValueChanged<String>? onSearch;
-
-
- @override
- _SearchAppBarState createState() => _SearchAppBarState();
- }
- class _SearchAppBarState extends State<SearchAppBar> {
- TextEditingController? _controller;
- FocusNode? _focusNode;
- bool get isFocus => _focusNode?.hasFocus ?? false;
- bool get isTextEmpty => _controller?.text.isEmpty ?? false;
- bool get isActionEmpty => widget.actions.isEmpty;
- bool isShowCancel = false;
- @override
- void initState() {
- _controller = widget.controller ?? TextEditingController();
- _focusNode = widget.focusNode ?? FocusNode();
- if (widget.value != null) _controller?.text = widget.value ?? "";
-
- _focusNode?.addListener(() => setState(() {}));
-
- _controller?.addListener(() {
- setState(() {});
- });
- super.initState();
- }
-
- void _onClearInput() {
- setState(() {
- _controller?.clear();
- });
- widget.onClear?.call();
- }
-
- void _onCancelInput() {
- setState(() {
- _controller?.clear();
- _focusNode?.unfocus();
- });
-
- widget.onCancel?.call();
- }
- Widget _suffix() {
- if (!isTextEmpty) {
- return InkWell(
- onTap: _onClearInput,
- child: SizedBox(
- width: widget.height,
- height: widget.height,
- child: Icon(Icons.cancel, size: 22, color: DarkThemeUtil.multiColors(const Color(0xFF999999), darkColor: Colors.white)),
- ),
- );
- }
- return widget.suffix ?? SizedBox();
- }
- List<Widget> _actions() {
- List<Widget> list = [];
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return list;
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- margin: const EdgeInsetsDirectional.only(end: 10),
- height: widget.height,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(widget.borderRadius ?? 0),
- border: Border.all(width: 0.5, color: DarkThemeUtil.multiColors(Colors.black, darkColor: Colors.white)!),
- ),
- child: Row(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
-
- SizedBox(width: 10),
-
- MyAssetImage(
- Assets.baseLibBlackBack,
- width: 16,
- height: 16,
- color: DarkThemeUtil.multiColors(null, darkColor: Colors.white),
- ).onTap(() {
- String? searchStr = _controller?.text;
- if (widget.onSearch != null) {
- widget.onSearch!(searchStr!);
- }
- }),
-
- SizedBox(width: 8),
-
- Expanded(
-
- child: IgnoreKeyboardDismiss(
- child: TextField(
- cursorColor: ColorConstants.appBlue,
- autofocus: widget.autoFocus ?? false,
-
- focusNode: _focusNode,
-
- controller: _controller,
-
-
- decoration: InputDecoration(
- isDense: true,
-
- isCollapsed: true,
-
- border: InputBorder.none,
- hintText: widget.hintText ?? '搜索'.tr,
- hintStyle: const TextStyle(
- fontSize: 15,
- color: const Color(0xff777777),
- fontWeight: FontWeight.w500,
- ),
- ),
- style: TextStyle(
- fontSize: 15,
- color: DarkThemeUtil.multiColors(Colors.black, darkColor: Colors.white),
- fontWeight: FontWeight.w500,
- ),
-
- textInputAction: TextInputAction.search,
- textAlignVertical: TextAlignVertical.center,
- onTap: widget.onTap,
-
- onChanged: widget.onChanged,
- onSubmitted: widget.onSearch,
- ),
- ),
- ),
- _suffix(),
- ],
- ),
- );
- }
- @override
- void dispose() {
- _controller?.dispose();
- _focusNode?.dispose();
- super.dispose();
- }
- }
|