gpt4 book ai didi

flutter - flutter_map通过BLoC使用 map Controller 移动相机

转载 作者:行者123 更新时间:2023-12-03 03:56:12 26 4
gpt4 key购买 nike

我正在使用BLoC模式和flutter_map包构建一个flutter应用程序。我想将相机移到特定位置。我正在尝试将 map Controller 传递给我的bloc结构并从那里移动相机,但即时通讯出现错误:NoSuchMethodError: The getter 'onReady' was called on null.我不确定这是否是正确的方法。

class HomePage extends StatelessWidget {
const HomePage({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
...,
BlocProvider<MapBloc>(
create: (BuildContext context) => MapBloc(mapController: MapController()) // passing map controller
..add(MapDataInit()),
)
],
...
);
}
}

map_bloc.dart
class MapBloc extends Bloc<MapEvent, MapState> {
final MapController mapController;
LocationRepository _locationRepository = LocationRepository();

MapBloc({@required this.mapController});

@override
get initialState => MapDataUninitialized();

@override
Stream<MapState> mapEventToState(MapEvent event) async* {
final currentState = state;

if (event is AddMarker) {
yield MapDataLoaded(
mapController: this.mapController,
markers: [...]);
this.add(MoveCamera(event.latLan)); // not sure about this
}
if (event is MoveCamera) {
mapController.onReady.then((result) { // i'm getting an error here
mapController.move(event.latLan, 15.0);
});
}
}
}



带有 map 的小部件
class SelectLocationView extends StatelessWidget {
Widget build(BuildContext context) {
return BlocBuilder<MapBloc, MapState>(
builder: (context, state) {
...
if (state is MapDataLoaded) {
return Container(
child: Center(
child: Container(
child: FlutterMap(
mapController: state.mapController, // I'm trying to get previously defined controller
options: MapOptions(...),
layers: [
TileLayerOptions(
urlTemplate:
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: ['a', 'b', 'c']),
MarkerLayerOptions(...),
],
))),
);
}
},
);
}
}

我不知道为什么 map Controller 的onReady方法有问题。

最佳答案

我在使用GetX时遇到了类似的问题。
我通过应用一些前提来决定:首先,我将对 map 的所有操作都保留在Widget(无状态)中,因为我需要在google插件和flutter_map之间进行切换。
然后,在 Controller 中(以您的BloC为例),它仅触发必要的信息,以便 View 从中接收必须居中的新位置,即,但 View 是知道 map 并使其居中而不是居中的 View BloC
简而言之,我必须使用静态变量来在应用程序中保留mapController的单例引用,因为“onReady()”方法仅被调用了一次,因此我保留了 bool(boolean) 值来控制 map 的状态。 “onReady”不会执行,我们无法访问 map 对象,例如“zoom”和“move”。
我的示例代码:

class FlutterMapWidget extends StatelessWidget {

static MapController _mapController;
bool isMapRead = false;

FlutterMapWidget() {
// create instance only not exists another reference
if(_mapController == null) {
_mapController = MapController();
}

// after onReady, flag local control variable
_mapController.onReady.then((v) {
isMapRead = _mapController.ready;
});
}

@override
Widget build(BuildContext context) {

return Stack(
children: [
_buildMap(),
// add another map components ...
],
);

}

void _gotoLocation(double lat,double long) {
//check if map is ready to move camera...
if(this.isMapRead) {
_mapController.move(LatLng(lat, long), _mapController?.zoom);
}
}

Widget _buildMap(){

return GetBuilder<MapEventsController>( // GetX state control
init: _mapEventsController, // GetX events controller
builder: (value) { // GetX event fire when update state in controller

updatePinOnMap(value.liveUpdate, value.location);

if(value.liveUpdate){
_gotoLocation(value.location.lat, value.location.lng);
}

return FlutterMap(
mapController: _mapController,
options: MapOptions(
center: LatLng(value?.location?.lat, value?.location?.lng),
zoom: 13.0,
plugins: [
],
onTap: _handleTap,
),
layers: [
TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c']
),
MarkerLayerOptions(markers: markers), //markers
],
);
},
);

}

void updatePinOnMap(bool liveUpdate, Location location) async {

if(location == null) {
return;
}


this.markers.clear();

Marker mark = Marker(
point: LatLng(location.lat, location.lng),
anchorPos: anchorPos,
builder: (ctx) =>
GestureDetector(
onTap: () => _configureModalBottomSheet(ctx),
child: Container(
child: Icon(Icons.my_location, size: 28, color: Colors.red,), // FlutterLogo(),
),
),
);

markers.add(mark);

}

void _handleTap(LatLng latlng) {

}

void _configureModalBottomSheet(context) {

}

}

关于flutter - flutter_map通过BLoC使用 map Controller 移动相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61155942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com