2022-10-04 13:12:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Yubico.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-07 14:59:44 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-03-08 11:30:47 +03:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
|
|
|
|
import '../core/state.dart';
|
2022-03-15 20:04:26 +03:00
|
|
|
|
2022-03-25 16:28:02 +03:00
|
|
|
class ResponsiveDialog extends StatefulWidget {
|
2022-03-15 20:04:26 +03:00
|
|
|
final Widget? title;
|
|
|
|
final Widget child;
|
2022-03-16 11:13:46 +03:00
|
|
|
final List<Widget> actions;
|
2022-03-17 22:10:10 +03:00
|
|
|
final Function()? onCancel;
|
2023-06-16 18:27:10 +03:00
|
|
|
final bool allowCancel;
|
2022-03-15 20:04:26 +03:00
|
|
|
|
2023-06-16 18:27:10 +03:00
|
|
|
const ResponsiveDialog({
|
|
|
|
super.key,
|
|
|
|
required this.child,
|
|
|
|
this.title,
|
|
|
|
this.actions = const [],
|
|
|
|
this.onCancel,
|
|
|
|
this.allowCancel = true,
|
|
|
|
});
|
2022-03-15 20:04:26 +03:00
|
|
|
|
2022-03-25 16:28:02 +03:00
|
|
|
@override
|
|
|
|
State<ResponsiveDialog> createState() => _ResponsiveDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ResponsiveDialogState extends State<ResponsiveDialog> {
|
|
|
|
final Key _childKey = GlobalKey();
|
2023-06-19 15:04:58 +03:00
|
|
|
final _focus = FocusScopeNode();
|
2023-07-27 15:25:10 +03:00
|
|
|
bool _hasLostFocus = false;
|
2023-06-19 15:04:58 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
_focus.dispose();
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:29:39 +03:00
|
|
|
String _getCancelText(BuildContext context) {
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
return widget.onCancel == null && widget.actions.isEmpty
|
|
|
|
? l10n.s_close
|
|
|
|
: l10n.s_cancel;
|
|
|
|
}
|
|
|
|
|
2023-06-19 15:04:58 +03:00
|
|
|
Widget _buildFullscreen(BuildContext context) => Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: widget.title,
|
|
|
|
actions: widget.actions,
|
|
|
|
leading: IconButton(
|
2024-04-10 14:29:39 +03:00
|
|
|
tooltip: _getCancelText(context),
|
2024-03-08 11:30:47 +03:00
|
|
|
icon: const Icon(Symbols.close),
|
2023-06-19 15:04:58 +03:00
|
|
|
onPressed: widget.allowCancel
|
|
|
|
? () {
|
|
|
|
widget.onCancel?.call();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
: null),
|
|
|
|
),
|
|
|
|
body: SingleChildScrollView(
|
|
|
|
child:
|
|
|
|
SafeArea(child: Container(key: _childKey, child: widget.child)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
Widget _buildDialog(BuildContext context) {
|
2024-01-11 18:11:45 +03:00
|
|
|
return PopScope(
|
2024-02-02 15:38:21 +03:00
|
|
|
canPop: widget.allowCancel,
|
2024-01-11 18:11:45 +03:00
|
|
|
child: AlertDialog(
|
|
|
|
title: widget.title,
|
|
|
|
titlePadding: const EdgeInsets.only(top: 24, left: 18, right: 18),
|
|
|
|
scrollable: true,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(vertical: 8),
|
|
|
|
content: SizedBox(
|
|
|
|
width: 600,
|
|
|
|
child: Container(key: _childKey, child: widget.child),
|
2023-06-19 15:04:58 +03:00
|
|
|
),
|
2024-01-11 18:11:45 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2024-02-02 15:38:21 +03:00
|
|
|
onPressed: widget.allowCancel
|
|
|
|
? () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
: null,
|
2024-04-10 14:29:39 +03:00
|
|
|
child: Text(_getCancelText(context)),
|
2024-01-11 18:11:45 +03:00
|
|
|
),
|
|
|
|
...widget.actions
|
|
|
|
],
|
|
|
|
),
|
2024-08-13 16:22:22 +03:00
|
|
|
onPopInvokedWithResult: (didPop, _) {
|
2024-01-11 18:11:45 +03:00
|
|
|
if (didPop) {
|
|
|
|
widget.onCancel?.call();
|
|
|
|
}
|
|
|
|
},
|
2023-06-19 15:04:58 +03:00
|
|
|
);
|
|
|
|
}
|
2022-03-25 16:28:02 +03:00
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
@override
|
2022-03-16 11:13:46 +03:00
|
|
|
Widget build(BuildContext context) =>
|
|
|
|
LayoutBuilder(builder: ((context, constraints) {
|
2023-08-31 17:52:08 +03:00
|
|
|
var maxWidth = isDesktop ? 400 : 600;
|
2023-06-19 15:04:58 +03:00
|
|
|
// This keeps the focus in the dialog, even if the underlying page changes.
|
|
|
|
return FocusScope(
|
|
|
|
node: _focus,
|
|
|
|
autofocus: true,
|
|
|
|
onFocusChange: (focused) {
|
2023-07-27 15:25:10 +03:00
|
|
|
if (!focused && !_hasLostFocus) {
|
2023-06-19 15:04:58 +03:00
|
|
|
_focus.requestFocus();
|
2023-07-27 15:25:10 +03:00
|
|
|
_hasLostFocus = true;
|
2023-06-19 15:04:58 +03:00
|
|
|
}
|
|
|
|
},
|
2023-08-31 17:52:08 +03:00
|
|
|
child: constraints.maxWidth < maxWidth
|
2023-06-19 15:04:58 +03:00
|
|
|
? _buildFullscreen(context)
|
|
|
|
: _buildDialog(context),
|
|
|
|
);
|
2022-03-16 11:13:46 +03:00
|
|
|
}));
|
2022-03-15 20:04:26 +03:00
|
|
|
}
|