Skip to content

Commit

Permalink
🌍Use based_split_view to adapt big screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Cierra-Runis committed Nov 10, 2023
1 parent acb14a8 commit be02028
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 38 deletions.
11 changes: 2 additions & 9 deletions lib/common/extension.dart
@@ -1,18 +1,11 @@
import 'package:mercurius/index.dart';

extension BuildContextExtension on BuildContext {
Future<T?> push<T extends Object?>(Widget page) =>
Navigator.push(this, CupertinoPageRoute<T>(builder: (_) => page));
Future<T?> push<T extends Object?>(Widget page) => splitViewKey.currentState!
.push(CupertinoPageRoute<T>(builder: (_) => page));

void pop<T extends Object?>([T? result]) => Navigator.pop(this, result);

Future<T?> pushAndRemoveRoot<T extends Object?>(Widget page) =>
Navigator.pushAndRemoveUntil(
this,
CupertinoPageRoute<T>(builder: (_) => page),
(_) => false,
);

ColorScheme get colorScheme => Theme.of(this).colorScheme;
Brightness get brightness => colorScheme.brightness;

Expand Down
5 changes: 0 additions & 5 deletions lib/common/mercurius.dart
Expand Up @@ -96,11 +96,6 @@ class App {
await FlutterDisplayMode.setHighRefreshRate();
}

/// 固定竖屏
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);

/// 启动
runApp(
ProviderScope(
Expand Down
4 changes: 2 additions & 2 deletions lib/index.dart
Expand Up @@ -14,8 +14,7 @@ export 'main.dart';
/// flutter 相关
/// [RefreshCallback]`export 'package:flutter/material.dart'` 冲突,两者近似
export 'package:flutter/cupertino.dart' hide RefreshCallback;
export 'package:flutter/services.dart'
show DeviceOrientation, SystemChrome; // 设备服务

/// [Badge]`export 'package:badges/badges.dart'; // 小红点提示` 冲突,我想用外部包
export 'package:flutter/material.dart' hide Badge;
export 'package:flutter/gestures.dart';
Expand All @@ -30,6 +29,7 @@ export 'dart:ui' show ImageFilter;
export 'package:another_flushbar/flushbar.dart'; // 提示框
export 'package:badges/badges.dart'; // 小红点提示
export 'package:based_list/based_list.dart';
export 'package:based_split_view/based_split_view.dart';
export 'package:cross_file/cross_file.dart'; // 文件操作
export 'package:dio/dio.dart'; // 网络请求
/// [Interval]`package:flutter/src/animation/curves.dart` 冲突,两者结构完全不同,但外部包里的这个用不到
Expand Down
77 changes: 57 additions & 20 deletions lib/pages/root_page.dart
@@ -1,44 +1,81 @@
import 'package:mercurius/index.dart';

class RootPage extends ConsumerStatefulWidget {
const RootPage({super.key});
final splitViewKey = GlobalKey<NavigatorState>();

class RootView extends StatefulWidget {
const RootView({super.key});

@override
ConsumerState<RootPage> createState() => _MercuriusRouteState();
State<RootView> createState() => _RootViewState();
}

class _MercuriusRouteState extends ConsumerState<RootPage> {
class _RootViewState extends State<RootView> {
int _currentIndex = 0;

void _onItemTapped(int index) {
setState(() => _currentIndex = index);
}

@override
Widget build(BuildContext context) {
final l10n = context.l10n;

return DoubleBack(
message: l10n.backAgainToExit,
background: context.colorScheme.outline.withAlpha(16),
backgroundRadius: BorderRadius.circular(16),
condition: _currentIndex == 0,
onConditionFail: () => _onItemTapped(0),
child: BasedSplitView(
splitMode: SplitMode.width,
navigatorKey: splitViewKey,
leftWidget: RootPage(
currentIndex: _currentIndex,
onItemTapped: _onItemTapped,
),
leftWidth: 364,
breakPoint: 364 * 2,
rightPlaceholder: const Scaffold(
body: Center(
child: AppIcon(
size: 96,
),
),
),
),
);
}
}

class RootPage extends StatelessWidget {
const RootPage({
super.key,
required this.currentIndex,
required this.onItemTapped,
});

final int currentIndex;
final void Function(int) onItemTapped;

static const List<Widget> _bodyWidgets = [
HomePage(key: ValueKey(HomePage)),
MorePage(key: ValueKey(MorePage)),
];

void _onItemTapped(int index) {
setState(() => _currentIndex = index);
}

@override
Widget build(BuildContext context) {
final l10n = context.l10n;

return Scaffold(
body: Center(
child: DoubleBack(
message: l10n.backAgainToExit,
background: context.colorScheme.outline.withAlpha(16),
backgroundRadius: BorderRadius.circular(16),
condition: _currentIndex == 0,
onConditionFail: () => setState(() => _currentIndex = 0),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: _bodyWidgets[_currentIndex],
),
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
child: _bodyWidgets[currentIndex],
),
),
bottomNavigationBar: NavigationBar(
selectedIndex: _currentIndex,
onDestinationSelected: _onItemTapped,
selectedIndex: currentIndex,
onDestinationSelected: onItemTapped,
destinations: [
NavigationDestination(
icon: const Icon(Icons.home),
Expand Down
6 changes: 5 additions & 1 deletion lib/pages/splash_page.dart
Expand Up @@ -23,7 +23,11 @@ class _MercuriusSplashPageState extends State<SplashPage>
_animation.addStatusListener(
(status) {
if (status == AnimationStatus.completed) {
context.pushAndRemoveRoot(const RootPage());
Navigator.pushAndRemoveUntil(
context,
CupertinoPageRoute(builder: (_) => const RootView()),
(_) => false,
);
}
},
);
Expand Down
3 changes: 2 additions & 1 deletion lib/widgets/diary/list/diary_list_item_widget.dart
Expand Up @@ -80,7 +80,8 @@ class DiaryListItemWidget extends ConsumerWidget {
onTap: onTap ??
() => {
showDialog<void>(
context: context,
context: splitViewKey.currentContext ?? context,
useRootNavigator: false,
builder: (context) => DiaryPageViewWidget(
diary: diary,
),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Expand Up @@ -65,6 +65,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.2"
based_split_view:
dependency: "direct main"
description:
name: based_split_view
sha256: dd7e5cd13c1d53d7cbd62252a8e274a4a7491dc74d5da4d9a0582961d0315226
url: "https://pub.dev"
source: hosted
version: "1.0.0"
boolean_selector:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -16,6 +16,7 @@ dependencies:
another_flushbar: ^1.12.30 # 提示框
badges: ^3.1.2 # 小红点提示
based_list: ^1.1.2
based_split_view: ^1.0.0
cross_file: ^0.3.3+6 # 文件操作
dio: ^5.3.3 # 网络请求
dart_date: ^1.3.2 # 转换时间工具
Expand Down

0 comments on commit be02028

Please sign in to comment.