如果Android原生开发让搞个阴影,那就把UI拉出去暴打一顿吧。当然,搞个带阴影的背景切图也是能勉强接受的。
    用了Flutter之后发现写带阴影控件简直不要太简单,妈妈再也不用担心我画不出来好看的阴影了。
    先上图: Flutter控件---超简单的模糊阴影实现 - 图1

    这次分享的例子是用Container的decoration属性实现的,当然你也可以使用Material家族的MaterialButton等,但是隐形的位置不可控。如上图第五个用的就是官方MaterialButton,阴影主要就是在下方,如果想改上下左右的阴影大小颜色等, 就没法实现了。不废话了,直接上代码:
    第一个圆形button:

    1. Widget _circleButton() {
    2. return Container(
    3. margin: EdgeInsets.only(left: 20),
    4. decoration: BoxDecoration(shape: BoxShape.circle, boxShadow: [
    5. ///阴影颜色/位置/大小等
    6. BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),
    7. ///模糊阴影半径
    8. blurRadius: 5,
    9. ),
    10. BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 5),
    11. BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 5),
    12. BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 5)
    13. ]),
    14. child: CircleAvatar(
    15. radius: 20,
    16. backgroundColor: Colors.white,
    17. child: Column(
    18. mainAxisAlignment: MainAxisAlignment.center,
    19. children: <Widget>[
    20. Icon(
    21. Icons.launch,
    22. color: Colors.black87,
    23. size: 20,
    24. ),
    25. Text(
    26. "分享",
    27. style: TextStyle(
    28. color: Colors.grey[400],
    29. fontSize: 10,
    30. ),
    31. )
    32. ],
    33. ),
    34. ),
    35. );
    36. }
    37. Widget _rectangleButton() {
    38. return Container(
    39. margin: EdgeInsets.only(top: 20, left: 20),
    40. decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
    41. BoxShadow(color: Colors.grey[300],offset: Offset(1, 1),blurRadius: 5,),
    42. BoxShadow(color: Colors.grey[300], offset: Offset(-1, -1), blurRadius: 5),
    43. BoxShadow(color: Colors.grey[300], offset: Offset(1, -1), blurRadius: 5),
    44. BoxShadow(color: Colors.grey[300], offset: Offset(-1, 1), blurRadius: 5)
    45. ]),
    46. child: FlatButton(
    47. onPressed: () {},
    48. color: Colors.white,
    49. padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
    50. child: Column(
    51. mainAxisAlignment: MainAxisAlignment.center,
    52. children: <Widget>[
    53. Icon(
    54. Icons.launch,
    55. color: Colors.black87,
    56. size: 20,
    57. ),
    58. Text(
    59. "分享",
    60. style: TextStyle(
    61. color: Colors.grey[400],
    62. fontSize: 10,
    63. ),
    64. )
    65. ],
    66. )),
    67. );
    68. }
    69. Widget _flatButton1() {
    70. return Container(
    71. margin: EdgeInsets.only(top: 20, left: 20),
    72. decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
    73. //BoxShadow中Offset (1,1)右下,(-1,-1)左上,(1,-1)右上,(-1,1)左下
    74. BoxShadow(
    75. color: Colors.red,
    76. offset: Offset(1, 1),
    77. blurRadius: 5,
    78. ),
    79. BoxShadow(
    80. color: Colors.blueAccent, offset: Offset(-1, -1), blurRadius: 5),
    81. ]),
    82. child: FlatButton(
    83. onPressed: () {},
    84. color: Colors.white,
    85. padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
    86. child: Column(
    87. mainAxisAlignment: MainAxisAlignment.center,
    88. children: <Widget>[
    89. Icon(
    90. Icons.launch,
    91. color: Colors.black87,
    92. size: 20,
    93. ),
    94. Text(
    95. "分享",
    96. style: TextStyle(
    97. color: Colors.grey[400],
    98. fontSize: 10,
    99. ),
    100. )
    101. ],
    102. )),
    103. );
    104. }
    105. Widget _flatButton2() {
    106. return Container(
    107. margin: EdgeInsets.only(top: 20, left: 20),
    108. decoration: BoxDecoration(shape: BoxShape.rectangle, boxShadow: [
    109. //BoxShadow中Offset (1,1)右下,(-1,-1)左上,(1,-1)右上,(-1,1)左下
    110. BoxShadow(
    111. color: Colors.red,
    112. offset: Offset(-1, 1),
    113. blurRadius: 5,
    114. ),
    115. BoxShadow(color: Colors.blueAccent, offset: Offset(1, -1), blurRadius: 5),
    116. ]),
    117. child: FlatButton(
    118. onPressed: () {},
    119. color: Colors.white,
    120. padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
    121. child: Column(
    122. mainAxisAlignment: MainAxisAlignment.center,
    123. children: <Widget>[
    124. Icon(
    125. Icons.launch,
    126. color: Colors.black87,
    127. size: 20,
    128. ),
    129. Text(
    130. "分享",
    131. style: TextStyle(
    132. color: Colors.grey[400],
    133. fontSize: 10,
    134. ),
    135. )
    136. ],
    137. )),
    138. );
    139. }
    140. Widget _materialButton() {
    141. return Padding(
    142. padding: EdgeInsets.only(top: 20, left: 20),
    143. child: MaterialButton(
    144. padding: EdgeInsets.only(left: 30, right: 30, top: 10, bottom: 10),
    145. color: Colors.white,
    146. onPressed: () {},
    147. elevation: 5,
    148. child: Column(
    149. mainAxisAlignment: MainAxisAlignment.center,
    150. children: <Widget>[
    151. Icon(
    152. Icons.launch,
    153. color: Colors.black87,
    154. size: 20,
    155. ),
    156. Text(
    157. "分享",
    158. style: TextStyle(
    159. color: Colors.grey[400],
    160. fontSize: 10,
    161. ),
    162. )
    163. ],
    164. ),
    165. ),
    166. );
    167. }

    decoration的boxShadow就是一系列的阴影组件,根据坐标位置和大小,颜色以及阴影模糊半径,想怎么实现就怎么实现,阴影实现就是这么简单。