confirm_dialog.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'dart:ui';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. class ConfirmDialogWidget extends StatefulWidget {
  5. String title;
  6. String message;
  7. String negativeText;
  8. String positiveText;
  9. Function onCloseEvent;
  10. Function onPositivePressEvent;
  11. Function keybackEvent;
  12. double minHeight;
  13. double barHeight = 48;
  14. double radius = 20;
  15. double msgFontSize;
  16. ConfirmDialogWidget(
  17. {@required this.title,
  18. @required this.message,
  19. this.negativeText,
  20. this.positiveText,
  21. this.msgFontSize = 15,
  22. this.onPositivePressEvent,
  23. @required this.onCloseEvent,
  24. this.keybackEvent,
  25. this.minHeight = 60});
  26. @override
  27. State<StatefulWidget> createState() {
  28. return new _ConfirmDialogWidgetState();
  29. }
  30. }
  31. class _ConfirmDialogWidgetState extends State<ConfirmDialogWidget> {
  32. Widget getContent() {
  33. return Row(
  34. children: <Widget>[
  35. SizedBox(
  36. width: 16,
  37. ),
  38. Expanded(
  39. child: Center(
  40. child: new Text(
  41. widget.message,
  42. style: TextStyle(
  43. fontSize: widget.msgFontSize ?? 16.0,
  44. color: Color(0xff666666),
  45. ),
  46. maxLines: 2,
  47. softWrap: true,
  48. overflow: TextOverflow.ellipsis,
  49. ),
  50. ),
  51. flex: 1,
  52. ),
  53. SizedBox(
  54. width: 16,
  55. ),
  56. ],
  57. mainAxisAlignment: MainAxisAlignment.center,
  58. );
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. // TODO: implement build
  63. return WillPopScope(
  64. child: new Padding(
  65. padding: const EdgeInsets.only(left: 40.0, right: 40.0),
  66. child: new Material(
  67. type: MaterialType.transparency,
  68. child: new Column(
  69. mainAxisAlignment: MainAxisAlignment.center,
  70. children: <Widget>[
  71. new Container(
  72. decoration: ShapeDecoration(
  73. color: Color(0xffffffff),
  74. shape: RoundedRectangleBorder(
  75. borderRadius: BorderRadius.all(
  76. Radius.circular(widget.radius),
  77. ),
  78. ),
  79. ),
  80. // margin: const EdgeInsets.all(12.0),
  81. child: new Column(
  82. children: <Widget>[
  83. new Container(
  84. constraints:
  85. BoxConstraints(minHeight: widget.minHeight),
  86. child: new Padding(
  87. padding: const EdgeInsets.all(0.0),
  88. child: new Center(child: getContent()),
  89. ),
  90. ),
  91. getDivider(),
  92. this._buildOperationBar(),
  93. ],
  94. ),
  95. ),
  96. ],
  97. ),
  98. ),
  99. ),
  100. onWillPop: () async {
  101. if (widget.keybackEvent != null) {
  102. widget.keybackEvent();
  103. } else {
  104. Navigator.of(context).pop();
  105. }
  106. return false;
  107. });
  108. }
  109. Widget getDivider({double height = 1.0}) {
  110. return Container(
  111. color: Color(0xFFf0f0f0),
  112. height: height,
  113. );
  114. }
  115. Widget _buildOperationBar() {
  116. if (widget.onCloseEvent == null) {
  117. return Material(
  118. color: Colors.white,
  119. shape: RoundedRectangleBorder(
  120. borderRadius: BorderRadius.only(
  121. bottomLeft: Radius.circular(widget.radius),
  122. bottomRight: Radius.circular(widget.radius)),
  123. ),
  124. child: Container(
  125. alignment: Alignment.center,
  126. height: widget.barHeight,
  127. child: InkWell(
  128. child: Center(
  129. child: Text(
  130. widget.positiveText,
  131. style: TextStyle(
  132. color: Theme.of(context).primaryColor, fontSize: 16.0),
  133. ),
  134. ),
  135. onTap: () {
  136. widget.onPositivePressEvent("hhhhhh");
  137. },
  138. borderRadius: BorderRadius.only(
  139. bottomLeft: Radius.circular(widget.radius),
  140. bottomRight: Radius.circular(widget.radius),
  141. ),
  142. ),
  143. ));
  144. }
  145. return Row(
  146. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  147. children: <Widget>[
  148. new Expanded(
  149. flex: 1,
  150. child: _getNegativeWidget(),
  151. ),
  152. new Expanded(
  153. flex: 1,
  154. child: _getPositiveWidget(),
  155. ),
  156. ]);
  157. }
  158. Widget _getNegativeWidget() {
  159. return Material(
  160. color: Colors.white,
  161. shape: RoundedRectangleBorder(
  162. borderRadius:
  163. BorderRadius.only(bottomLeft: Radius.circular(widget.radius)),
  164. ),
  165. child: Container(
  166. alignment: Alignment.center,
  167. height: widget.barHeight,
  168. child: InkWell(
  169. child: Center(
  170. child: Text(widget.negativeText,
  171. style: TextStyle(color: Color(0xFF333333), fontSize: 16.0)),
  172. ),
  173. onTap: widget.onCloseEvent,
  174. borderRadius: BorderRadius.only(
  175. bottomLeft: Radius.circular(widget.radius))),
  176. ));
  177. }
  178. Widget _getPositiveWidget() {
  179. return Material(
  180. color: Theme.of(context).primaryColor,
  181. shape: RoundedRectangleBorder(
  182. borderRadius:
  183. BorderRadius.only(bottomRight: Radius.circular(widget.radius)),
  184. ),
  185. child: Container(
  186. alignment: Alignment.center,
  187. height: widget.barHeight,
  188. child: InkWell(
  189. child: Center(
  190. child: Text(widget.positiveText,
  191. style: TextStyle(color: Colors.white, fontSize: 16.0)),
  192. ),
  193. onTap: () {
  194. widget.onPositivePressEvent("hhhhhh");
  195. },
  196. borderRadius: BorderRadius.only(
  197. bottomRight: Radius.circular(widget.radius))),
  198. ));
  199. }
  200. @override
  201. void initState() {
  202. super.initState();
  203. }
  204. }
  205. // ignore: must_be_immutable
  206. class ConfirmDialog extends Dialog {
  207. String title;
  208. String message;
  209. String negativeText;
  210. String positiveText;
  211. double msgFontSize;
  212. Function onCloseEvent;
  213. Function onPositivePressEvent;
  214. Function keybackEvent;
  215. double minHeight;
  216. double barHeight = 48;
  217. double radius = 20;
  218. ConfirmDialog(
  219. {Key key,
  220. @required this.title,
  221. @required this.message,
  222. this.negativeText,
  223. this.positiveText,
  224. this.onPositivePressEvent,
  225. this.keybackEvent,
  226. this.msgFontSize,
  227. @required this.onCloseEvent,
  228. this.minHeight = 60})
  229. : assert(minHeight > 0),
  230. super(key: key);
  231. @override
  232. Widget build(BuildContext context) {
  233. return new ConfirmDialogWidget(
  234. title: title,
  235. message: message,
  236. negativeText: negativeText,
  237. positiveText: positiveText,
  238. msgFontSize: msgFontSize,
  239. onPositivePressEvent: onPositivePressEvent,
  240. onCloseEvent: onCloseEvent,
  241. keybackEvent: keybackEvent,
  242. minHeight: minHeight);
  243. }
  244. }