Skip to content

Instantly share code, notes, and snippets.

@aqibgatoo
Last active January 19, 2019 03:40
Show Gist options
  • Save aqibgatoo/a3aa567d194718675c7bda55832a76c4 to your computer and use it in GitHub Desktop.
Save aqibgatoo/a3aa567d194718675c7bda55832a76c4 to your computer and use it in GitHub Desktop.
// main.dart
void main() {
runApp(
BlocProvider<HomeBloc>(
baloc: HomeBloc(),
child: MyApp(),
),
);
}
//app.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("my app buidl metod called");
return MaterialApp(
title: 'Jk GAD',
theme: ThemeData(brightness: Brightness.dark, fontFamily: "RobotoMono"),
home: HomePage(),
routes: routes,
);
}
Map<String, WidgetBuilder> get routes => <String, WidgetBuilder>{
'orderDetials': (BuildContext context) => OrderDetails()
};
}
//home bloc
class HomeBloc extends BlocBase {
final _navigationSubject = BehaviorSubject<int>(seedValue: 0);
Sink<int> get navigationIndex => _navigationSubject.sink;
Observable<int> get currentNavigationIndex => _navigationSubject.stream;
HomeBloc() {
// _navigationSubject.listen((index) {
// _navigationSubject.sink.add(index);
// });
}
@override
void dispose() {
_navigationSubject.close();
}
}
//homepage.dart
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var ordersBloc;
var noticesBloc;
@override
void initState() {
super.initState();
print('called initstate in homepage');
ordersBloc = OrdersBloc();
noticesBloc = NoticesBloc();
}
@override
Widget build(BuildContext context) {
final bloc = BlocProvider.of<HomeBloc>(context);
print(bloc);
print("home page buidld method called ");
return StreamBuilder<int>(
stream: bloc.currentNavigationIndex,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: Text("...."),
),
drawer: Drawer(),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
title: Text("Orders"), icon: Icon(Icons.view_headline)),
BottomNavigationBarItem(
title: Text("Notice Board"),
icon: Icon(Icons.notifications)),
BottomNavigationBarItem(
title: Text("Calendar"), icon: Icon(Icons.calendar_today)),
],
type: BottomNavigationBarType.fixed,
currentIndex: snapshot.data,
onTap: (index) {
bloc.navigationIndex.add(index);
},
),
body: getPage(snapshot.data),//page changes here
);
} else {
return Text('alskdjfl');
}
},
);
}
@override
void dispose() {
// ordersBloc?.dispose();
// noticesBloc?.dispose();
super.dispose();
}
Widget getPage(int currentIndex) {
if (currentIndex == 0) {
return BlocProvider<OrdersBloc>(bloc: ordersBloc, child: OrdersPage());
} else if (currentIndex == 1) {
return BlocProvider<NoticesBloc>(bloc: noticesBloc, child: HomePage());
} else {
return CalendarPage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment