content: import 'package:flutter/material.dart';
  
  /// 搜索栏组件
  class SearchBarWidget extends StatefulWidget {
    final Function(String) onSearch;
    final String hintText;
    final bool autofocus;
    final TextEditingController? controller;
    final VoidCallback? onClear;
  
    const SearchBarWidget({
      super.key,
      required this.onSearch,
      this.hintText = '搜索账单',
      this.autofocus = false,
      this.controller,
      this.onClear,
    });
  
    @override
    State<SearchBarWidget> createState() => _SearchBarWidgetState();
  }
  
  class _SearchBarWidgetState extends State<SearchBarWidget> {
    late TextEditingController _controller;
    bool _hasText = false;
  
    @override
    void initState() {
      super.initState();
      _controller = widget.controller ?? TextEditingController();
      _controller.addListener(_onTextChanged);
    }
  
    @override
    void dispose() {
      if (widget.controller == null) {
        _controller.dispose();
      }
      super.dispose();
    }
  
    void _onTextChanged() {
      setState(() {
        _hasText = _controller.text.isNotEmpty;
      });
    }
  
    void _clearSearch() {
      _controller.clear();
      widget.onClear?.call();
    }
  
    @override
    Widget build(BuildContext context) {
      return Container(
        height: 48,
        margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        decoration: BoxDecoration(
          color: Colors.grey[200],
          borderRadius: BorderRadius.circular(24),
        ),
        child: TextField(
          controller: _controller,
          autofocus: widget.autofocus,
          textInputAction: TextInputAction.search,
          decoration: InputDecoration(
            hintText: widget.hintText,
            prefixIcon: const Icon(Icons.search),
            suffixIcon:
                _hasText
                    ? IconButton(
                      icon: const Icon(Icons.clear),
                      onPressed: _clearSearch,
                    )
                    : null,
            border: InputBorder.none,
            contentPadding: const EdgeInsets.symmetric(vertical: 12),
          ),
          onSubmitted: widget.onSearch,
        ),
      );
    }
  }
  