提问人:Goofyrocks 提问时间:10/16/2023 更新时间:10/17/2023 访问量:53
Flutter Map,控制器在模态中丢失
Flutter Map, Controller lost in modal
问:
我正在尝试在flutter_map中打开一个模态以创建重新定位快捷方式。 我尝试将flutter_map上下文发送到模态中,但是当我进入时。如果我点击它,它会告诉我,它找不到颤振映射上下文。
FlutterMap(
options: const MapOptions(
initialCenter: LatLng(54.939196, -3.929788),
initialZoom: 14,
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'com.example.app',
),
const NavigationButton(),
],
当我使用 showModal 时,我认为我正确地给出了我的 FlutterMapContext。
class NavigationButton extends StatelessWidget {
const NavigationButton({super.key});
@override
Widget build(BuildContext context) {
var mapController = MapController.of(context);
var mapCamera = MapCamera.of(context);
return Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: const EdgeInsets.only(left: 2.0, bottom: 2.0, right: 2.0),
child: FloatingActionButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) => NavigationList());
},
child: const Icon(Icons.list),
),
));
}
}
class NavigationList extends StatelessWidget {
const NavigationList(
{Key? key})
: super(key: key);
@override
Widget build(BuildContext context) {
return ListView(
children: [
Shortcut(
title: "Entrance Hall",
lat: 54.949196,
lng: -3.929788),
],
);
}
}
class Shortcut extends StatelessWidget {
final String title;
final double lat;
final double lng;
const Shortcut(
{super.key,
required this.title,
required this.lat,
required this.lng});
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),
onTap: () => MapController.of(context).move(LatLng(lat, lng),MapCamera.of(context)),
);
}
}
谢谢
Bad state: `MapController.of()` should not be called outside a `FlutterMap` and its children
答:
1赞
JaffaKetchup
10/17/2023
#1
发生这种情况的原因是 材质模态子项实际上是 的子项,如下面的 DevTools 检查器屏幕截图所示。MaterialApp
您需要:
- 通过适当的上下文,确保不要混淆它们
我不完全确定上下文
参数在showModalBottomSheet
中的作用,但这显然不是正确的事情 - 使用外部地图控制器,如这些文档所示,这可能更可取
评论