Untitled

 avatar
user_6232722
plain_text
10 days ago
2.6 kB
1
Indexable
Never
import 'dart:developer';
import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart'
    hide ClusterManager, Cluster;
import 'package:lmc_user_app/features/home/bloc/home_bloc.dart';
import 'package:lmc_user_app/features/home/data/model/place_model.dart';
import '../../../bloc/home_state.dart';
import 'map_controller.dart';
import 'map_marker.dart';

class MapPage extends StatefulWidget {
  const MapPage({super.key});

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

class _MapPageState extends State<MapPage> {
  Set<Marker> markers = {};
  List<PlaceModel> placeList = [];
  late ClusterManager clusterManager;
  InteractiveMapsController controller = InteractiveMapsController();

  void _updateMarkers(Set<Marker> markers) {
    setState(() {
      this.markers = markers;
    });
  }

  Future<Marker> Function(dynamic) get markerBuilder => (cluster) async {
        return Marker(
          markerId: MarkerId(cluster.isMultiple
              ? cluster.getId()
              : cluster.items.single.id.toString()),
          position: cluster.location,
        );
      };

  ClusterManager _initClusterManager() {
    return ClusterManager<PlaceModel>(placeList, _updateMarkers,
        markerBuilder: markerBuilder);
  }

  @override
  void initState() {
    clusterManager = _initClusterManager();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocConsumer<HomeBusinessFetchBloc, HomeBusinessFetchState>(
        listener: (context, state) {
          log("123qwe${state.toString()}");
          if (state is HomeBusinessFetchLoaded) {
            setState(() {
              placeList = state.businesses.map((e) {
                return PlaceModel(
                    id: e.id,
                    latLng: LatLng(e.latitude ?? 0.0, e.longitude ?? 0.0));
              }).toList();
            });
          }
        },
        builder: (context, state) {
          log(markers.length.toString());
          if (state is HomeBusinessFetchLoading) {
            return const Center(child: CircularProgressIndicator());
          }
          return InteractiveMapsMarker(
            markers: markers,
            controller: controller,
            center: const LatLng(27.677433, 85.318545),
            zoom: 14,
            onLastItem: () {
              print('Last Item');
            },
          );
        },
      ),
    );
  }
}
Leave a Comment