Created
June 21, 2018 06:29
-
-
Save harsh159357/62f338a196b0e5edad7aed5017dae3e5 to your computer and use it in GitHub Desktop.
How to programmatically select BottomNavigationBar Tab in Flutter instead of built in onTap callback?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:contacts/utils/constants.dart'; | |
import 'package:contacts/customviews/progress_dialog.dart'; | |
import 'package:contacts/models/contact.dart'; | |
import 'package:contacts/ways/api/futures/api_futures.dart'; | |
import 'package:contacts/models/base/event_object.dart'; | |
import 'package:contacts/models/contact.dart'; | |
import 'package:contacts/ways/api/pages/contact_page.dart'; | |
class APIHomePage extends StatefulWidget { | |
@override | |
createState() => new APIHomePageState(); | |
} | |
class APIHomePageState extends State<APIHomePage> { | |
static final globalKey = new GlobalKey<ScaffoldState>(); | |
ProgressDialog progressDialog = | |
ProgressDialog.getProgressDialog(ProgressDialogTitles.LOADING_CONTACTS); | |
Widget apiHomeWidget = new Container(); | |
static const String TAPPED_ON_HEADER = "Tapped On Header"; | |
String title = DrawerTitles.CONTACTS; | |
int navigationIndex = 0; | |
@override | |
Future<void> didChangeDependencies() async { | |
super.didChangeDependencies(); | |
await initContacts(); | |
} | |
Future<void> initContacts() async { | |
EventObject eventObject = await getContacts(); | |
setState(() { | |
progressDialog.hideProgress(); | |
switch (eventObject.id) { | |
case EventConstants.READ_CONTACT_SUCCESSFUL: | |
apiHomeWidget = new ContactPage(contactList: eventObject.object); | |
break; | |
case EventConstants.READ_CONTACT_UN_SUCCESSFUL: | |
apiHomeWidget = new ContactPage(); | |
break; | |
case EventConstants.NO_CONTACT_FOUND: | |
apiHomeWidget = new ContactPage(); | |
break; | |
case EventConstants.NO_INTERNET_CONNECTION: | |
apiHomeWidget = new ContactPage(); | |
break; | |
} | |
}); | |
} | |
//------------------------------------------------------------------------------ | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
key: globalKey, | |
appBar: new AppBar( | |
centerTitle: true, | |
textTheme: new TextTheme( | |
title: new TextStyle( | |
color: Colors.white, | |
fontSize: 22.0, | |
)), | |
iconTheme: new IconThemeData(color: Colors.white), | |
title: new Text(title), | |
), | |
body: _apiHomePage(), | |
floatingActionButton: _floatingActionButton(), | |
drawer: _navigationDrawer(), | |
bottomNavigationBar: _bottomNavigationBar(), | |
); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _apiHomePage() { | |
return new Stack( | |
children: <Widget>[apiHomeWidget, progressDialog], | |
); | |
} | |
//------------------------------------------------------------------------------ | |
FloatingActionButton _floatingActionButton() { | |
return new FloatingActionButton( | |
onPressed: null, | |
child: new Icon( | |
Icons.add, | |
), | |
); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _navigationDrawer() { | |
return new Drawer(child: _navigationData()); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _bottomNavigationBar() { | |
return new BottomNavigationBar( | |
items: <BottomNavigationBarItem>[ | |
_bottomNavigationItem(Icons.account_circle, DrawerTitles.CONTACTS), | |
_bottomNavigationItem(Icons.delete, DrawerTitles.DELETED_CONTACTS), | |
_bottomNavigationItem(Icons.list, DrawerTitles.LOGS), | |
], | |
onTap: (int index) { | |
setState(() { | |
navigationIndex = index; | |
switch (navigationIndex) { | |
case 0: | |
handleBottomNavigationBarClicks(DrawerTitles.CONTACTS); | |
break; | |
case 1: | |
handleBottomNavigationBarClicks(DrawerTitles.DELETED_CONTACTS); | |
break; | |
case 2: | |
handleBottomNavigationBarClicks(DrawerTitles.LOGS); | |
break; | |
} | |
}); | |
}, | |
currentIndex: navigationIndex, | |
fixedColor: Colors.blue[400], | |
type: BottomNavigationBarType.fixed, | |
); | |
} | |
//------------------------------------------------------------------------------ | |
BottomNavigationBarItem _bottomNavigationItem(IconData icon, String title) { | |
return new BottomNavigationBarItem( | |
icon: new Icon( | |
icon, | |
), | |
title: new Text( | |
title, | |
)); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _navigationData() { | |
navigationData = <NavigationItem>[ | |
new HeaderItem(_drawerHeader()), | |
new SimpleItem( | |
leadingIconData: Icons.account_circle, title: DrawerTitles.CONTACTS), | |
new SimpleItem( | |
leadingIconData: Icons.delete, title: DrawerTitles.DELETED_CONTACTS), | |
new SimpleItem(leadingIconData: Icons.list, title: DrawerTitles.LOGS), | |
]; | |
return new ListView.builder( | |
itemBuilder: (BuildContext context, int index) { | |
final item = navigationData[index]; | |
if (item is HeaderItem) { | |
return item.gestureDetector; | |
} else if (item is SimpleItem) { | |
return _simpleItem(item); | |
} | |
}, | |
itemCount: navigationData.length); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _drawerHeader() { | |
return new GestureDetector( | |
onTap: () { | |
handleNavigationDrawerClicks(TAPPED_ON_HEADER); | |
}, | |
child: new DrawerHeader( | |
child: new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
new Column( | |
children: <Widget>[ | |
new Icon( | |
Icons.description, | |
size: 75.0, | |
color: Colors.white, | |
), | |
new Container( | |
child: new Text( | |
Texts.API, | |
style: new TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.normal, | |
fontSize: 26.0), | |
), | |
margin: EdgeInsets.only(top: 5.0, bottom: 10.0), | |
), | |
], | |
) | |
], | |
), | |
/*new Text('Drawer Header')*/ | |
decoration: new BoxDecoration(color: Colors.blue[400]), | |
), | |
); | |
} | |
//------------------------------------------------------------------------------ | |
Widget _simpleItem(SimpleItem simpleItem) { | |
return new ListTile( | |
onTap: () { | |
handleNavigationDrawerClicks(simpleItem.title); | |
}, | |
leading: new Icon( | |
simpleItem.leadingIconData, | |
size: 25.0, | |
color: Colors.blueGrey[400], | |
), | |
title: new Text( | |
simpleItem.title, | |
style: new TextStyle( | |
color: Colors.blueGrey[400], | |
fontWeight: FontWeight.normal, | |
fontSize: 18.0), | |
)); | |
} | |
//------------------------------------------------------------------------------ | |
List<NavigationItem> navigationData; | |
//------------------------------------------------------------------------------ | |
void handleNavigationDrawerClicks(String whatToDo) { | |
Navigator.pop(context); | |
setState(() { | |
if (whatToDo != TAPPED_ON_HEADER) { | |
title = whatToDo; | |
} else { | |
showSnackBar(SnackBarText.TAPPED_ON_API_HEADER); | |
} | |
}); | |
switch (whatToDo) { | |
case DrawerTitles.CONTACTS: | |
setState(() { | |
progressDialog | |
.showProgressWithText(ProgressDialogTitles.LOADING_CONTACTS); | |
loadContacts(); | |
}); | |
break; | |
case DrawerTitles.DELETED_CONTACTS: | |
break; | |
case DrawerTitles.LOGS: | |
break; | |
} | |
} | |
void handleBottomNavigationBarClicks(String whatToDo) { | |
setState(() { | |
if (whatToDo != TAPPED_ON_HEADER) { | |
title = whatToDo; | |
switch (whatToDo) { | |
case DrawerTitles.CONTACTS: | |
navigationIndex = 0; | |
progressDialog | |
.showProgressWithText(ProgressDialogTitles.LOADING_CONTACTS); | |
loadContacts(); | |
break; | |
case DrawerTitles.DELETED_CONTACTS: | |
navigationIndex = 1; | |
break; | |
case DrawerTitles.LOGS: | |
navigationIndex = 2; | |
break; | |
} | |
} else { | |
showSnackBar(SnackBarText.TAPPED_ON_API_HEADER); | |
} | |
}); | |
} | |
//------------------------------------------------------------------------------ | |
void showSnackBar(String textToBeShown) { | |
globalKey.currentState.showSnackBar(new SnackBar( | |
content: new Text(textToBeShown), | |
)); | |
} | |
//------------------------------------------------------------------------------ | |
void loadContacts() async { | |
EventObject eventObject = await getContacts(); | |
if (this.mounted) { | |
setState(() { | |
progressDialog.hideProgress(); | |
switch (eventObject.id) { | |
case EventConstants.READ_CONTACT_SUCCESSFUL: | |
apiHomeWidget = new ContactPage(contactList: eventObject.object); | |
break; | |
case EventConstants.READ_CONTACT_UN_SUCCESSFUL: | |
apiHomeWidget = new ContactPage(); | |
break; | |
case EventConstants.NO_CONTACT_FOUND: | |
apiHomeWidget = new ContactPage(); | |
break; | |
case EventConstants.NO_INTERNET_CONNECTION: | |
apiHomeWidget = new ContactPage(); | |
break; | |
} | |
}); | |
} | |
} | |
//------------------------------------------------------------------------------ | |
} | |
//------------------------------------------------------------------------------ | |
abstract class NavigationItem {} | |
//------------------------------------------------------------------------------ | |
class HeaderItem implements NavigationItem { | |
GestureDetector gestureDetector; | |
HeaderItem(this.gestureDetector); | |
} | |
//------------------------------------------------------------------------------ | |
class SimpleItem implements NavigationItem { | |
SimpleItem({this.title, this.leadingIconData, this.trailingIconData}); | |
final String title; | |
final IconData leadingIconData, trailingIconData; | |
} | |
//------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment