2022-03-15 20:04:26 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
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;
|
2022-03-15 20:04:26 +03:00
|
|
|
|
|
|
|
const ResponsiveDialog(
|
2022-05-12 10:56:55 +03:00
|
|
|
{super.key,
|
2022-03-17 22:10:10 +03:00
|
|
|
required this.child,
|
|
|
|
this.title,
|
|
|
|
this.actions = const [],
|
2022-05-12 10:56:55 +03:00
|
|
|
this.onCancel});
|
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();
|
|
|
|
|
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) {
|
|
|
|
if (constraints.maxWidth < 540) {
|
|
|
|
// Fullscreen
|
2022-03-31 12:50:40 +03:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: widget.title,
|
|
|
|
actions: widget.actions,
|
2022-04-05 12:46:22 +03:00
|
|
|
leading: CloseButton(
|
2022-03-31 12:50:40 +03:00
|
|
|
onPressed: () {
|
|
|
|
widget.onCancel?.call();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
body: SingleChildScrollView(
|
2022-06-02 16:36:50 +03:00
|
|
|
padding: const EdgeInsets.all(20.0),
|
2022-03-31 12:50:40 +03:00
|
|
|
child: Container(key: _childKey, child: widget.child),
|
|
|
|
),
|
|
|
|
);
|
2022-03-16 11:13:46 +03:00
|
|
|
} else {
|
|
|
|
// Dialog
|
2022-03-25 16:39:35 +03:00
|
|
|
final cancelText = widget.onCancel == null && widget.actions.isEmpty
|
|
|
|
? 'Close'
|
|
|
|
: 'Cancel';
|
2022-07-05 13:11:43 +03:00
|
|
|
return AlertDialog(
|
|
|
|
title: widget.title,
|
|
|
|
scrollable: true,
|
|
|
|
content: SizedBox(
|
|
|
|
width: 380,
|
|
|
|
child: Container(key: _childKey, child: widget.child),
|
2022-03-25 17:46:52 +03:00
|
|
|
),
|
2022-07-05 13:11:43 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(cancelText),
|
|
|
|
onPressed: () {
|
|
|
|
widget.onCancel?.call();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
...widget.actions
|
|
|
|
],
|
2022-03-16 11:13:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}));
|
2022-03-15 20:04:26 +03:00
|
|
|
}
|