Untitled

filter_dialog
 avatar
unknown
dart
5 months ago
6.8 kB
1
Indexable
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../core/extensions/context_extensions.dart';
import '../../domain/entities/charger_station.dart';
import '../../domain/entities/filter.dart';

import '../../../../core/res/colours.dart';

class FilterDialog extends StatefulWidget {
  const FilterDialog({
    super.key,
    required this.onFilter,
  });

  final Function(Filter) onFilter;

  @override
  State<FilterDialog> createState() => _FilterDialogState();
}

class _FilterDialogState extends State<FilterDialog> {
  double _value = 0;
  List<String> _selectedConnectors = [];

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          height: MediaQuery.of(context).size.height * 0.57,
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(30),
              topRight: Radius.circular(30),
            ),
          ),
          child: SafeArea(
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 24),
              child: Column(
                mainAxisSize: MainAxisSize.max,
                children: [
                  const Text(
                    'Filter',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const Divider(color: Colours.secondaryColourDisabled),
                  Expanded(
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          const Text(
                            'Minimum Power',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              SizedBox(
                                width: MediaQuery.of(context).size.width * 0.6,
                                child: Slider(
                                  thumbColor: Colours.secondaryColour,
                                  activeColor: Colours.secondaryColour,
                                  inactiveColor:
                                      Colours.secondaryColourDisabled,
                                  min: 0.0,
                                  max: 240.0,
                                  value: _value,
                                  divisions: 100,
                                  label: '${_value.round()} kW',
                                  onChanged: (value) {
                                    setState(() {
                                      _value = value.roundToDouble();
                                    });
                                  },
                                ),
                              ),
                              Text(
                                '${_value.round()} kW',
                                style: const TextStyle(fontSize: 16),
                              ),
                            ],
                          ),
                          const SizedBox(height: 10),
                          const Text(
                            'Connector',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          Column(
                            children: [
                              _buildConnectorRow('CCS1'),
                              _buildConnectorRow('CCS2'),
                              _buildConnectorRow('Type 1'),
                              _buildConnectorRow('Type 2'),
                              _buildConnectorRow('J1772'),
                              _buildConnectorRow('CHAdeMO'),
                            ],
                          ),
                          const SizedBox(height: 10),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        Positioned(
          bottom: 20,
          child: SizedBox(
            width: MediaQuery.of(context).size.width,
            height: 50,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildActionButton('Save', Colours.greenColour, () {
                  debugPrint(_selectedConnectors.toString());
                  debugPrint(_value.toString());
                  final filter = Filter(
                    minimumPower: _value,
                    connectors: _selectedConnectors,
                    chargeStations:
                        context.chargeStationsProvider.chargeStations ?? [],
                  );
                  widget.onFilter(filter);
                  Navigator.of(context).pop();
                }),
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildConnectorRow(String connectorType) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(connectorType),
        Checkbox(
          value: _selectedConnectors.contains(connectorType),
          onChanged: (selected) {
            setState(() {
              if (selected != null && selected) {
                if (!_selectedConnectors.contains(connectorType)) {
                  _selectedConnectors.add(connectorType);
                }
              } else {
                _selectedConnectors.remove(connectorType);
              }
            });
          },
        ),
      ],
    );
  }

  Widget _buildActionButton(String text, Color color, VoidCallback onPressed) {
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.45,
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: color,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5),
          ),
        ),
        child: Text(text, style: const TextStyle(color: Colors.white)),
      ),
    );
  }
}
Editor is loading...
Leave a Comment