I'm new to dart and i tried to implement a BottomNaviagtor. I want to jump to a specific page when the BottomNavigatorItem is tapped.
Below is the error i'm getting:
I/flutter ( 2447): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 2447): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter ( 2447): The method 'jumpToPage' was called on null.
I/flutter ( 2447): Receiver: null
I/flutter ( 2447): Tried calling: jumpToPage(2)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:skypeclone/utils/universal_variables.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
PageController pageController;
int _page = 0;
@override
void initState() {
super.initState();
pageController = PageController();
}
void onPageChanged(int page) {
setState(() {
_page = page;
});
}
void navigationTapped(int page) {
pageController.jumpToPage(page);
}
@override
Widget build(BuildContext context) {
double _labelFontSize = 10;
return Scaffold(
backgroundColor: UniversalVariables.blackColor,
body: PageView(
children: <Widget>[
Center(
child: Text(
"Chat List Screen",
style: TextStyle(color: Colors.white),
)),
Center(
child: Text(
"Call Logs",
style: TextStyle(color: Colors.white),
)),
Center(
child: Text(
"Contact Screen",
style: TextStyle(color: Colors.white),
)),
],
controller: pageController,
onPageChanged: onPageChanged,
),
bottomNavigationBar: Container(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 10),
child: CupertinoTabBar(
backgroundColor: UniversalVariables.blackColor,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.chat,
color: (_page == 0)
? UniversalVariables.lightBlueColor
: UniversalVariables.greyColor),
title: Text(
"Chats",
style: TextStyle(
fontSize: _labelFontSize,
color: (_page == 0)
? UniversalVariables.lightBlueColor
: Colors.grey),
),
),
BottomNavigationBarItem(
icon: Icon(Icons.call,
color: (_page == 1)
? UniversalVariables.lightBlueColor
: UniversalVariables.greyColor),
title: Text(
"Calls",
style: TextStyle(
fontSize: _labelFontSize,
color: (_page == 1)
? UniversalVariables.lightBlueColor
: Colors.grey),
),
),
BottomNavigationBarItem(
icon: Icon(Icons.contact_phone,
color: (_page == 2)
? UniversalVariables.lightBlueColor
: UniversalVariables.greyColor),
title: Text(
"Contacts",
style: TextStyle(
fontSize: _labelFontSize,
color: (_page == 2)
? UniversalVariables.lightBlueColor
: Colors.grey),
),
),
],
onTap: navigationTapped,
currentIndex: _page,
),
),
),
);
}
}
PageController _controller = PageController(
initialPage: 0,
);
Shouldn't the initialPage be declared in the controller ? And also you need to dispose the controller, like
@override
void dispose() {
_controller.dispose();
super.dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With