Files

237 lines
6.5 KiB
Dart
Raw Permalink Normal View History

2026-11-07 14:39:59 +01:00
import 'package:flutter/material.dart';
import 'package:cesiumjs/cesiumjs.dart';
/// Optional Cesium ion token (https://ion.cesium.com/tokens), injected at
/// build time so it never lands in source control:
2026-11-07 15:48:01 +01:00
/// flutter run --dart-define-from-file=cesium_ion.json
2026-11-07 14:39:59 +01:00
/// (see cesium_ion.example.json). Without it the demo uses OpenStreetMap
/// imagery and an ellipsoid globe.
const String ionToken = String.fromEnvironment('CESIUM_ION_TOKEN');
void main() => runApp(const DemoApp());
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CesiumJS demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark,
),
),
home: const DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _Place {
/// Marker at [lon]/[lat]. The camera is placed at [camLon]/[camLat]/[height]
/// (defaults to straight above the marker) looking with [heading]/[pitch].
const _Place(
this.name,
this.lon,
this.lat,
this.height, {
double? camLon,
double? camLat,
this.heading = 0,
this.pitch = -90,
}) : camLon = camLon ?? lon,
camLat = camLat ?? lat;
final String name;
final double lon, lat, height, camLon, camLat, heading, pitch;
}
const _places = [
_Place('Toulouse', 1.4442, 43.6047, 15000),
_Place('Capitole (zoom ortho)', 1.4433, 43.6045, 900),
_Place('Paris', 2.3522, 48.8566, 20000),
// Oblique views to show the terrain model (MNT).
_Place(
'Mont Blanc (3D)',
6.8652,
45.8326,
7500,
camLon: 6.80,
camLat: 45.73,
heading: 25,
pitch: -18,
),
_Place(
'Pic du Midi (3D)',
0.1424,
42.9369,
5500,
camLon: 0.10,
camLat: 42.86,
heading: 20,
pitch: -18,
),
_Place('New York', -74.0060, 40.7128, 25000),
_Place('Tokyo', 139.6917, 35.6895, 30000),
];
class _DemoPageState extends State<DemoPage> {
final _controller = CesiumController();
String _status = 'Loading CesiumJS…';
String _camera = '';
CesiumImagery? _imagery;
bool _terrain = false;
bool _fps = false;
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _onMessage(CesiumMessage msg) {
switch (msg.type) {
case 'ready':
setState(
() => _status =
'Ready — Cesium ${msg.data['cesium']}, WebGL ${msg.data['webgl']}',
);
case 'layers':
setState(() {
_imagery = CesiumImagery.values
.where((i) => i.jsName == msg.data['imagery'])
.firstOrNull;
_terrain = msg.data['terrain'] == true;
});
case 'camera':
final d = msg.data;
setState(
() => _camera =
'lon ${(d['longitude'] as num).toStringAsFixed(4)} '
'lat ${(d['latitude'] as num).toStringAsFixed(4)} '
'h ${((d['height'] as num) / 1000).toStringAsFixed(1)} km',
);
}
}
Future<void> _goTo(_Place p) async {
await _controller.flyTo(
p.camLon,
p.camLat,
p.height,
headingDegrees: p.heading,
pitchDegrees: p.pitch,
);
await _controller.addPoint(p.lon, p.lat, label: p.name);
}
@override
Widget build(BuildContext context) {
final ready = _controller.isReady;
const hasToken = ionToken != '';
2026-11-07 15:48:01 +01:00
// Phones: the side panel becomes a drawer.
final narrow = MediaQuery.sizeOf(context).width < 700;
final panel = ListView(
children: [
for (final p in _places)
ListTile(
dense: true,
leading: const Icon(Icons.place_outlined),
title: Text(p.name),
enabled: ready,
onTap: () {
if (narrow) Navigator.of(context).pop(); // close the drawer
_goTo(p);
},
),
const Divider(),
const ListTile(dense: true, title: Text('Orthophoto / fond')),
RadioGroup<CesiumImagery>(
groupValue: _imagery,
onChanged: (v) {
if (ready && v != null) _controller.setImagery(v);
},
child: Column(
children: [
for (final i in CesiumImagery.values)
RadioListTile<CesiumImagery>(
dense: true,
value: i,
enabled: ready && (i != CesiumImagery.ionAerial || hasToken),
title: Text(i.label),
),
],
),
),
SwitchListTile(
dense: true,
title: const Text('MNT (Cesium World Terrain)'),
value: _terrain,
onChanged: ready && hasToken ? _controller.setTerrain : null,
),
],
);
final viewer = CesiumViewer(
ionToken: hasToken ? ionToken : null,
controller: _controller,
onMessage: _onMessage,
onError: (e) => setState(() => _status = 'Error: $e'),
);
2026-11-07 14:39:59 +01:00
return Scaffold(
2026-11-07 15:48:01 +01:00
drawer: narrow ? Drawer(child: SafeArea(child: panel)) : null,
2026-11-07 14:39:59 +01:00
appBar: AppBar(
2026-11-07 15:48:01 +01:00
title: const Text('CesiumJS in Flutter'),
2026-11-07 14:39:59 +01:00
actions: [
IconButton(
tooltip: 'Clear markers',
icon: const Icon(Icons.layers_clear),
onPressed: ready ? _controller.clearEntities : null,
),
IconButton(
tooltip: 'Show FPS',
icon: Icon(_fps ? Icons.speed : Icons.speed_outlined),
onPressed: ready
? () {
setState(() => _fps = !_fps);
_controller.showFps(_fps);
}
: null,
),
IconButton(
tooltip: 'Open web inspector',
icon: const Icon(Icons.bug_report_outlined),
onPressed: _controller.openInspector,
),
],
),
2026-11-07 15:48:01 +01:00
body: narrow
? viewer
: Row(
2026-11-07 14:39:59 +01:00
children: [
2026-11-07 15:48:01 +01:00
SizedBox(width: 230, child: panel),
Expanded(child: viewer),
2026-11-07 14:39:59 +01:00
],
),
bottomNavigationBar: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
color: Theme.of(context).colorScheme.surfaceContainer,
2026-11-07 15:48:01 +01:00
child: SafeArea(
top: false,
child: Text(
'$_status $_camera',
style: Theme.of(context).textTheme.bodySmall,
),
2026-11-07 14:39:59 +01:00
),
),
);
}
}