import 'package:flutter/material.dart';
int hourSelection = 1;
FixedExtentScrollController scrollController = FixedExtentScrollController();
Future<void> showDurationSelector(BuildContext context, {required Function(Duration) onChange, int? selectedHour}) async {

  hourSelection = selectedHour ?? 1;

  scrollController = FixedExtentScrollController(initialItem: hourSelection-1);
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {

      return StatefulBuilder(
        builder:(context, setState) {
          setState((){});
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(30)),
            backgroundColor: Color(0xFF1F1F1F),
            title: Align(alignment: Alignment.center,
                child: const Text('Select Time Range')),
            content: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  height: 150,
                  width: 50,
                  child: ListWheelScrollView(
                    // useMagnifier: true,
                    controller: scrollController,
                    diameterRatio: 1,
                    itemExtent: 30,
                    children: printHours(),

                    onSelectedItemChanged: (index) {
                      hourSelection = index + 1;
                      setState(() {});
                      //scrollController.jumpTo(index.toDouble());
                      print("Changed to $index");
                    },
                  ),

                ),
                Text(' Hours')
              ],
            ),
            actions: <Widget>[
              TextButton(
                child: const Text('Select'),
                onPressed: () {
                  onChange(Duration(hours: hourSelection));
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        }
      );
    },
  );
}


List<Widget> printHours(){
  List<Widget> _hours = [];
  for(int i = 0; i < 5000; i++){
    _hours.add(Container(decoration: BoxDecoration(borderRadius: BorderRadius.circular(5), color:(hourSelection-1==i) ? Colors.redAccent :null ),alignment:Alignment.center,width:50,child: Text((i+1).toString())));
  }

  return _hours;
}