Search code examples
androidflutterdartnavigationbottomnavigationview

How do I "activate" a custom bottom bar in Flutter?


Flutter is not natural for me yet and was hoping you guys can help me. I'm creating a map application with a bottomNavigationBar and I'm using a package called circular_bottom_navigation 1.0.1 to do this, which you can find at the link below.

https://pub.dev/packages/circular_bottom_navigation#-readme-tab-

I have managed to display the bar, but I have a couple of issues that have been frustrating me.

I have added the following line in the Callback

CircularBottomNavigationController _navigationController = 
new CircularBottomNavigationController(selectedPos);

but I unsure how to use the next

// Write a new value 
_navigationController.value = 0;

// Read the latest value
int latest = _navigationController.value;

As you will know the bar is not active yet.

Also, if you take a look at the screenshot you will notice the the map stops at the top of the icon and is not visible to the bar. I would like the map to reach the top of the bar, so the icons are overlaid.

Screenshot of map with Bottom Bar


Solution

  • import 'dart:async';
    
    import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
    import 'package:circular_bottom_navigation/tab_item.dart';
    import 'package:flutter/material.dart';
    import 'package:google_maps_flutter/google_maps_flutter.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Google Maps Demo',
          home: MapSample(),
        );
      }
    }
    
    class MapSample extends StatefulWidget {
      @override
      State<MapSample> createState() => MapSampleState();
    }
    
    class MapSampleState extends State<MapSample> {
      Completer<GoogleMapController> _controller = Completer();
    
      static final CameraPosition _kGooglePlex = CameraPosition(
        target: LatLng(37.42796133580664, -122.085749655962),
        zoom: 14.4746,
      );
    
      List<TabItem> tabItems = List.of([
        new TabItem(Icons.home, "Home", Colors.blue, labelStyle: TextStyle(fontWeight: FontWeight.normal)),
        new TabItem(Icons.search, "Search", Colors.orange, labelStyle: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
        new TabItem(Icons.layers, "Reports", Colors.red),
        new TabItem(Icons.notifications, "Notifications", Colors.cyan),
      ]);
    
      static final CameraPosition _kLake = CameraPosition(
          bearing: 192.8334901395799,
          target: LatLng(37.43296265331129, -122.08832357078792),
          tilt: 59.440717697143555,
          zoom: 19.151926040649414);
    
      CircularBottomNavigationController _navigationController = new CircularBottomNavigationController(0);
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: GoogleMap(
            mapType: MapType.hybrid,
            initialCameraPosition: _kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
          ),
          floatingActionButton: FloatingActionButton.extended(
            onPressed: _goToTheLake,
            label: Text('To the lake!'),
            icon: Icon(Icons.directions_boat),
          ),
          extendBody: true,
          bottomNavigationBar: CircularBottomNavigation(
            tabItems,
            controller: _navigationController,
            selectedCallback: (int selected) {
              _navigationController.value = selected;
            },
          ),
        );
      }
    
      Future<void> _goToTheLake() async {
        final GoogleMapController controller = await _controller.future;
        controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
      }
    }
    

    you must pass extendBody: true to Scaffold

    enter image description here