Skip to content

Instantly share code, notes, and snippets.

@erikwco
Created October 14, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikwco/68b9f398ed3fcdb3dd828147c6d1a0a5 to your computer and use it in GitHub Desktop.
Save erikwco/68b9f398ed3fcdb3dd828147c6d1a0a5 to your computer and use it in GitHub Desktop.
Flutter Communication With Stream
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
//* ***************************************
//* Main
//* ***************************************
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Communication'),
);
}
}
//* ***************************************
//* HomePage Widget
//* ***************************************
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// page index store
int _pageIndex = 0 ;
// page view controller
PageController _pageController;
@override
void initState() {
super.initState();
_pageController = PageController(initialPage: _pageIndex);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: PageView(
onPageChanged: (int page) {
setState(() {
_pageIndex = page;
});
},
controller: _pageController,
scrollDirection: Axis.horizontal,
pageSnapping: true,
children: <Widget>[
Container(color: Colors.red,),
Container(color: Colors.green,),
Container(color: Colors.blue,)
],
)
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _pageIndex,
onTap: (int pageIndex) {
print(pageIndex);
setState(() {
_pageController.jumpToPage(pageIndex);
});
},
items: [
BottomNavigationBarItem(title: Text('General info'), icon: Icon(Icons.info_outline,color: Colors.red)),
BottomNavigationBarItem(title: Text('Services'), icon: Icon(Icons.list,color: Colors.green)),
BottomNavigationBarItem(title: Text('Invoices'), icon: Icon(Icons.payment,color: Colors.blue)),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Save',
child: Icon(Icons.save),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment