fragment_navigate 0.1.2 fragment_navigate: ^0.1.2 copied to clipboard
A controller to make the effect of swapping fragments like in native Android.
import 'package:fragment_navigate/navigate-bloc.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Main(),
);
}
}
final String a = 'a';
final String b = 'b';
final String c = 'c';
final String d = 'd';
class Main extends StatelessWidget {
static final FragNavigate _fragNav = FragNavigate(
firstKey: a,
drawerContext: null,
screens: <Posit>[
Posit(
key: a,
title: 'Title A',
fragment: Container(color: Colors.amberAccent,)
),
Posit(
key: b,
title: 'Title B',
fragment: SecondScreen()
),
Posit(
key: c,
title: 'Title C',
fragment: Container(color: Colors.blueAccent,)
),
Posit(
key: d,
title: 'Title D',
fragment: Text('qqqq')
),
],
actionsList: [
ActionPosit(
keys: [a, b, c],
actions: [
IconButton(
icon: Icon(Icons.add),
onPressed: (){
_fragNav.action('teste');
}
)
]
)
],
bottomList: [
BottomPosit(
keys: [a, b, c],
length: 2,
child: TabBar(
indicatorColor: Colors.white,
tabs: <Widget>[
Text('a'),
Text('b')
],
)
)
]
);
@override
Widget build(BuildContext context) {
_fragNav.setDrawerContext = context;
return StreamBuilder<FullPosit>(
stream: _fragNav.outStreamFragment,
builder: (con, s){
if(s.data != null){
return DefaultTabController(
length: s.data.bottom.length,
child: Scaffold(
key: _fragNav.drawerKey,
appBar: AppBar(
title: Text(s.data.title),
actions: s.data.actions,
bottom: s.data.bottom.child,
),
drawer: CustomDrawer(fragNav: _fragNav),
body: ScreenNavigate(
child: s.data.fragment,
bloc: _fragNav
),
)
);
}
return Container();
}
);
}
}
class SecondScreen extends StatelessWidget implements ActionInterface{
@override
Widget build(BuildContext context) {
return Container(color: Colors.purple,);
}
@override
void action(String tag, {Object params}) {
print('called on secondScreen with tag: $tag');
}
}
class CustomDrawer extends StatelessWidget {
final FragNavigate fragNav;
const CustomDrawer({@required this.fragNav});
@override
Widget build(BuildContext context) {
Widget _getItem({@required String currentSelect, @required text, @required key}){
Color _getColor() => currentSelect == key ? Colors.white : Colors.black87;
return Material(
color: currentSelect == key ? Colors.blueAccent : Colors.transparent,
child: ListTile(
leading: Icon(Icons.settings, color: currentSelect == key ? Colors.white : null),
selected: currentSelect == key,
title: Text(text, style: TextStyle(color: _getColor())),
onTap: () => fragNav.putPosit(key: key),
)
);
}
return Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.blueAccent,
),
),
_getItem(
currentSelect: fragNav.currentKey,
text: 'Title A',
key: a
),
_getItem(
currentSelect: fragNav.currentKey,
text: 'Title B',
key: b
),
_getItem(
currentSelect: fragNav.currentKey,
text: 'Title C',
key: c
),
_getItem(
currentSelect: fragNav.currentKey,
text: 'Title D',
key: d
),
],
),
);
}
}