Skip to content

Commit

Permalink
✨ Introduce UMEWidget.closeActivatedPlugin (bytedance#76)
Browse files Browse the repository at this point in the history
* ✨ Introduce `UMEWidget.closeActivatedPlugin`

* 🚀 Add example button

* 💚 ++

* Update root_widget.dart
  • Loading branch information
AlexV525 authored Sep 21, 2022
1 parent fd51d48 commit 1fc058c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 52 deletions.
23 changes: 15 additions & 8 deletions example/lib/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:dio/dio.dart';
import 'package:example/ume_switch.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ume/flutter_ume.dart';
import 'package:provider/provider.dart';

import 'main.dart';
Expand Down Expand Up @@ -34,15 +35,21 @@ class _HomePageState extends State<HomePage> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
debugPrint('statement');
},
child: const Text('debugPrint')),
onPressed: () => UMEWidget.closeActivatedPlugin(),
child: const Text('Close activated plugin'),
),
TextButton(
onPressed: () {
debugPrint('statement');
},
child: const Text('debugPrint'),
),
TextButton(
onPressed: () {
Navigator.of(context).pushNamed('detail');
},
child: const Text('Push Detail Page')),
onPressed: () {
Navigator.of(context).pushNamed('detail');
},
child: const Text('Push Detail Page'),
),
TextButton(
onPressed: () {
showDialog(
Expand Down
4 changes: 1 addition & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ class _UMEAppState extends State<UMEApp> {
onGenerateRoute: (settings) {
switch (settings.name) {
case 'detail':
return MaterialPageRoute(
builder: (BuildContext context) => DetailPage(),
);
return MaterialPageRoute(builder: (_) => const DetailPage());
default:
return null;
}
Expand Down
43 changes: 19 additions & 24 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,48 +1,43 @@
name: example
description: A new Flutter project.
version: 1.0.1
publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.3
provider: ^6.0.3
# For Dio Kit
dio: ^4.0.0

dependency_overrides:
vm_service: ^6.2.0
# flutter_ume:
# path: ../
# flutter_ume_kit_ui:
# path: ../kits/flutter_ume_kit_ui
# flutter_ume_kit_perf:
# path: ../kits/flutter_ume_kit_perf
# flutter_ume_kit_show_code:
# path: ../kits/flutter_ume_kit_show_code
# flutter_ume_kit_device:
# path: ../kits/flutter_ume_kit_device
# flutter_ume_kit_console:
# path: ../kits/flutter_ume_kit_console
# flutter_ume_kit_dio:
# path: ../kits/flutter_ume_kit_dio
vm_service: ^9.4.0
flutter_ume:
path: ../

dev_dependencies:
flutter_driver:
sdk: flutter
test: ^1.16.6
flutter_ume: ^1.0.1
flutter_ume_kit_ui: ^1.0.0
flutter_ume_kit_perf: ^1.0.0
flutter_ume_kit_show_code: ^1.0.0
flutter_ume_kit_device: ^1.0.0
flutter_ume_kit_console: ^1.0.0
flutter_ume_kit_dio: ^1.0.0
flutter_ume:
path: ../
flutter_ume_kit_ui:
path: ../kits/flutter_ume_kit_ui
flutter_ume_kit_perf:
path: ../kits/flutter_ume_kit_perf
flutter_ume_kit_show_code:
path: ../kits/flutter_ume_kit_show_code
flutter_ume_kit_device:
path: ../kits/flutter_ume_kit_device
flutter_ume_kit_console:
path: ../kits/flutter_ume_kit_console
flutter_ume_kit_dio:
path: ../kits/flutter_ume_kit_dio
flutter_ume_kit_channel_monitor:
path: ../kits/flutter_ume_kit_channel_monitor

flutter:
uses-material-design: true
66 changes: 49 additions & 17 deletions lib/core/ui/root_widget.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'
hide FlutterLogo, FlutterLogoDecoration, FlutterLogoStyle;
import 'package:flutter_ume/core/pluggable_message_service.dart';
Expand All @@ -18,7 +17,6 @@ import 'package:flutter_localizations/flutter_localizations.dart';
const defaultLocalizationsDelegates = const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
];

Expand All @@ -39,13 +37,40 @@ class UMEWidget extends StatefulWidget {
final Iterable<Locale>? supportedLocales;
final Iterable<LocalizationsDelegate> localizationsDelegates;

/// Close the activated plugin if any.
///
/// The method does not have side-effects whether the [UMEWidget]
/// is not enabled or no plugin has been activated.
static void closeActivatedPlugin() {
final _ContentPageState? state =
_umeWidgetState?._contentPageKey.currentState;
if (state?._currentSelected != null) {
state?._closeActivatedPluggable();
}
}

@override
_UMEWidgetState createState() => _UMEWidgetState();
}

/// Hold the [_UMEWidgetState] as a global variable.
_UMEWidgetState? _umeWidgetState;

class _UMEWidgetState extends State<UMEWidget> {
late Widget _child;
_UMEWidgetState() {
// Make sure only a single `UMEWidget` is being used.
assert(
_umeWidgetState == null,
'Only one `UMEWidget` can be used at the same time.',
);
if (_umeWidgetState != null) {
throw StateError('Only one `UMEWidget` can be used at the same time.');
}
_umeWidgetState = this;
}

final GlobalKey<_ContentPageState> _contentPageKey = GlobalKey();
late Widget _child;
VoidCallback? _onMetricsChanged;

bool _overlayEntryInserted = false;
Expand Down Expand Up @@ -77,6 +102,8 @@ class _UMEWidgetState extends State<UMEWidget> {
_onMetricsChanged;
}
super.dispose();
// Do the cleaning at last.
_umeWidgetState = null;
}

@override
Expand Down Expand Up @@ -152,6 +179,7 @@ class _UMEWidgetState extends State<UMEWidget> {
builder: (_) => Material(
type: MaterialType.transparency,
child: _ContentPage(
key: _contentPageKey,
refreshChildLayout: () {
_replaceChild();
setState(() {});
Expand All @@ -170,15 +198,15 @@ class _UMEWidgetState extends State<UMEWidget> {
}

class _ContentPage extends StatefulWidget {
_ContentPage({Key? key, this.refreshChildLayout}) : super(key: key);
const _ContentPage({Key? key, this.refreshChildLayout}) : super(key: key);

final VoidCallback? refreshChildLayout;

@override
__ContentPageState createState() => __ContentPageState();
_ContentPageState createState() => _ContentPageState();
}

class __ContentPageState extends State<_ContentPage> {
class _ContentPageState extends State<_ContentPage> {
PluginStoreManager _storeManager = PluginStoreManager();
Size _windowSize = windowSize;
double _dx = 0;
Expand Down Expand Up @@ -218,23 +246,27 @@ class __ContentPageState extends State<_ContentPage> {

void onTap() {
if (_currentSelected != null) {
PluginManager.instance.deactivatePluggable(_currentSelected!);
if (widget.refreshChildLayout != null) {
widget.refreshChildLayout!();
}
_currentSelected = null;
_currentWidget = _empty;
if (_minimalContent) {
_currentWidget = _toolbarWidget;
_showedMenu = true;
}
setState(() {});
_closeActivatedPluggable();
return;
}
_showedMenu = !_showedMenu;
_updatePanelWidget();
}

void _closeActivatedPluggable() {
PluginManager.instance.deactivatePluggable(_currentSelected!);
if (widget.refreshChildLayout != null) {
widget.refreshChildLayout!();
}
_currentSelected = null;
_currentWidget = _empty;
if (_minimalContent) {
_currentWidget = _toolbarWidget;
_showedMenu = true;
}
setState(() {});
}

void _updatePanelWidget() {
setState(() {
_currentWidget =
Expand Down

0 comments on commit 1fc058c

Please sign in to comment.