dialog.dart 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import 'package:flutter/material.dart';
  2. import '../utils/index.dart';
  3. class CustomDialog extends Dialog {
  4. final double width; // 宽度
  5. final double height; // 高度
  6. final String title; // 顶部标题
  7. final String content; // 内容
  8. final String cancelTxt; // 取消按钮的文本
  9. final String enterTxt; // 确认按钮的文本
  10. final Function callback; // 修改之后的回掉函数
  11. CustomDialog({
  12. this.width: 270,
  13. this.height: 141,
  14. this.title,
  15. this.content, // 根据content来,判断显示哪种类型
  16. this.cancelTxt: "取消",
  17. this.enterTxt: "确认",
  18. this.callback
  19. });
  20. @override
  21. Widget build(BuildContext context) {
  22. String _inputVal = "";
  23. return GestureDetector( // 点击遮罩层隐藏弹框
  24. child: Material(
  25. type: MaterialType.transparency, // 配置透明度
  26. child: Center(
  27. child: GestureDetector( // 点击遮罩层关闭弹框,并且点击非遮罩区域禁止关闭
  28. onTap: () {
  29. print('我是非遮罩区域~');
  30. },
  31. child: Container(
  32. width: this.width,
  33. height: this.height,
  34. decoration: BoxDecoration(
  35. borderRadius: BorderRadius.circular(10.px),
  36. color: Colors.white,
  37. ),
  38. child: Stack(
  39. alignment: Alignment.bottomCenter,
  40. children: <Widget>[
  41. Visibility(
  42. visible: this.content == null ? true : false,
  43. child: Positioned(
  44. top: 0,
  45. child: Container(
  46. alignment: Alignment.center,
  47. margin: EdgeInsets.fromLTRB(0, 19.px, 0, 19.px),
  48. child: Text(
  49. "${this.title}",
  50. style: TextStyle(
  51. fontSize: 17,
  52. color: Color(0xff000000),
  53. fontWeight: FontWeight.w600
  54. )
  55. )
  56. )
  57. )
  58. ),
  59. Container(
  60. padding: EdgeInsets.fromLTRB(15.px, 0, 15.px, 0),
  61. alignment: Alignment.center,
  62. child: this.content != null ?
  63. Container(
  64. width: double.infinity,
  65. margin: EdgeInsets.fromLTRB(0, 0, 0, 42.px),
  66. alignment: Alignment.center,
  67. child: Text(
  68. "${this.content}",
  69. style: TextStyle(
  70. color: Colors.black,
  71. fontSize: 17.px,
  72. fontWeight: FontWeight.w600
  73. )
  74. )
  75. )
  76. :
  77. TextField(
  78. textAlignVertical: TextAlignVertical.center,
  79. style: TextStyle(
  80. fontSize: 13
  81. ),
  82. textInputAction: TextInputAction.send,
  83. decoration: new InputDecoration(
  84. hintText: '${this.title}',
  85. contentPadding: const EdgeInsets.symmetric(vertical: 3.0, horizontal: 5.0),
  86. enabledBorder: OutlineInputBorder( // 边框默认色
  87. borderSide: const BorderSide(color: Color(0xffC8C7CC))
  88. ),
  89. focusedBorder: OutlineInputBorder( // 聚焦之后的边框色
  90. borderSide: const BorderSide(color: Color(0xfff3187D2))
  91. ),
  92. ),
  93. onChanged: (value) {
  94. _inputVal = value;
  95. }
  96. )
  97. ),
  98. Container(
  99. height: 43,
  100. decoration: BoxDecoration(
  101. border: Border(top: BorderSide(width: 1.px,color: Color(0xffEFEFF4)))
  102. ),
  103. child: Row(
  104. children: <Widget>[
  105. Expanded(
  106. flex: 1,
  107. child: GestureDetector(
  108. behavior: HitTestBehavior.opaque,
  109. child: Container(
  110. height: double.infinity,
  111. alignment: Alignment.center,
  112. decoration: BoxDecoration(
  113. border: Border(right: BorderSide(width: 1.px,color: Color(0xffEFEFF4)))
  114. ),
  115. child: Text(
  116. "${this.cancelTxt}",
  117. textAlign: TextAlign.center,
  118. style: TextStyle(
  119. color: Color(0xff007AFF),
  120. fontSize: 17.px,
  121. fontWeight: FontWeight.w400
  122. )
  123. )
  124. ),
  125. onTap: () {
  126. Navigator.pop(context);
  127. }
  128. )
  129. ),
  130. Expanded(
  131. flex: 1,
  132. child: GestureDetector(
  133. behavior: HitTestBehavior.opaque,
  134. child: Container(
  135. height: double.infinity, // 继承父级的高度
  136. alignment: Alignment.center,
  137. child: Text(
  138. "${this.enterTxt}",
  139. textAlign: TextAlign.center,
  140. style: TextStyle(
  141. color: Color(0xff007AFF),
  142. fontSize: 17.px,
  143. fontWeight: FontWeight.w600
  144. )
  145. )
  146. ),
  147. onTap: () {
  148. if(this.content != null) {
  149. this.callback(_inputVal); // 通过回掉函数传给父级
  150. }
  151. Navigator.pop(context); // 关闭dialog
  152. }
  153. )
  154. )
  155. ]
  156. )
  157. )
  158. ]
  159. )
  160. )
  161. )
  162. )
  163. ),
  164. onTap: () {
  165. Navigator.pop(context);
  166. }
  167. );
  168. }
  169. }