import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'dart:ui';

///
/// 搜索联想页面
///
class SearchThinkPage extends StatefulWidget {
  String keywords;
  final Map<String, dynamic> data;

  SearchThinkPage(this.data, {Key key}) : super(key: key) {
    try {} catch (_) {}
  }

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

class _SearchThinkPageState extends State<SearchThinkPage> {
  TextEditingController _textEditingController;
  FocusNode _focusNode;
  TabController _tabController;

  /// 打开搜索结果
  void _openSearchResult(String keywords) {}

  /// 关闭当前页面
  void _closePage() {
    Navigator.maybePop(context);
  }

  /// 点击联想结果页面
  void _onClickThinkResult(String keywords) {}

  @override
  void initState() {
    _textEditingController = TextEditingController(text: widget?.keywords);
    _focusNode = FocusNode();
    super.initState();
  }

  @override
  void dispose() {
    _textEditingController?.dispose();
    _focusNode?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        margin: EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top + 7.5),
        color: HexColor.fromHex('#FFFFFF'),
        width: double.infinity,
        child: Column(
          children: <Widget>[
            /// 标题
            _buildTitleWidget(),

            /// 联想结果
            Expanded(
              child: _buildThinkWidget(null),
            )
          ],
        ),
      ),
    );
  }

  /// 联想结果
  Widget _buildThinkWidget(List datas) {
    int length = datas?.length ?? 0;
    return Visibility(
      visible: length > 0,
      child: ListView.builder(
        itemBuilder: (context, item) {
          return Container(
            padding: const EdgeInsets.symmetric(vertical: 13),
            child: Text('', style: TextStyle(color: HexColor.fromHex('#333333'), fontSize: 14)),
          );
        },
        itemCount: length,
      ),
    );
  }

  /// titile
  Widget _buildTitleWidget() {
    return Container(
      height: 32,
      width: double.infinity,
      child: Row(
        children: <Widget>[
          /// 输入框
          Expanded(
            child: _buildInputWidget(),
          ),

          /// 取消按钮
          _buildButtomWidget(),
        ],
      ),
    );
  }

  /// 取消按钮
  Widget _buildButtomWidget() {
    return GestureDetector(
      onTap: () => _closePage(),
      child: Container(
        padding: const EdgeInsets.only(right: 12.5, left: 10, bottom: 6, top: 6),
        child: Text('取消', style: TextStyle(fontSize: 14, color: HexColor.fromHex('#999999'))),
      ),
    );
  }

  /// 输入框
  Widget _buildInputWidget() {
    return Container(
      height: 32,
      margin: const EdgeInsets.only(left: 12.5),
      decoration: BoxDecoration(
        color: HexColor.fromHex('#F9F9F9'),
        borderRadius: BorderRadius.circular(30),
      ),
      child: TextField(
        controller: _textEditingController,
        focusNode: _focusNode,
        style: TextStyle(color: HexColor.fromHex('#333333'), fontSize: 13, textBaseline: TextBaseline.alphabetic),
        decoration: InputDecoration(
          contentPadding: EdgeInsets.only(left: 12.5, right: 12.5, bottom: 12),
          fillColor: Colors.transparent,
          filled: true,
          border: InputBorder.none,
          errorBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
          focusedErrorBorder: InputBorder.none,
          focusedBorder: InputBorder.none,
        ),
      ),
    );
  }
}
