@@ -18,7 +18,6 @@ import 'selection_controls.dart';
1818import 'selection_painter.dart' ;
1919import 'selection_paragraph.dart' ;
2020import 'selections.dart' ;
21- import 'tagged_text.dart' ;
2221
2322part 'selectable_controller.dart' ;
2423
@@ -155,11 +154,11 @@ class _SelectableState extends State<Selectable>
155154 final sc = _selectionController! ;
156155
157156 if (sc.isTextSelected &&
158- _selections.main.isHidden != sc.getSelection ()! .isHidden) {
157+ ( _selections.main? .isHidden ?? false ) != sc.getSelection ()! .isHidden) {
159158 // ignore: avoid_positional_boolean_parameters
160159 // String bToStr(bool isHidden) => isHidden ? 'hidden' : 'visible';
161160 // dmPrint('Selection state changed from '
162- // '${bToStr(_selections.main.isHidden)} to '
161+ // '${bToStr(_selections.main? .isHidden ?? false )} to '
163162 // '${bToStr(sc.isHidden)}.');
164163 _selectionIsHidden = sc.getSelection ()! .isHidden;
165164 if (_selectionIsHidden) {
@@ -227,7 +226,8 @@ class _SelectableState extends State<Selectable>
227226 if (isScrolling != _buildHelper.isScrolling) {
228227 // dmPrint(isScrolling ? 'STARTED SCROLLING...' : 'STOPPED SCROLLING.');
229228 _buildHelper.isScrolling = isScrolling;
230- if (_selections.main.isTextSelected && _buildHelper.showPopupMenu) {
229+ if ((_selections.main? .isTextSelected ?? false ) &&
230+ _buildHelper.showPopupMenu) {
231231 _refresh ();
232232 }
233233 }
@@ -259,8 +259,8 @@ class _SelectableState extends State<Selectable>
259259
260260 // Ignore taps if text is not selected, because the child might want to
261261 // handle them.
262- final ignoreTap =
263- ! (widget.showSelectionControls && _selections.main.isTextSelected);
262+ final ignoreTap = ! (widget.showSelectionControls &&
263+ ( _selections.main? .isTextSelected ?? false ) );
264264
265265 // This is how the selection color is set in the Flutter 2.5.2
266266 // version of src/material/selectable_text.dart, except that
@@ -300,33 +300,33 @@ class _SelectableState extends State<Selectable>
300300 ? _selectionController? .getCustomPainter () ??
301301 DefaultSelectionPainter (
302302 color: selectionColor,
303- opacityAnimation:
304- _selectionIsHidden == _selections.main.isHidden
305- ? _selectionOpacityController
306- : _selections.main.isHidden
307- ? kAlwaysDismissedAnimation
308- : kAlwaysCompleteAnimation,
303+ opacityAnimation: _selectionIsHidden ==
304+ ( _selections.main? .isHidden ?? false )
305+ ? _selectionOpacityController
306+ : ( _selections.main? .isHidden ?? false )
307+ ? kAlwaysDismissedAnimation
308+ : kAlwaysCompleteAnimation,
309309 )
310310 : null ,
311311 child: IgnorePointer (
312312 // Ignore gestures (e.g. taps) on the child if text is selected.
313313 ignoring: widget.showSelectionControls &&
314314 (_selections.dragInfo.isSelectingWordOrDraggingHandle ||
315- _selections.main.isTextSelected),
315+ ( _selections.main? .isTextSelected ?? false ) ),
316316 child: widget.child,
317317 ),
318318 ),
319319 ),
320320 if (widget.showSelection &&
321321 (_selections.dragInfo.isSelectingWordOrDraggingHandle ||
322- _selections.main.isTextSelected ||
322+ ( _selections.main? .isTextSelected ?? false ) ||
323323 _buildHelper.showParagraphRects))
324324 Positioned .fill (
325325 child: LayoutBuilder (
326326 builder: (context, constraints) {
327327 // If text is selected, and a handle is being dragged,
328328 // autoscroll if necessary.
329- if (_selections.main.isTextSelected &&
329+ if (( _selections.main? .isTextSelected ?? false ) &&
330330 _selections.dragInfo.isDraggingHandle) {
331331 final paragraphs = _selections.cachedParagraphs.list;
332332 assert (paragraphs.isNotEmpty);
@@ -346,16 +346,17 @@ class _SelectableState extends State<Selectable>
346346 }
347347
348348 // dmPrint('selection.update resulted in '
349- // '${_selections.main.rects?.length ?? 0} selection rects');
349+ // '${_selections.main? .rects?.length ?? 0} selection rects');
350350 _selections.dragInfo
351351 ..selectionPt = null
352352 ..handleType = null ;
353353
354- if ((_selections.main.rects? .isNotEmpty ?? false ) ||
354+ if ((_selections.main? .rects? .isNotEmpty ?? false ) ||
355355 _buildHelper.showParagraphRects) {
356356 return AnimatedOpacity (
357- opacity: _selections.main.isHidden ? 0.0 : 1.0 ,
358- duration: _selections.main.animationDuration,
357+ opacity: (_selections.main? .isHidden ?? false ) ? 0.0 : 1.0 ,
358+ duration:
359+ (_selections.main? .animationDuration ?? Duration .zero),
359360 child: Stack (
360361 children: [
361362 // if (_selections.main.rects?.isNotEmpty ?? false)
@@ -416,8 +417,13 @@ class _SelectableState extends State<Selectable>
416417 if (! mounted) return ;
417418 final pt = localPosition;
418419 // dmPrint('onLongPressOrDoubleTap at: $pt');
419- if (pt != null && ! _selections.main.containsPoint (pt)) {
420+ if (pt != null && ! ( _selections.main? .containsPoint (pt) ?? false )) {
420421 _refresh (() {
422+ if (_selections.main == null ) {
423+ // Create the main selection object, if needed.
424+ _selections[0 ] =
425+ _selectionController? .getSelection () ?? const Selection ();
426+ }
421427 _buildHelper.showPopupMenu = widget.showPopup;
422428 _selections.dragInfo
423429 ..selectionPt = pt
@@ -431,15 +437,15 @@ class _SelectableState extends State<Selectable>
431437 if (! mounted) return ;
432438 final pt = localPosition;
433439 // dmPrint('onTap at: $pt');
434- if (pt != null && _selections.main.isTextSelected) {
440+ if (pt != null && ( _selections.main? .isTextSelected ?? false ) ) {
435441 _refresh (() {
436442 if (_buildHelper.usingCupertinoControls &&
437- _selections.main.containsPoint (pt)) {
443+ ( _selections.main? .containsPoint (pt) ?? false )) {
438444 if (widget.showPopup) {
439445 _buildHelper.showPopupMenu = ! _buildHelper.showPopupMenu;
440446 }
441- } else {
442- _selections[0 ] = _selections.main.cleared ();
447+ } else if (_selections.main != null ) {
448+ _selections[0 ] = _selections.main! .cleared ();
443449 }
444450 });
445451 }
0 commit comments