在flutter中使用带有flutter_bloc的控制器

时间:2020-10-26 15:06:05

标签: flutter google-maps flutter-bloc

在flutter_bloc中使用flutter_bloc中的(mapeContrller)或(PageController)这样的(contrllers)是什么好习惯,我应该在(bloc)或(UI)中还是在哪里添加它?这是我的例子:

config.dart:

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

GoogleMapController mapController;
PageController pageController = PageController(initialPage: 1, viewportFraction: 0.8);
final double mapInitialZoom = 13.0;

bloc.dart

@override
  Stream<GetPlacesStates> mapEventToState(GetPlacesEvent event) async*{
     if(event is GetPlacesStartEvent) {
       yield GetPlacesLoadingState(message: "جاري تحميل الأماكن");
       try {
         List<PlaceModel> places =  await repo.getPlaces();
         getAllMarkers(places);
         yield GetPlacesSuccessState(places: places, markers: markers);
       }catch(e) {
         yield GetPlacesErrorState(message: "لا يمكن جلب المواقع");
       }
     } else if (event is GetPlacesTileMarkerPressedEvent) {
       goToPlace(event.lat, event.lon, event.pid, event.listIndex);
     }
  }

  Future<void> goToPlace(double lat, double lon, String pid, int index) async {
    tileScrollTo(index);
    mapController.animateCamera(
        CameraUpdate.newLatLngZoom(LatLng(lat, lon), mapInitialZoom + 1));
    if (pid != null) mapController.showMarkerInfoWindow(MarkerId('$pid'));
  }

ui / map.dart

Widget buildMap(Set<Marker> marker) {
    return GoogleMap(
      markers: marker,
      mapType: MapType.satellite,
      initialCameraPosition: CameraPosition(
        target: _center,
        zoom: 15.0,
      ),
      onMapCreated: _onMapCreated,
    );
  }

0 个答案:

没有答案