cruiser 1.2.3 cruiser: ^1.2.3 copied to clipboard
Cruiser is a fully featured router library for Flutter applications.
import 'dart:math';
import 'package:cruiser/cruiser.dart';
import 'package:cruiser_example/example_args.dart';
import 'package:cruiser_example/utils/color_converter.dart';
import 'package:cruiser_example/utils/cruiser_transition_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
const colors = [Colors.blue, Colors.green, Colors.purple, Colors.orange, Colors.pink, Colors.amber, Colors.cyan];
const routeName = '/example';
//const routeNameSecond = '/second-example';
final router = Cruiser();
final random = Random();
//bool isSecondRoute = false;
void main()
{
router.addRoute(CruiserRoute(name: routeName, builder: (context, args) => ExamplePage(args: args)));
// router.addRoute(CruiserRoute(name: routeNameSecond, builder: (context, args) => SecondExamplePage(args: args)));
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
@override
Widget build(BuildContext context)
{
return MaterialApp(
title: 'Cruiser Example',
initialRoute: routeName,
navigatorKey: router.navigatorKey,
onGenerateRoute: router.generator,
navigatorObservers: [router.observer],
//home: ExamplePage(args: null),
/*builder: (BuildContext context, Widget child)
{
return WillPopScope(
child: Scaffold(
resizeToAvoidBottomInset: true,
body: DefaultTextStyle(
style: TextStyle(fontSize: 16, color: Colors.white),
child: child
)
),
onWillPop: () async
{
return Future.value(false);
}
);
}*/
);
}
}
class SecondExamplePage extends ExamplePage
{
final ExampleArgs args;
SecondExamplePage({@required this.args}): super(args: args);
@override
ExamplePageState createState() => new ExamplePageState();
}
class ExamplePage extends StatefulWidget
{
final ExampleArgs args;
ExamplePage({@required this.args});
@override
ExamplePageState createState() => new ExamplePageState();
}
class ExamplePageState extends State<ExamplePage>
{
Color nextColor;
TransitionType nextTransition;
@override
void initState()
{
super.initState();
nextColor = _getNextColor(widget.args?.color);
nextTransition = _getNextTransition(widget.args?.transition);
}
@override
Widget build(BuildContext context)
{
return WillPopScope(
child: Scaffold(
resizeToAvoidBottomInset: true,
body: DefaultTextStyle(
style: TextStyle(fontSize: 16, color: Colors.white),
child: Container(
color: widget.args?.color ?? Colors.white,
child: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ExampleButton(
text: '${nextTransition.name}', // '${isSecondRoute ? 'Second': 'First'}
color: ColorConverter.darken(widget.args?.color ?? Colors.blue, 0.1),
onPressed: () async => await _toNextRoute(context)
)
]
)
)
),
)
)
),
onWillPop: () async
{
router.pop();
return Future.value(false);
}
);
}
Future _toNextRoute(BuildContext context) async
{
/* isSecondRoute = !isSecondRoute;
if(isSecondRoute)
{
await router.navigate(
context,
routeNameSecond,
transition: TransitionType.secondZoomIn,
args: ExampleArgs(color: nextColor, transition: nextTransition));
}
else
{
await router.navigate(
context,
routeName,
transition: TransitionType.zoomIn,
args: ExampleArgs(color: nextColor, transition: nextTransition));
}
return;
final route = isSecondRoute ? routeNameSecond : routeName;
isSecondRoute = !isSecondRoute;
*/
await router.navigate(
context,
routeName,
transition: nextTransition,
args: ExampleArgs(color: nextColor, transition: nextTransition)
);
}
TransitionType _getNextTransition(TransitionType previousTransition)
{
if(previousTransition == null) return TransitionType.values[0];
var index = TransitionType.values.indexOf(previousTransition) + 1;
return TransitionType.values[index < TransitionType.values.length ? index : 0];
}
Color _getNextColor(Color previousColor)
{
var available = previousColor != null ? colors.where((x) => x != previousColor).toList() : colors;
return available[random.nextInt(available.length)];
}
}
class ExampleButton extends StatelessWidget
{
final VoidCallback onPressed;
final String text;
final Color color;
ExampleButton({this.text, this.onPressed, this.color});
@override
Widget build(BuildContext context)
{
return ButtonTheme(
height: 47,
padding: EdgeInsets.symmetric(horizontal: 25),
child: FlatButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0),
),
color: color,
textColor: Colors.white,
onPressed: onPressed,
child: Text(text, style: TextStyle(fontSize: 14))
)
);
}
}