import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/progressbar/progress_indicator.dart';
import 'package:flutter/material.dart';
import 'package:fis_common/event/event_type.dart';

class FCustomProgressBar extends FStatefulWidget {
  final FEventHandler<double> onProcessChange;

  FCustomProgressBar({Key? key, required this.onProcessChange})
      : super(key: key);

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

class _FCustomProgressBarState extends FState<FCustomProgressBar> {
  double _processValue = 0;
  @override
  void initState() {
    widget.onProcessChange.addListener(_onProcessChange);
    super.initState();
  }

  @override
  void dispose() {
    widget.onProcessChange.removeListener(_onProcessChange);
    super.dispose();
  }

  @override
  FWidget build(BuildContext context) {
    return FRow(
      children: [
        FExpanded(
          child: FLinearProgressIndicator(
            value: _processValue,
            backgroundColor: Colors.grey,
          ),
        ),
        FText('  ${(_processValue * 100).toStringAsFixed(0)} %'),
      ],
    );
  }

  void _onProcessChange(Object sender, double e) {
    setState(() {
      _processValue = e;
    });
  }
}
