cruiser 1.1.0 cruiser: ^1.1.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) => ExamplePage(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(),
builder: (BuildContext context, Widget child)
{
return Scaffold(
resizeToAvoidBottomInset: true,
body: DefaultTextStyle(
style: TextStyle(fontSize: 16, color: Colors.white),
child: child
)
);
},
);
}
}
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 Container(
color: widget.args?.color ?? Colors.white,
child: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ExampleButton(
text: nextTransition.name,
color: ColorConverter.darken(widget.args?.color ?? Colors.blue, 0.1),
onPressed: _toNextRoute
)
]
)
)
),
);
}
Future _toNextRoute() async
{
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))
)
);
}
}