Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added interceptor's #674

Merged
merged 14 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions examples/interceptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
**/ios/Flutter/.last_build_id
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Android Studio will place build artifacts here
/android/app/debug
/android/app/profile
/android/app/release
30 changes: 30 additions & 0 deletions examples/interceptors/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "b0850beeb25f6d5b10426284f506557f66181b36"
channel: "stable"

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
base_revision: b0850beeb25f6d5b10426284f506557f66181b36
- platform: web
create_revision: b0850beeb25f6d5b10426284f506557f66181b36
base_revision: b0850beeb25f6d5b10426284f506557f66181b36

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
8 changes: 8 additions & 0 deletions examples/interceptors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Interceptors Example

This example shows what you can achieve with BeamInterceptors. Interceptors are similar to BeamGuards, but interceptors can be dynamically added and removed from a BeamerDelegate.

<p align="center">
<img src="https://raw.githubusercontent.com/slovnicki/beamer/master/examples/interceptors/example-interceptors.gif" alt="example-interceptors">

Run `flutter create .` to generate all necessary files, if needed.
28 changes: 28 additions & 0 deletions examples/interceptors/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at https://dart.dev/lints.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
Binary file added examples/interceptors/example-interceptors.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions examples/interceptors/lib/app/beam_intercept_example_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:interceptors/main.dart';

class BeamInterceptExampleApp extends StatelessWidget {
const BeamInterceptExampleApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerDelegate: beamerDelegate,
routeInformationParser: BeamerParser(),
backButtonDispatcher: BeamerBackButtonDispatcher(delegate: beamerDelegate),
debugShowCheckedModeBanner: false,
builder: (context, child) => Scaffold(
appBar: AppBar(),
body: child,
),
);
}
}
38 changes: 38 additions & 0 deletions examples/interceptors/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:interceptors/app/beam_intercept_example_app.dart';
import 'package:interceptors/stack/interceptor_stack_to_block.dart';
import 'package:interceptors/stack/test_interceptor_stack.dart';

final beamerDelegate = BeamerDelegate(
transitionDelegate: const NoAnimationTransitionDelegate(),
beamBackTransitionDelegate: const NoAnimationTransitionDelegate(),
stackBuilder: BeamerStackBuilder(
beamStacks: [
TestInterceptorStack(),
IntercepterStackToBlock(),
],
),
);

final BeamInterceptor allowNavigatingInterceptor = BeamInterceptor(
intercept: (context, delegate, currentPages, origin, target, deepLink) => false,
enabled: true, // this can be false too
name: 'allow',
);

final BeamInterceptor blockNavigatingInterceptor = BeamInterceptor(
intercept: (context, delegate, currentPages, origin, target, deepLink) => target is IntercepterStackToBlock,
enabled: true,
name: 'block',
);

void main() {
Beamer.setPathUrlStrategy();
runApp(
BeamerProvider(
routerDelegate: beamerDelegate,
child: const BeamInterceptExampleApp(),
),
);
}
40 changes: 40 additions & 0 deletions examples/interceptors/lib/screens/allow_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:interceptors/main.dart';

class AllowScreen extends StatelessWidget {
const AllowScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BeamInterceptorPopScope(
interceptors: [allowNavigatingInterceptor],
beamerDelegate: beamerDelegate,
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
context.beamToNamed('/block-this-route');

ScaffoldMessenger.of(beamerDelegate.navigator.context).removeCurrentSnackBar();
ScaffoldMessenger.of(beamerDelegate.navigator.context).showSnackBar(
const SnackBar(content: Text('This route is NOT intercepted and thus NOT blocked.')),
);
},
child: const Text('Go to /block-this-route (not blocked)'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Go to back'),
),
],
),
),
),
);
}
}
40 changes: 40 additions & 0 deletions examples/interceptors/lib/screens/block_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:interceptors/main.dart';

class BlockScreen extends StatelessWidget {
const BlockScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return BeamInterceptorPopScope(
interceptors: [
blockNavigatingInterceptor,
],
beamerDelegate: beamerDelegate,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
context.beamToNamed('/block-this-route');

WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(beamerDelegate.navigator.context).removeCurrentSnackBar();
ScaffoldMessenger.of(beamerDelegate.navigator.context).showSnackBar(
const SnackBar(content: Text('This route is intercepted and thus blocked.')),
);
});
},
child: const Text('Go to blocked route'),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text('Go to back'),
),
],
),
);
}
}
50 changes: 50 additions & 0 deletions examples/interceptors/lib/stack/interceptor_stack_to_block.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';

class IntercepterStackToBlock extends BeamStack<BeamState> {
@override
List<Pattern> get pathPatterns => ['/block-this-route'];

@override
List<BeamPage> buildPages(BuildContext context, BeamState state) {
final pages = [
BeamPage(
key: const ValueKey('block-this-route'),
title: 'block-this-route',
type: BeamPageType.noTransition,
child: UnconstrainedBox(
child: SizedBox(
width: 500,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Super secret page', style: Theme.of(context).textTheme.titleLarge),
Padding(
padding: const EdgeInsets.only(top: 16),
child: SizedBox(
height: 50,
width: double.infinity,
child: ElevatedButton(
onPressed: () {
if (context.canBeamBack) {
context.beamBack();
} else if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
context.beamToNamed('/');
}
},
child: const Text('Go back'),
),
),
),
],
),
),
),
),
];

return pages;
}
}
68 changes: 68 additions & 0 deletions examples/interceptors/lib/stack/test_interceptor_stack.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:beamer/beamer.dart';
import 'package:flutter/material.dart';
import 'package:interceptors/screens/allow_screen.dart';
import 'package:interceptors/screens/block_screen.dart';

class TestInterceptorStack extends BeamStack<BeamState> {
@override
List<Pattern> get pathPatterns => ['/', '/allow', '/block'];

@override
List<BeamPage> buildPages(BuildContext context, BeamState state) {
final pages = [
BeamPage(
key: const ValueKey('intercepter'),
title: 'Intercepter',
type: BeamPageType.noTransition,
child: UnconstrainedBox(
child: SizedBox(
width: 250,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: SizedBox(
height: 50,
width: double.infinity,
child: ElevatedButton(
onPressed: () => context.beamToNamed('/allow'),
child: const Text('Allow navigating to test'),
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: SizedBox(
height: 50,
width: double.infinity,
child: ElevatedButton(
onPressed: () => context.beamToNamed('/block'),
child: const Text('Block navigating to test'),
),
),
),
],
),
),
),
),
if (state.uri.toString().contains('allow'))
const BeamPage(
key: ValueKey('allow'),
title: 'Allow',
type: BeamPageType.noTransition,
child: AllowScreen(),
),
if (state.uri.toString().contains('block'))
const BeamPage(
key: ValueKey('block'),
title: 'Block',
type: BeamPageType.noTransition,
child: BlockScreen(),
),
];

return pages;
}
}
Loading