Skip to content

Commit

Permalink
docs(katana_router): Organize Example.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Oct 20, 2022
1 parent b45e1f7 commit 33b8dfe
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 283 deletions.
245 changes: 226 additions & 19 deletions packages/katana_router/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:katana_router/katana_router.dart';
import 'test.dart';

import 'main.router.dart';

part 'main.page.dart';

@appRoute
final appRouter = AppRouter();

Expand Down Expand Up @@ -50,34 +51,240 @@ class Boot extends BootRouteQueryBuilder {
TransitionQuery get initialTransitionQuery => TransitionQuery.fade;
}

class Localize {
const Localize._();
@PagePath("/", name: "main")
class MainPage extends StatelessWidget {
const MainPage({
super.key,
required this.title,
});

final String title;

@pageRouteQuery
static const query = _$MainPage();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("MainPage"),
),
body: ListView(
children: [
ListTile(
title: const Text("UserPage"),
onTap: () async {
await context.router.push<String>(
UserPage.query(userId: "UserID"),
TransitionQuery.fullscreen,
);
},
),
ListTile(
title: const Text("ContentPage"),
onTap: () async {
await context.router
.replace<int>(ContentPage.query(contentId: "ContentID"));
},
),
ListTile(
title: const Text("NestedPage"),
onTap: () async {
context.router.push(NestedContainerPage.query());
},
),
ListTile(
title: const Text("Pop"),
onTap: () {
context.router.pop();
},
)
],
));
}
}

@PagePath("/page/:user_id", name: "user")
class UserPage extends StatelessWidget {
const UserPage({
super.key,
required this.userId,
});
final String userId;

@pageRouteQuery
static const query = _$UserPage();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("UserPage")),
body: Center(
child: Text(userId),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.router.pop("Back to MainPage");
},
child: const Icon(Icons.abc),
),
);
}
}

@PagePath("/content/:content_id")
class ContentPage extends StatelessWidget {
const ContentPage({
super.key,
required this.contentId,
});

final String contentId;

@pageRouteQuery
static const query = _$ContentPage();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("ContentPage")),
body: Center(
child: Text(contentId),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.router.pop(100);
},
child: const Icon(Icons.abc),
),
);
}
}

@PagePath("/nested", name: "nested")
class NestedContainerPage extends StatefulWidget {
const NestedContainerPage({
super.key,
});

static const _map = {
"ja": _$LocalizejaJP(Locale("ja", "JP")),
@pageRouteQuery
static const query = _$NestedContainerPage();

@override
State<StatefulWidget> createState() => _NestedContainerPageState();
}

class _NestedContainerPageState extends State<NestedContainerPage> {
final router = AppRouter(
initialQuery: InnerPage1.query(),
defaultTransitionQuery: TransitionQuery.fade,
pages: [
InnerPage1.query,
InnerPage2.query,
],
);

final queries = {
InnerPageType.type1: InnerPage1.query(),
InnerPageType.type2: InnerPage2.query(),
};

_$LocalizeBase of(BuildContext context) {
final locale = Localizations.localeOf(context);
final lang = locale.languageCode;
if (!_map.containsKey(lang)) {
return _map.entries.first.value;
}
return _map[lang]!;
@override
void initState() {
super.initState();
router.addListener(handledOnUpdate);
}

void handledOnUpdate() {
setState(() {});
}

@override
void dispose() {
super.dispose();
router.removeListener(handledOnUpdate);
router.dispose();
}

@override
Widget build(BuildContext context) {
final query = router.currentQuery;
return Scaffold(
appBar: AppBar(title: const Text("NestedPage")),
body: Router.withConfig(config: router),
bottomNavigationBar: BottomNavigationBar(
onTap: (value) {
router.push(
queries[InnerPageType.values[value]]!,
);
},
currentIndex: query?.key<InnerPageType>()?.index ?? 0,
items: InnerPageType.values.map((type) {
return BottomNavigationBarItem(
icon: Icon(type.icon),
label: type.label,
);
}).toList(),
),
);
}
}

@NestedPage(key: InnerPageType.type1)
class InnerPage1 extends StatelessWidget {
const InnerPage1({super.key});

static const query = _$InnerPage1();

@override
Widget build(BuildContext context) {
final current = context.rootRouter.currentQuery;
return Center(
child: TextButton(
onPressed: () {
context.rootRouter.pop();
},
child: Text("To Innerpage2 ${current?.name}"),
),
);
}
}

class _$LocalizejaJP extends _$LocalizeBase {
const _$LocalizejaJP(super.locale);
@NestedPage(key: InnerPageType.type2)
class InnerPage2 extends StatelessWidget {
const InnerPage2({super.key});

static const query = _$InnerPage2();

@override
String get aaa => "aaa";
Widget build(BuildContext context) {
return Center(
child: TextButton(
onPressed: () {
context.router.push(InnerPage1.query());
},
child: Text("To Innerpage1"),
),
);
}
}

abstract class _$LocalizeBase {
const _$LocalizeBase(this.locale);
enum InnerPageType {
type1(
icon: Icons.people,
label: "people",
),
type2(
icon: Icons.settings,
label: "settings",
);

const InnerPageType({
required this.icon,
required this.label,
});

final Locale locale;
final IconData icon;

String get aaa => throw UnimplementedError();
final String label;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore_for_file: unused_field, unused_element, require_trailing_commas, prefer_const_constructors, unnecessary_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_in_if_null_operators, library_prefixes, directives_ordering

part of 'test.dart';
part of 'main.dart';

// **************************************************************************
// PageGenerator
Expand Down Expand Up @@ -198,9 +198,9 @@ class _$_InnerPage1 extends RouteQuery {
const _$_InnerPage1();

@override
String get path => "0bd4da1e295946faa232f3e6b4b00099";
String get path => "67bbf6d381b241b2a4b9494990232bae";
@override
String get name => "type1";
String get name => path;
@override
E? key<E>() => InnerPageType.type1 as E?;
@override
Expand Down Expand Up @@ -231,9 +231,9 @@ class _$_InnerPage2 extends RouteQuery {
const _$_InnerPage2();

@override
String get path => "7b6a4dfdcea64b5cb375dd1c4301c144";
String get path => "54392ce8302f4348b8cdfda2213660bc";
@override
String get name => "type2";
String get name => path;
@override
E? key<E>() => InnerPageType.type2 as E?;
@override
Expand Down
5 changes: 2 additions & 3 deletions packages/katana_router/example/lib/main.router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:katana_router/katana_router.dart';
import 'package:katana_router_example/test.dart' as _$1;
export 'package:katana_router/katana_router.dart';
export 'package:katana_router_example/test.dart'
import 'package:katana_router_example/main.dart' as _$1;
export 'package:katana_router_example/main.dart'
show MainPage, UserPage, ContentPage, NestedContainerPage;

class AppRouter extends AppRouterBase {
Expand Down
Loading

0 comments on commit 33b8dfe

Please sign in to comment.