cruiser 1.0.0 cruiser: ^1.0.0 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/services.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';
final router = Cruiser();
final random = Random();
void main()
{
router.addRoute(CruiserRoute(name: routeName, builder: (context, args, params) => ExamplePage()));
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
@override
Widget build(BuildContext context)
{
return MaterialApp(
title: 'Cruiser Example',
initialRoute: routeName,
navigatorKey: router.navigatorKey,
onGenerateRoute: router.generator(),
builder: (BuildContext context, Widget child)
{
return Scaffold(
resizeToAvoidBottomInset: true,
body: DefaultTextStyle(
style: TextStyle(fontSize: 16, color: Colors.white),
child: SafeArea(child: child)
)
);
},
);
}
}
class ExamplePage extends StatefulWidget
{
@override
ExamplePageState createState() => new ExamplePageState();
}
class ExamplePageState extends State<ExamplePage>
{
@override
Widget build(BuildContext context)
{
var args = Cruiser.args<ExampleArgs>(context);
var currentColor = args?.color ?? colors.first;
_setUIOverlayColor(currentColor);
var nextTransition = _getNextTransition(args?.nextTransition);
var nextColor = _getNextColor(args?.color);
return Container(
color: args?.color ?? colors.first,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ExampleButton(
text: nextTransition.name,
color: ColorConverter.darken(currentColor, 0.1),
onPressed: () async
{
await router.navigate(
routeName,
transitions: [nextTransition],
navigationType: NavigationType.push,
args: new ExampleArgs(
nextTransition: nextTransition,
color: nextColor
));
})
]
)
),
);
}
CruiserTransition _getNextTransition(CruiserTransition previousTransition)
{
if(previousTransition == null) return CruiserTransition.values[0];
var index = CruiserTransition.values.indexOf(previousTransition) + 1;
return CruiserTransition.values[index < CruiserTransition.values.length ? index : 0];
}
Color _getNextColor(Color previousColor)
{
var available = colors.where((x) => previousColor == null || x != previousColor).toList();
return available[random.nextInt(available.length)];
}
void _setUIOverlayColor(Color color)
{
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: color,
statusBarIconBrightness: Brightness.light,
statusBarBrightness: Brightness.dark,
systemNavigationBarColor: color,
systemNavigationBarIconBrightness: Brightness.light
));
}
}
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: 16))
)
);
}
}