Hibok
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

161 rader
5.2 KiB

  1. import 'package:chat/generated/i18n.dart';
  2. import 'package:flutter/material.dart';
  3. /// A type for the authorization button.
  4. enum ButtonType { defaultButton, continueButton, signIn }
  5. /// A style for the authorization button.
  6. enum ButtonStyle { black, whiteOutline, white }
  7. /// A button for Sign in With Apple
  8. class AppleSignInBtn extends StatefulWidget {
  9. /// The callback that is called when the button is tapped or otherwise activated.
  10. ///
  11. /// If this is set to null, the button will be disabled.
  12. final VoidCallback onPressed;
  13. /// A type for the authorization button.
  14. final ButtonType type;
  15. /// A style for the authorization button.
  16. final ButtonStyle style;
  17. /// A custom corner radius to be used by this button.
  18. final double cornerRadius;
  19. const AppleSignInBtn({
  20. this.onPressed,
  21. this.type = ButtonType.defaultButton,
  22. this.style = ButtonStyle.white,
  23. this.cornerRadius = 20,
  24. }) : assert(type != null),
  25. assert(style != null),
  26. assert(cornerRadius != null);
  27. @override
  28. State<StatefulWidget> createState() => _AppleSignInBtnState();
  29. }
  30. class _AppleSignInBtnState extends State<AppleSignInBtn> {
  31. bool _isTapDown = false;
  32. @override
  33. Widget build(BuildContext context) {
  34. final bgColor =
  35. widget.style == ButtonStyle.black ? Colors.black : Colors.white;
  36. final textColor =
  37. widget.style == ButtonStyle.black ? Colors.white : Colors.black;
  38. final borderColor =
  39. widget.style == ButtonStyle.white ? Colors.white : Colors.black;
  40. return GestureDetector(
  41. onTapDown: (_) => setState(() => _isTapDown = true),
  42. onTapUp: (_) {
  43. setState(() => _isTapDown = false);
  44. widget?.onPressed();
  45. },
  46. onTapCancel: () => setState(() => _isTapDown = false),
  47. child: AnimatedContainer(
  48. duration: Duration(milliseconds: 100),
  49. constraints: BoxConstraints(
  50. minHeight: 32,
  51. maxHeight: 64,
  52. minWidth: 150,
  53. ),
  54. height: 20,
  55. decoration: BoxDecoration(
  56. color: _isTapDown ? Colors.grey : bgColor,
  57. borderRadius: BorderRadius.all(
  58. Radius.circular(widget.cornerRadius),
  59. ),
  60. border: Border.all(width: .7, color: borderColor),
  61. ),
  62. child: Center(
  63. child: Row(
  64. mainAxisSize: MainAxisSize.min,
  65. children: <Widget>[
  66. Padding(
  67. padding: const EdgeInsets.only(bottom: 1 , right: 6),
  68. child: SizedBox(
  69. height: 14,
  70. child: AspectRatio(
  71. aspectRatio: 25 / 31,
  72. child: CustomPaint(
  73. painter: _AppleLogoPainter(color: textColor),
  74. ),
  75. ),
  76. ),
  77. ),
  78. Text(
  79. widget.type == ButtonType.continueButton
  80. ? 'Continue with Apple'
  81. : I18n.of(context).sign_in_apple,
  82. style: TextStyle(
  83. fontSize: 12,
  84. fontWeight: FontWeight.w500,
  85. letterSpacing: .3,
  86. wordSpacing: -.5,
  87. color: textColor,
  88. ),
  89. ),
  90. ],
  91. )),
  92. ),
  93. );
  94. }
  95. }
  96. class _AppleLogoPainter extends CustomPainter {
  97. final Color color;
  98. _AppleLogoPainter({@required this.color});
  99. @override
  100. void paint(Canvas canvas, Size size) {
  101. final paint = Paint()..color = color;
  102. canvas.drawPath(_getApplePath(size.width, size.height), paint);
  103. }
  104. static Path _getApplePath(double w, double h) {
  105. return Path()
  106. ..moveTo(w * .50779, h * .28732)
  107. ..cubicTo(
  108. w * .4593, h * .28732, w * .38424, h * .24241, w * .30519, h * .24404)
  109. ..cubicTo(
  110. w * .2009, h * .24512, w * .10525, h * .29328, w * .05145, h * .36957)
  111. ..cubicTo(w * -.05683, h * .5227, w * .02355, h * .74888, w * .12916,
  112. h * .87333)
  113. ..cubicTo(w * .18097, h * .93394, w * .24209, h * 1.00211, w * .32313,
  114. h * .99995)
  115. ..cubicTo(w * .40084, h * .99724, w * .43007, h * .95883, w * .52439,
  116. h * .95883)
  117. ..cubicTo(w * .61805, h * .95883, w * .64462, h * .99995, w * .72699,
  118. h * .99833)
  119. ..cubicTo(
  120. w * .81069, h * .99724, w * .86383, h * .93664, w * .91498, h * .8755)
  121. ..cubicTo(
  122. w * .97409, h * .80515, w * .99867, h * .73698, w * 1, h * .73319)
  123. ..cubicTo(w * .99801, h * .73265, w * .83726, h * .68233, w * .83526,
  124. h * .53082)
  125. ..cubicTo(
  126. w * .83394, h * .4042, w * .96214, h * .3436, w * .96812, h * .34089)
  127. ..cubicTo(
  128. w * .89505, h * .25378, w * .78279, h * .24404, w * .7436, h * .24187)
  129. ..cubicTo(
  130. w * .6413, h * .23538, w * .55561, h * .28732, w * .50779, h * .28732)
  131. ..close()
  132. ..moveTo(w * .68049, h * .15962)
  133. ..cubicTo(w * .72367, h * .11742, w * .75223, h * .05844, w * .74426, 0)
  134. ..cubicTo(w * .68249, h * .00216, w * .60809, h * .03355, w * .56359,
  135. h * .07575)
  136. ..cubicTo(w * .52373, h * .11309, w * .48919, h * .17315, w * .49849,
  137. h * .23051)
  138. ..cubicTo(w * .56691, h * .23484, w * .63732, h * .20183, w * .68049,
  139. h * .15962)
  140. ..close();
  141. }
  142. @override
  143. bool shouldRepaint(CustomPainter oldDelegate) => false;
  144. }