Skip to content

Commit

Permalink
Feat: 对 navigation 2.0 进行封装
Browse files Browse the repository at this point in the history
  • Loading branch information
OHeroJ committed Mar 5, 2021
1 parent d512be3 commit 50b2564
Show file tree
Hide file tree
Showing 24 changed files with 742 additions and 8 deletions.
14 changes: 14 additions & 0 deletions lib/config/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
abstract class Config {
/// 请求url
String baseUrl =
"https://d6579fc5-c18b-443b-a2ef-01c2b6be51d5.bspapp.com/http";
}

/// 开发环境
class ConfigDebug extends Config {}

/// 预发布环境
class ConfigPreview extends Config {}

/// 生产环境
class ConfigProduct extends Config {}
11 changes: 11 additions & 0 deletions lib/locator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:get_it/get_it.dart';
import 'package:web_demo/config/config.dart';
import 'package:web_demo/services/platform/platform.dart';

GetIt locator = GetIt.instance;

setupLocator() {
// 配置项目环境
locator.registerSingleton<Config>(ConfigProduct());
locator.registerFactory<PlatformRepository>(() => PlatformRepositoryImpl());
}
27 changes: 20 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import 'package:flutter/material.dart';
import 'package:web_demo/locator.dart';
import 'package:web_demo/pages/home_page.dart';
import 'package:router_impl/router_impl.dart';
import 'package:web_demo/services/router/router.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await setupLocator();

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OldBirds',
RouterImpl impl = RouterImpl(
parser: LocationParserImpl(homeRouteBuilder: _homeRouteBuilder),
homeRouteBuilder: _homeRouteBuilder);

return MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
routeInformationParser: impl.routeInformationParser,
routerDelegate: impl.routerDelegate,
);
}

HomeRoute _homeRouteBuilder() {
return HomeRoute(builder: () {
return HomePage();
});
}
}
8 changes: 8 additions & 0 deletions lib/pages/unknown_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/material.dart';

class UnknownPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}
}
Empty file added lib/services/api/api.dart
Empty file.
49 changes: 49 additions & 0 deletions lib/services/platform/platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:io' as dart;
import 'package:flutter/foundation.dart';

enum Platform {
android,
fuchsia,
ios,
linux,
macos,
web,
windows,
}

extension PlatformExtension on Platform {
bool get isAndroid => this == Platform.android;
bool get isFuchsia => this == Platform.fuchsia;
bool get isIOS => this == Platform.ios;
bool get isLinux => this == Platform.linux;
bool get isMacos => this == Platform.macos;
bool get isWeb => this == Platform.web;
bool get isWindows => this == Platform.windows;
}

abstract class PlatformRepository {
Platform getPlatform();
}

class PlatformRepositoryImpl extends PlatformRepository {
Platform getPlatform() {
if (kIsWeb) {
return Platform.web;
}
if (dart.Platform.isAndroid) {
return Platform.android;
} else if (dart.Platform.isFuchsia) {
return Platform.fuchsia;
} else if (dart.Platform.isIOS) {
return Platform.ios;
} else if (dart.Platform.isLinux) {
return Platform.linux;
} else if (dart.Platform.isMacOS) {
return Platform.macos;
} else if (dart.Platform.isWindows) {
return Platform.windows;
} else {
throw UnimplementedError();
}
}
}
30 changes: 30 additions & 0 deletions lib/services/router/router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:web_demo/pages/unknown_page.dart';
import 'package:router_impl/router_impl.dart';

class LocationParserImpl extends LocationParser {
final HomeRoute Function() homeRouteBuilder;

LocationParserImpl({
@required this.homeRouteBuilder,
});

@override
AppRoute parse(String location) {
Uri uri = Uri.parse(location);

if (uri.pathSegments.isEmpty) {
return homeRouteBuilder();
}

return UnknownRoute();
}
}

class UnknownRoute extends AppRoute {
@override
Widget get child => UnknownPage();

@override
String get location => '/404';
}
Empty file added lib/style/style.dart
Empty file.
74 changes: 74 additions & 0 deletions plugins/router_impl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
10 changes: 10 additions & 0 deletions plugins/router_impl/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 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: 60bd88df915880d23877bfc1602e8ddcf4c4dd2a
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions plugins/router_impl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions plugins/router_impl/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
14 changes: 14 additions & 0 deletions plugins/router_impl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# router_impl

A new Flutter package.

## Getting Started

This project is a starting point for a Dart
[package](https://flutter.dev/developing-packages/),
a library module containing code that can be shared easily across
multiple Flutter or Dart projects.

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
89 changes: 89 additions & 0 deletions plugins/router_impl/lib/app_route.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

abstract class AppRoute {
const AppRoute();

String get location;
Widget get child;

Page<T> createPage<T>({Key key}) {
return AdaptivePage.create(
child: child,
key: key ?? UniqueKey(),
name: location,
);
}
}

class HomeRoute extends AppRoute {
final Widget Function() builder;

HomeRoute({@required this.builder});

@override
String get location => "/";

@override
Widget get child => builder();
}

class AdaptivePage {
static Page<T> create<T>({
@required Widget child,
bool maintainState = true,
bool fullscreenDialog = false,
LocalKey key,
String name,
Object arguments,
String title,
}) {
assert(child != null);
assert(maintainState != null);
assert(fullscreenDialog != null);

if (kIsWeb) {
return MaterialPage<T>(
key: key,
child: Builder(
builder: (context) => WillPopScope(
onWillPop: () async {
if (Navigator.of(context).userGestureInProgress) {
return false;
} else {
return true;
}
},
child: child,
),
),
maintainState: maintainState,
fullscreenDialog: fullscreenDialog,
name: name,
arguments: arguments,
);
} else if (Platform.isIOS) {
return CupertinoPage<T>(
key: key,
child: child,
maintainState: maintainState,
fullscreenDialog: fullscreenDialog,
name: name,
arguments: arguments,
title: title,
);
} else {
return MaterialPage<T>(
key: key,
child: child,
maintainState: maintainState,
fullscreenDialog: fullscreenDialog,
name: name,
arguments: arguments,
);
}
}
}
Loading

0 comments on commit 50b2564

Please sign in to comment.