我试图在flutter_map中打开一个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条答案
按热度按时间uz75evzq1#
发生这种情况是因为Material模态子对象实际上是
MaterialApp
的子对象,正如您可以从下面的DevTools检查器屏幕截图中看到的那样。您需要:
context
参数在showModalBottomSheet
中的作用,但它显然不是正确的事情 *