22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ import 'dart:math' ;
6+
57import 'package:flutter/widgets.dart' ;
68
79import 'button.dart' ;
@@ -103,6 +105,7 @@ class CupertinoTextSelectionToolbarButton extends StatefulWidget {
103105 return localizations.pasteButtonLabel;
104106 case ContextMenuButtonType .selectAll:
105107 return localizations.selectAllButtonLabel;
108+ case ContextMenuButtonType .liveTextInput:
106109 case ContextMenuButtonType .delete:
107110 case ContextMenuButtonType .custom:
108111 return '' ;
@@ -131,6 +134,7 @@ class _CupertinoTextSelectionToolbarButtonState extends State<CupertinoTextSelec
131134
132135 @override
133136 Widget build (BuildContext context) {
137+ final Widget content = _getContentWidget (context);
134138 final Widget child = CupertinoButton (
135139 color: isPressed
136140 ? _kToolbarPressedColor.resolveFrom (context)
@@ -145,15 +149,7 @@ class _CupertinoTextSelectionToolbarButtonState extends State<CupertinoTextSelec
145149 // There's no foreground fade on iOS toolbar anymore, just the background
146150 // is darkened.
147151 pressedOpacity: 1.0 ,
148- child: widget.child ?? Text (
149- widget.text ?? CupertinoTextSelectionToolbarButton .getButtonLabel (context, widget.buttonItem! ),
150- overflow: TextOverflow .ellipsis,
151- style: _kToolbarButtonFontStyle.copyWith (
152- color: widget.onPressed != null
153- ? _kToolbarTextColor.resolveFrom (context)
154- : CupertinoColors .inactiveGray,
155- ),
156- ),
152+ child: content,
157153 );
158154
159155 if (widget.onPressed != null ) {
@@ -170,4 +166,85 @@ class _CupertinoTextSelectionToolbarButtonState extends State<CupertinoTextSelec
170166 return child;
171167 }
172168 }
169+
170+ Widget _getContentWidget (BuildContext context) {
171+ if (widget.child != null ) {
172+ return widget.child! ;
173+ }
174+ final Widget textWidget = Text (
175+ widget.text ?? CupertinoTextSelectionToolbarButton .getButtonLabel (context, widget.buttonItem! ),
176+ overflow: TextOverflow .ellipsis,
177+ style: _kToolbarButtonFontStyle.copyWith (
178+ color: widget.onPressed != null
179+ ? _kToolbarTextColor.resolveFrom (context)
180+ : CupertinoColors .inactiveGray,
181+ ),
182+ );
183+ if (widget.buttonItem == null ) {
184+ return textWidget;
185+ }
186+ switch (widget.buttonItem! .type) {
187+ case ContextMenuButtonType .cut:
188+ case ContextMenuButtonType .copy:
189+ case ContextMenuButtonType .paste:
190+ case ContextMenuButtonType .selectAll:
191+ case ContextMenuButtonType .delete:
192+ case ContextMenuButtonType .custom:
193+ return textWidget;
194+ case ContextMenuButtonType .liveTextInput:
195+ return SizedBox (
196+ width: 13.0 ,
197+ height: 13.0 ,
198+ child: CustomPaint (
199+ painter: _LiveTextIconPainter (color: _kToolbarTextColor.resolveFrom (context)),
200+ ),
201+ );
202+ }
203+ }
204+ }
205+
206+ class _LiveTextIconPainter extends CustomPainter {
207+ _LiveTextIconPainter ({required this .color});
208+
209+ final Color color;
210+
211+ final Paint _painter = Paint ()
212+ ..strokeCap = StrokeCap .round
213+ ..strokeJoin = StrokeJoin .round
214+ ..strokeWidth = 1.0
215+ ..style = PaintingStyle .stroke;
216+
217+ @override
218+ void paint (Canvas canvas, Size size) {
219+ _painter.color = color;
220+ canvas.save ();
221+ canvas.translate (size.width / 2.0 , size.height / 2.0 );
222+
223+ final Offset origin = Offset (- size.width / 2.0 , - size.height / 2.0 );
224+ // Path for the one corner.
225+ final Path path = Path ()
226+ ..moveTo (origin.dx, origin.dy + 3.5 )
227+ ..lineTo (origin.dx, origin.dy + 1.0 )
228+ ..arcToPoint (Offset (origin.dx + 1.0 , origin.dy), radius: const Radius .circular (1 ))
229+ ..lineTo (origin.dx + 3.5 , origin.dy);
230+
231+ // Rotate to draw corner four times.
232+ final Matrix4 rotationMatrix = Matrix4 .identity ()..rotateZ (pi / 2.0 );
233+ for (int i = 0 ; i < 4 ; i += 1 ) {
234+ canvas.drawPath (path, _painter);
235+ canvas.transform (rotationMatrix.storage);
236+ }
237+
238+ // Draw three lines.
239+ canvas.drawLine (const Offset (- 3.0 , - 3.0 ), const Offset (3.0 , - 3.0 ), _painter);
240+ canvas.drawLine (const Offset (- 3.0 , 0.0 ), const Offset (3.0 , 0.0 ), _painter);
241+ canvas.drawLine (const Offset (- 3.0 , 3.0 ), const Offset (1.0 , 3.0 ), _painter);
242+
243+ canvas.restore ();
244+ }
245+
246+ @override
247+ bool shouldRepaint (covariant _LiveTextIconPainter oldDelegate) {
248+ return oldDelegate.color != color;
249+ }
173250}
0 commit comments