Created
June 22, 2020 08:23
-
-
Save Laebrye/1714137866e0dfa4ee1d3985119cc4e9 to your computer and use it in GitHub Desktop.
Platform specific dialog flow widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:blockmark_test/ui/common_widgets/platform_widget.dart'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/src/widgets/framework.dart'; | |
class PlatformAlertDialog extends PlatformWidget { | |
PlatformAlertDialog({ | |
@required this.title, | |
@required this.content, | |
@required this.defaultActionText, | |
this.cancelActionText, | |
}) : assert(title != null), | |
assert(content != null), | |
assert(defaultActionText != null); | |
Future<bool> show(BuildContext context) async { | |
return Platform.isIOS | |
? await showCupertinoDialog<bool>( | |
context: context, | |
builder: (context) => this, | |
) | |
: await showDialog<bool>( | |
context: context, | |
builder: (context) => this, | |
); | |
} | |
final String title; | |
final String content; | |
final String defaultActionText; | |
final String cancelActionText; | |
@override | |
Widget buildCupertinoWidget(BuildContext context) { | |
return CupertinoAlertDialog( | |
title: Text(title), | |
content: Text(content), | |
actions: _buildActions(context), | |
); | |
} | |
@override | |
Widget buildMaterialWidget(BuildContext context) { | |
return AlertDialog( | |
title: Text(title), | |
content: Text(content), | |
actions: _buildActions(context), | |
); | |
} | |
List<Widget> _buildActions(BuildContext context) { | |
final actions = <Widget>[]; | |
if (cancelActionText != null) { | |
actions.add(PlatformAlertDialogAction( | |
onPressed: () => Navigator.of(context).pop(false), | |
child: Text(cancelActionText), | |
)); | |
} | |
actions.add(PlatformAlertDialogAction( | |
onPressed: () => Navigator.of(context).pop(true), | |
child: Text(defaultActionText), | |
)); | |
return actions; | |
} | |
} | |
class PlatformAlertDialogAction extends PlatformWidget { | |
PlatformAlertDialogAction({ | |
@required this.child, | |
@required this.onPressed, | |
}) : assert(onPressed != null), | |
assert(child != null); | |
final Widget child; | |
final VoidCallback onPressed; | |
@override | |
Widget buildCupertinoWidget(BuildContext context) { | |
return CupertinoDialogAction( | |
child: child, | |
onPressed: onPressed, | |
); | |
} | |
@override | |
Widget buildMaterialWidget(BuildContext context) { | |
return FlatButton( | |
child: child, | |
onPressed: onPressed, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment