rafalbednarczuk / curved_navigation_bar

Animated Curved Navigation Bar in Flutter
BSD 2-Clause "Simplified" License
694 stars 242 forks source link

_bottomNavigationKey.currentState and setPage functions are not working in Null safety project #102

Closed kamleshwebtech closed 3 years ago

kamleshwebtech commented 3 years ago

I am using null safety version of this package: https://pub.dev/packages/curved_navigation_bar/versions/0.4.0-nullsafety.0/example

I have used below example to check this package before adding it to my project, which code is as:

import 'package:flutter/material.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';

void main() => runApp(MaterialApp(home: BottomNavBar()));

class BottomNavBar extends StatefulWidget {
  @override
  _BottomNavBarState createState() => _BottomNavBarState();
}

class _BottomNavBarState extends State<BottomNavBar> {
  int _page = 0;
  GlobalKey _bottomNavigationKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        bottomNavigationBar: CurvedNavigationBar(
          key: _bottomNavigationKey,
          index: 0,
          height: 50.0,
          items: <Widget>[
            Icon(Icons.add, size: 30),
            Icon(Icons.list, size: 30),
            Icon(Icons.compare_arrows, size: 30),
            Icon(Icons.call_split, size: 30),
            Icon(Icons.perm_identity, size: 30),
          ],
          color: Colors.white,
          buttonBackgroundColor: Colors.white,
          backgroundColor: Colors.blueAccent,
          animationCurve: Curves.easeInOut,
          animationDuration: Duration(milliseconds: 600),
          onTap: (index) {
            setState(() {
              _page = index;
            });
          },
        ),
        body: Container(
          color: Colors.blueAccent,
          child: Center(
            child: Column(
              children: <Widget>[
                Text(_page.toString(), textScaleFactor: 10.0),
                RaisedButton(
                  child: Text('Go To Page of index 1'),
                  onPressed: () {
                    final CurvedNavigationBarState navBarState =
                        _bottomNavigationKey.currentState;
                    navBarState.setPage(1);
                  },
                )
              ],
            ),
          ),
        ));
  }
}

onPressed function has below code:

final CurvedNavigationBarState navBarState = _bottomNavigationKey.currentState;
navBarState.setPage(1);

I am getting below error in this code part:

A value of type 'State<StatefulWidget>?' can't be assigned to a variable of type 'CurvedNavigationBarState'.
Try changing the type of the variable, or casting the right-hand type to 'CurvedNavigationBarState'.

Kindly suggest what is the issue and how can I solve it. Thanks a lot.

rafalbednarczuk commented 3 years ago

should be working now in 1.0.0