MapView Style
introduction
This Example Shows:
- Display a MapView Widget
- Update mapview style by setting the styleString in NBMap Widget
![]() | ![]() |
Android snapshot | iOS snapshot |
For all code examples, refer to Flutter Navigation Code Example
MapViewStyle view source
1import 'package:flutter/material.dart';
2import 'package:nb_maps_flutter/nb_maps_flutter.dart';
3
4class MapViewStyle extends StatefulWidget {
5 static const String title = "MapView Style";
6
7
8 MapViewStyleState createState() => MapViewStyleState();
9}
10
11class MapViewStyleState extends State<MapViewStyle> {
12 NextbillionMapController? controller;
13 var isLight = true;
14
15 void _onMapCreated(NextbillionMapController controller) {
16 this.controller = controller;
17 }
18
19 void _onStyleLoaded() {
20 }
21
22
23 void initState() {
24 super.initState();
25 }
26
27
28 Widget build(BuildContext context) {
29 return Scaffold(
30 appBar: AppBar(
31 title: const Text(MapViewStyle.title),
32 ),
33 floatingActionButton: Padding(
34 padding: const EdgeInsets.all(32.0),
35 child: FloatingActionButton(
36 child: const Icon(Icons.swap_horiz),
37 onPressed: () => setState(
38 () => isLight = !isLight,
39 ),
40 ),
41 ),
42 body: Stack(
43 children: [
44 NBMap(
45 onMapCreated: _onMapCreated,
46 initialCameraPosition: const CameraPosition(
47 target: LatLng(1.312533169133601, 103.76986708439264),
48 zoom: 13.0,
49 ),
50 onStyleLoadedCallback: _onStyleLoaded,
51 styleString: isLight ? NbMapStyles.LIGHT : NbMapStyles.DARK,
52 ),
53 ],
54 ),
55 );
56 }
57
58
59 void dispose() {
60 super.dispose();
61 }
62
63}
Code summary
The above code snippet demonstrates how to display a map view widget using nb_maps_flutter and how to update the map view style dynamically between light and dark modes.
Display a MapView Widget:
-
The MapViewStyle widget is the main entry point of the application.
-
The _onMapCreated function is called when the map is created, and it sets the map controller to be used for controlling the map.
-
The NBMap widget from the nb_maps_flutter package is used to display the map. It is positioned in a Stack widget along with the "FloatingActionButton" widget.
Update MapView Style:
-
The isLight variable is used to determine whether the map view style should be light or dark. It is initially set to true, indicating the light mode.
-
A "FloatingActionButton" is provided at the bottom right corner of the screen, allowing the user to toggle between light and dark styles by pressing the "swap_horiz" icon.
-
When the FloatingActionButton is pressed, the setState function is called to update the isLight variable, toggling it between true and false.
-
The styleString property of the NBMap widget is used to set the map view style. If isLight is true, the light style is applied (NbMapStyles.LIGHT), otherwise, the dark style is applied (NbMapStyles.DARK).
When the app is launched, it displays a map view with the initial light mode. When the "FloatingActionButton" is pressed, the map view style toggles between light and dark modes dynamically. Please ensure that you have the necessary permissions and API keys set up to use the mapping services properly.