blob: 595bb66c62b966eda70e4239633bec6625f03baf [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
void main() => runApp(App());
const blue = Color(0xFF2196F3);
const white = Color(0xFFFFFFFF);
class App extends StatelessWidget {
App({Key? key}) : super(key: key);
static const title = 'GoRouter Example: WidgetsApp';
@override
Widget build(BuildContext context) => WidgetsApp.router(
routeInformationParser: _router.routeInformationParser,
routerDelegate: _router.routerDelegate,
title: title,
color: blue,
textStyle: const TextStyle(color: blue),
);
final _router = GoRouter(
debugLogDiagnostics: true,
routes: [
GoRoute(
path: '/',
builder: (context, state) => const Page1Screen(),
),
GoRoute(
path: '/page2',
builder: (context, state) => const Page2Screen(),
),
],
);
}
class Page1Screen extends StatelessWidget {
const Page1Screen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
App.title,
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Button(
onPressed: () => context.go('/page2'),
child: const Text(
'Go to page 2',
style: TextStyle(color: white),
),
),
],
),
),
);
}
class Page2Screen extends StatelessWidget {
const Page2Screen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) => SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
App.title,
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 16),
Button(
onPressed: () => context.go('/'),
child: const Text(
'Go to home page',
style: TextStyle(color: white),
),
),
],
),
),
);
}
class Button extends StatelessWidget {
const Button({
required this.onPressed,
required this.child,
Key? key,
}) : super(key: key);
final VoidCallback onPressed;
final Widget child;
@override
Widget build(BuildContext context) => GestureDetector(
onTap: onPressed,
child: Container(
padding: const EdgeInsets.all(8),
color: blue,
child: child,
),
);
}