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

import 'load_image.dart';

/// 搜索页的AppBar
class SearchAppBar extends StatefulWidget implements PreferredSizeWidget {
  SearchAppBar({
    Key key,
    this.hintText: "",
    this.searchText,
    this.onPressed,
    this.autoFocus = true,
  }) : super(key: key);

  final String hintText;
  String searchText;
  final Function(String) onPressed;
  final bool autoFocus;

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

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

class _SearchAppBarState extends State<SearchAppBar> {
  SystemUiOverlayStyle overlayStyle = SystemUiOverlayStyle.light;
  TextEditingController _controller = TextEditingController();

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

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;

    bool isDark = ThemeUtils.isDark(context);
    overlayStyle =
        isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;
    Color iconColor = Color(0xFF999999);

    _controller.text = widget.searchText ?? '';

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle,
      child: Material(
        color: ThemeUtils.getBackgroundColor(context),
        child: SafeArea(
          child: Container(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Row(
                // mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  if (Navigator.canPop(context)) ...[
                    InkWell(
                      onTap: () {
                        FocusScope.of(context).unfocus();
                        Navigator.maybePop(context);
                      },
                      child: Container(
                        child: Icon(
                          Iconfont.fanhui,
                          size: 17,
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 20,
                    ),
                  ],
                  Expanded(
                    child: Container(
                      height: 30.0,
                      decoration: BoxDecoration(
                        color: ThemeUtils.getDialogTextFieldColor(context),
                        borderRadius: BorderRadius.circular(17.5),
                      ),
                      child: TextField(
                        key: const Key('srarch_text_field'),
                        autofocus: widget.autoFocus,
                        controller: _controller,
                        maxLines: 1,
                        style: TextStyle(fontSize: 14),
                        // textInputAction:'',
                        onSubmitted: (value) {
                          widget.onPressed(_controller.text);
                          FocusScope.of(context).requestFocus(FocusNode());
                        },
                        decoration: InputDecoration(
                          contentPadding: const EdgeInsets.only(
                              top: -3.0,
                              left: -8.0,
                              right: -16.0,
                              bottom: 14.0),
                          border: InputBorder.none,
                          icon: Padding(
                            padding: const EdgeInsets.only(left: 10.0),
                            child: Icon(
                              Iconfont.sousuo,
                              size: 16,
                              color: Color(0xff6A6A76),
                            ),
                          ),
                          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((_) {
                                widget.searchText = '';
                                _controller.text = "";
                              });
                            },
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 20,
                  ),
                  GestureDetector(
                    onTap: () {
                      widget.onPressed(_controller.text);
                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                    child: Text("搜索",
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0xff343434),
                        )),
                  ),
                ],
              )),
        ),
      ),
    );
  }
}
