import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../tool/YSTools.dart';

class YSSearchView extends StatefulWidget {
  final ValueSetter postSearch;
  const YSSearchView({Key? key, required this.postSearch}) : super(key: key);

  @override
  YSSearchViewState createState() => YSSearchViewState();
}

class YSSearchViewState extends State<YSSearchView> {
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
      height: hsp(42),
      width: ysWidth(context)-hsp(40),
      decoration: const BoxDecoration(
          color: Color(0xFF1A1C1F),
          borderRadius: BorderRadius.all(Radius.circular(3))
      ),
      child: CupertinoTextField(
        placeholder: '搜索内容',
        style: TextStyle(fontSize: zsp(14),color: Colors.white),
        placeholderStyle: TextStyle(fontSize: zsp(14),color: const Color(0xFFD5D5D5)),
        decoration: const BoxDecoration(color: Colors.transparent),
        prefix: Image.asset('images/tzh_search.png',height: hsp(20),width: hsp(20),),
        suffix: _editingController.text.isNotEmpty?GestureDetector(
          onTap: (){
            _editingController.text = '';
            FocusScope.of(context).unfocus();
            widget.postSearch('');
          },
          child: Image.asset('images/tzh_clear.png',height: hsp(20),width: hsp(20),),
        ):Container(),
        onChanged: (value){
          setState(() {});
        },
        controller:_editingController,
        textInputAction: TextInputAction.search,
        onSubmitted: (value){
          FocusScope.of(context).unfocus();
          widget.postSearch(value);
        },
      ),
    )
    ;
  }
}
