import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/utils/theme_utils.dart';

import 'load_image.dart';

/// 搜索页的AppBar
class SearchBar extends StatefulWidget implements PreferredSizeWidget{

  const SearchBar({
    Key key,
    this.hintText: "",
    this.autofocus:true,
    this.onPressed,
  }): super(key: key);

  final String hintText;
  final bool autofocus;
  final Function(String) onPressed;

  @override
  _SearchBarState createState() => _SearchBarState();

  @override
  Size get preferredSize => Size.fromHeight(48.0);
}

class _SearchBarState extends State<SearchBar> {

  SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  TextEditingController _controller = TextEditingController();

//  Color getColor(){
//    return overlayStyle == SystemUiOverlayStyle.light ? Colours.dark_text : Colours.text;
//  }

  @override
  Widget build(BuildContext context) {
//    bool isDark = ThemeUtils.isDark(context);
//    overlayStyle = isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
    Color iconColor = Color(0xFF999999);
    return Material(
//        color: ThemeUtils.getBackgroundColor(context),
        child: Container(
            height: 32.0,
            decoration: BoxDecoration(
              color: Color(0xFFE8E9E8),
              borderRadius: BorderRadius.circular(17.5),
            ),
            child: TextField(
              key: const Key('srarch_text_field'),
              autofocus: widget.autofocus,
              controller: _controller,
              maxLines: 1,
              onChanged: widget.onPressed,
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.only(top: 0.0, left: -8.0, right: -16.0, bottom: 14.0),
                border: InputBorder.none,
                icon: Padding(
                  padding: const EdgeInsets.only(top: 8.0, bottom: 8.0, left: 10.0),
                  child: LoadAssetImage("icon_search", color: iconColor,),
                ),
                hintText: widget.hintText,
                hintStyle: TextStyle(
                    color: Color(0xFF999999)
                ),
                suffixIcon: InkWell(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 16.0, top: 8.0, bottom: 8.0,right: 10),
                    child: LoadAssetImage("icon_delete", color: iconColor),
                  ),
                  onTap: (){
                    /// https://github.com/flutter/flutter/issues/35909
                    SchedulerBinding.instance.addPostFrameCallback((_) {
                      _controller.text = "";
                    });
                  },
                ),
              ),
            ),
          ),

    );
  }
}
