Skip to content

Commit

Permalink
refactor: replace flutter_portal package
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimozkn committed Sep 1, 2024
1 parent f503af8 commit 375931e
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 130 deletions.
4 changes: 0 additions & 4 deletions designer_v2/lib/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:flutter_portal/flutter_portal.dart';

class Config {
static const isDebugMode = false;

Expand All @@ -26,8 +24,6 @@ class Config {
static const participantInactiveDuration = 3;
}

const outPortalLabel = PortalLabel("out");

const kPathSeparator = ' / ';

const String rootRouteName = 'root';
Expand Down
254 changes: 147 additions & 107 deletions designer_v2/lib/features/dashboard/dashboard_page.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:studyu_core/core.dart';
import 'package:studyu_designer_v2/common_views/async_value_widget.dart';
import 'package:studyu_designer_v2/common_views/empty_body.dart';
import 'package:studyu_designer_v2/common_views/primary_button.dart';
import 'package:studyu_designer_v2/common_views/search.dart';
import 'package:studyu_designer_v2/constants.dart';
import 'package:studyu_designer_v2/features/dashboard/dashboard_controller.dart';
import 'package:studyu_designer_v2/features/dashboard/dashboard_scaffold.dart';
import 'package:studyu_designer_v2/features/dashboard/dashboard_state.dart';
Expand Down Expand Up @@ -42,6 +40,48 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
}
}

final GlobalKey _createMenuButtonKey = GlobalKey();

void _showDropdownMenu(BuildContext context,
{required DashboardController controller}) {
final RenderBox button =
_createMenuButtonKey.currentContext!.findRenderObject()! as RenderBox;
final RenderBox overlay =
Overlay.of(context).context.findRenderObject()! as RenderBox;

final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(
button.size.bottomLeft(Offset(0, 10)),
ancestor: overlay,
),
button.localToGlobal(
button.size.bottomRight(Offset(0, 10)),
ancestor: overlay,
),
),
Offset.zero & overlay.size,
);

showMenu(
context: context,
position: position,
items: [
_buildCreatePopupMenuItem(
title: tr.action_button_standalone_study_title,
subtitle: tr.action_button_standalone_study_subtitle,
onTap: () => controller.onClickNewStudy(false),
),
_buildCreatePopupMenuItem(
title: tr.action_button_template_title,
subtitle: tr.action_button_template_subtitle,
hint: tr.action_button_template_hint,
onTap: () => controller.onClickNewStudy(true),
),
],
);
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
Expand All @@ -54,64 +94,13 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
children: <Widget>[
Row(
children: [
PortalTarget(
visible: state.createNewMenuOpen,
portalCandidateLabels: const [outPortalLabel],
portalFollower: GestureDetector(
onTap: () => controller.setCreateNewMenuOpen(false),
child: Container(color: Colors.transparent),
),
child: const SizedBox.shrink(),
),
PortalTarget(
visible: state.createNewMenuOpen,
anchor: const Aligned(
follower: Alignment.topLeft,
target: Alignment.bottomLeft,
),
portalFollower: GestureDetector(
onTap: () => controller.setCreateNewMenuOpen(false),
child: Container(
width: 600,
margin: const EdgeInsets.only(top: 10.0),
child: Material(
color: theme.colorScheme.onPrimary,
borderRadius: BorderRadius.circular(16),
elevation: 20.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildCreateNewDropdownItem(
title: tr.action_button_standalone_study_title,
subtitle:
tr.action_button_standalone_study_subtitle,
onTap: () => controller.onClickNewStudy(false),
),
const Divider(
height: 0,
),
_buildCreateNewDropdownItem(
title: tr.action_button_template_title,
subtitle: tr.action_button_template_subtitle,
hint: tr.action_button_template_hint,
onTap: () => controller.onClickNewStudy(true),
),
],
),
),
),
),
),
child: SizedBox(
height: 36.0,
child: PrimaryButton(
text: tr.action_button_create,
onPressed: () => controller
.setCreateNewMenuOpen(!state.createNewMenuOpen),
),
SizedBox(
key: _createMenuButtonKey,
height: 36.0,
child: PrimaryButton(
text: tr.action_button_create,
onPressed: () =>
_showDropdownMenu(context, controller: controller),
),
),
const SizedBox(width: 28.0),
Expand All @@ -136,7 +125,7 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
),
],
),
const SizedBox(height: 24.0), // spacing between body elements
const SizedBox(height: 24.0),
FutureBuilder<StudyUUser>(
future: ref.read(userRepositoryProvider).fetchUser(),
builder: (context, snapshot) {
Expand Down Expand Up @@ -189,59 +178,110 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
);
}

Widget _buildCreateNewDropdownItem({
// Widget _buildCreateNewDropdownItem({
// required String title,
// required String subtitle,
// String? hint,
// GestureTapCallback? onTap,
// }) {
// final theme = Theme.of(context);
// return Material(
// color: theme.colorScheme.onPrimary,
// child: InkWell(
// onTap: onTap,
// child: Container(
// padding: const EdgeInsets.all(20),
// child: Row(
// children: [
// Expanded(
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// title,
// style: theme.textTheme.titleMedium
// ?.copyWith(color: theme.colorScheme.primary),
// ),
// Text(subtitle),
// if (hint != null)
// Padding(
// padding: const EdgeInsets.only(top: 8),
// child: Text(
// hint,
// style: const TextStyle(fontStyle: FontStyle.italic),
// ),
// )
// else
// const SizedBox.shrink(),
// ],
// ),
// ),
// const SizedBox(
// width: 20,
// ),
// Icon(
// Icons.add,
// color: theme.colorScheme.primary,
// size: 28,
// ),
// const SizedBox(
// width: 8,
// ),
// ],
// ),
// ),
// ),
// );
// }

PopupMenuItem _buildCreatePopupMenuItem({
required String title,
required String subtitle,
String? hint,
GestureTapCallback? onTap,
}) {
final theme = Theme.of(context);
return Material(
color: theme.colorScheme.onPrimary,
child: InkWell(
return PopupMenuItem(
onTap: onTap,
child: Container(
padding: const EdgeInsets.all(20),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: theme.textTheme.titleMedium
?.copyWith(color: theme.colorScheme.primary),
),
Text(subtitle),
if (hint != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
hint,
style: const TextStyle(fontStyle: FontStyle.italic),
),
)
else
const SizedBox.shrink(),
],
child: Column(
children: [
Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: theme.textTheme.titleMedium
?.copyWith(color: theme.colorScheme.primary),
),
Text(subtitle),
if (hint != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
hint,
style: const TextStyle(fontStyle: FontStyle.italic),
),
)
else
const SizedBox.shrink(),
],
),
),
),
const SizedBox(
width: 20,
),
Icon(
Icons.add,
color: theme.colorScheme.primary,
size: 28,
),
const SizedBox(
width: 8,
),
],
),
),
),
);
const SizedBox(
width: 10,
),
Icon(
Icons.add,
color: theme.colorScheme.primary,
size: 28,
),
],
),
const Divider()
],
));
}
}
11 changes: 1 addition & 10 deletions designer_v2/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:studyu_designer_v2/constants.dart';
import 'package:studyu_designer_v2/features/app.dart';
import 'package:studyu_designer_v2/utils/performance.dart';
import 'package:studyu_flutter_common/studyu_flutter_common.dart';
Expand All @@ -27,14 +25,7 @@ Future<void> main() async {
runApp(
// Make dependencies managed by Riverpod available in Widget.build methods
// by wrapping the app in a [ProviderScope]
const ProviderScope(
child: Portal(
child: Portal(
labels: [outPortalLabel],
child: App(),
),
),
),
const ProviderScope(child: App()),
);
}, (error, stackTrace) {
// TODO: top-level error handling
Expand Down
8 changes: 0 additions & 8 deletions designer_v2/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_portal:
dependency: "direct main"
description:
name: flutter_portal
sha256: "4601b3dc24f385b3761721bd852a3f6c09cddd4e943dd184ed58ee1f43006257"
url: "https://pub.dev"
source: hosted
version: "1.1.4"
flutter_riverpod:
dependency: "direct main"
description:
Expand Down
1 change: 0 additions & 1 deletion designer_v2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies:
flutter_colorpicker: ^1.1.0
flutter_localizations:
sdk: flutter
flutter_portal: ^1.1.4
flutter_riverpod: ^2.5.1
flutter_web_plugins:
sdk: flutter
Expand Down

0 comments on commit 375931e

Please sign in to comment.