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

class ActionListAlert extends StatefulWidget {
  final String title;
  final List<String> actions;
  final TextStyle normalStyle;
  final TextStyle selectedStyle;

  const ActionListAlert(
      {Key key, this.title, this.actions, this.normalStyle, this.selectedStyle})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _ActionListAlert();
}

class _ActionListAlert extends State<ActionListAlert> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: GestureDetector(
        child: Column(
          children: <Widget>[
            Expanded(
              child: Container(),
            ),
            Container(
              width: double.infinity,
              height: 250,
              margin: EdgeInsets.all(0),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(10),
                      topRight: Radius.circular(10))),
              child: Column(
                children: <Widget>[
                  widget.title == null || widget.title == ''
                      ? Container()
                      : Container(
                          height: 44,
                          child: Row(
                            children: <Widget>[
                              FlatButton(
                                child: Text('取消'),
                                onPressed: () {
                                  Navigator.pop(context);
                                },
                              ),
                              Expanded(
                                child: Center(
                                  child: Text(
                                    widget.title,
                                    style: TextStyle(
                                        fontSize: 13,
                                        color: Colors.black.withAlpha(180)),
                                  ),
                                ),
                              ),
                              FlatButton(
                                child: Text(
                                  '完成',
                                  style: TextStyle(
                                      color: HexColor.fromHex('#E52425')),
                                ),
                                onPressed: () {
                                  Navigator.pop(context, _currentIndex);
                                },
                              ),
                            ],
                          )),
                  Expanded(
                    child: _createContent(context),
                  )
                ],
              ),
            ),
          ],
        ),
        onTap: () {
          Navigator.pop(context);
        },
      ),
    );
  }

  Widget _createContent(BuildContext context) {
    List<Widget> widgets = List<Widget>();

    TextStyle normalStyle = widget.normalStyle ??
        TextStyle(fontSize: 12, color: HexColor.fromHex('#7f7f7f'));
    TextStyle selectedStyle = widget.selectedStyle ??
        TextStyle(fontSize: 14, color: HexColor.fromHex('#E52425'));

    for (int index = 0; index < widget.actions.length; index++) {
      int i = index;
      widgets.add(Column(
        children: <Widget>[
          Container(
            // color: HexColor.random(),
            height: 40,
            child: Center(
              child: Text(
                widget.actions[index],
                style: i == _currentIndex ? selectedStyle : normalStyle,
              ),
            ),
          )
        ],
      ));
    }
    return CupertinoPicker(
      backgroundColor: Colors.white,
      children: widgets,
      itemExtent: 40,
      onSelectedItemChanged: (int index) {
        setState(() {
          _currentIndex = index;
        });
      },
    );
  }
}
