import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';

class CommonRadioButtonDialog extends StatefulWidget {
  final List<String> list;
  final String title;
  final int selectedIndex;
  final Function(int) onSelect;
  CommonRadioButtonDialog({Key key, this.title = "", this.selectedIndex = 0, this.list, this.onSelect}) : super(key: key);

  @override
  _CommonRadioButtonDialogState createState() {
    return _CommonRadioButtonDialogState();
  }
}

class _CommonRadioButtonDialogState extends State<CommonRadioButtonDialog> {
  int _selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    _selectedIndex = widget.selectedIndex;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(4),
      ),
      elevation: 0,
      backgroundColor: Colors.white,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: widget.title == "" ? 24 : 50,
            alignment: Alignment.center,
            child: Text(
              widget.title,
              style: TextStyle(color: Colors.black87, fontSize: 18, fontWeight: FontWeight.w500),
            ),
          ),
          Divider(height: 1, color: Colors.black38),
          ListView.builder(
            shrinkWrap: true,
            padding: EdgeInsets.only(top: 4, bottom: 4),
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              return RadioListTile(
                value: index,
                dense: true,
                activeColor: Colors.black54,
                groupValue: _selectedIndex,
                onChanged: (_index) => setState(() => _selectedIndex = _index),
                title: Text(
                  widget.list[index],
                  style: TextStyle(fontWeight: FontWeight.w500),
                ),
              );
            },
            itemCount: widget.list?.length ?? 0,
          ),
          Divider(height: 1, color: Colors.black38),
          Container(
            height: 50,
            alignment: Alignment.center,
            child: Row(
              children: [
                Expanded(
                  child: FlatButton(
                    child: Text(
                      TranslationBase.of(context).cancel,
                      style: TextStyle(color: Colors.redAccent, fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                    onPressed: () => Navigator.pop(context),
                  ),
                ),
                Expanded(
                  child: FlatButton(
                    child: Text(
                      TranslationBase.of(context).ok,
                      style: TextStyle(color: Colors.black87, fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                    onPressed: () => widget.onSelect(_selectedIndex),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
