|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter/cupertino.dart'; |
| 3 | + |
| 4 | +class DialogActions { |
| 5 | + DialogActions({this.text, this.onPressed}); |
| 6 | + final String text; |
| 7 | + final VoidCallback onPressed; |
| 8 | +} |
| 9 | + |
| 10 | +class _AndroidDialog extends StatelessWidget { |
| 11 | + _AndroidDialog({Key key, this.title, this.content, this.actions}) : super(key: key); |
| 12 | + |
| 13 | + final String title; |
| 14 | + final String content; |
| 15 | + final List<DialogActions> actions; |
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + final ThemeData theme = Theme.of(context); |
| 19 | + final TextStyle dialogTextStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color); |
| 20 | + return AlertDialog( |
| 21 | + title: title != null ? Text(title) : null, |
| 22 | + content: Text( |
| 23 | + content, |
| 24 | + style: dialogTextStyle, |
| 25 | + ), |
| 26 | + actions: actions |
| 27 | + .map((action) => FlatButton( |
| 28 | + child: Text(action.text), |
| 29 | + onPressed: () { |
| 30 | + Navigator.pop(context); |
| 31 | + if (action.onPressed != null) action.onPressed(); |
| 32 | + }, |
| 33 | + )) |
| 34 | + .toList(), |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class _IosDialog extends StatelessWidget { |
| 40 | + _IosDialog({Key key, this.title, this.content, this.actions}) : super(key: key); |
| 41 | + |
| 42 | + final String title; |
| 43 | + final String content; |
| 44 | + final List<DialogActions> actions; |
| 45 | + @override |
| 46 | + Widget build(BuildContext context) { |
| 47 | + // TODO: implement build |
| 48 | + return CupertinoAlertDialog( |
| 49 | + title: title != null ? Text(title) : null, |
| 50 | + content: Text(content), |
| 51 | + actions: actions |
| 52 | + .map( |
| 53 | + (action) => CupertinoDialogAction( |
| 54 | + child: Text(action.text), |
| 55 | + isDefaultAction: true, |
| 56 | + onPressed: () { |
| 57 | + Navigator.pop(context); |
| 58 | + if (action.onPressed != null) action.onPressed(); |
| 59 | + }, |
| 60 | + ), |
| 61 | + ) |
| 62 | + .toList()); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class WDialog { |
| 67 | + static show(BuildContext context, {String title, String content, List<DialogActions> actions = const [], TargetPlatform platform = TargetPlatform.android}) { |
| 68 | + if (platform == TargetPlatform.android) { |
| 69 | + showDialog( |
| 70 | + context: context, |
| 71 | + builder: (_) => _AndroidDialog( |
| 72 | + title: title, |
| 73 | + content: content, |
| 74 | + actions: actions, |
| 75 | + )); |
| 76 | + } else { |
| 77 | + showDialog( |
| 78 | + context: context, |
| 79 | + builder: (_) => _IosDialog( |
| 80 | + title: title, |
| 81 | + content: content, |
| 82 | + actions: actions, |
| 83 | + )); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments