dialog.dart 8.9 KB

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