|
| 1 | +import 'package:example/text/my_special_text_span_builder.dart'; |
| 2 | +import 'package:extended_text/extended_text.dart'; |
| 3 | +import 'package:ff_annotation_route_library/ff_annotation_route_library.dart'; |
| 4 | +import 'package:flutter/cupertino.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +@FFRoute( |
| 8 | + name: 'fluttercandies://SelectableRegionWithTextFieldDemo', |
| 9 | + routeName: 'SelectableRegionWithTextField', |
| 10 | + description: 'SelectableRegion works with TextField', |
| 11 | +) |
| 12 | +class SelectableRegionWithTextFieldDemo extends StatefulWidget { |
| 13 | + const SelectableRegionWithTextFieldDemo({super.key}); |
| 14 | + |
| 15 | + @override |
| 16 | + State<SelectableRegionWithTextFieldDemo> createState() => |
| 17 | + _SelectableRegionWithTextFieldDemoState(); |
| 18 | +} |
| 19 | + |
| 20 | +class _SelectableRegionWithTextFieldDemoState |
| 21 | + extends State<SelectableRegionWithTextFieldDemo> { |
| 22 | + final SelectableRegionFocusNode _myFocusNode = SelectableRegionFocusNode(); |
| 23 | + |
| 24 | + final GlobalKey<SelectableRegionState> _key = |
| 25 | + GlobalKey<SelectableRegionState>(); |
| 26 | + |
| 27 | + @override |
| 28 | + Widget build(BuildContext context) { |
| 29 | + final TextSelectionControls controls = switch (Theme.of(context).platform) { |
| 30 | + TargetPlatform.android || |
| 31 | + TargetPlatform.fuchsia => |
| 32 | + materialTextSelectionHandleControls, |
| 33 | + TargetPlatform.linux || |
| 34 | + TargetPlatform.windows => |
| 35 | + desktopTextSelectionHandleControls, |
| 36 | + TargetPlatform.iOS => cupertinoTextSelectionHandleControls, |
| 37 | + TargetPlatform.macOS => cupertinoDesktopTextSelectionHandleControls, |
| 38 | + }; |
| 39 | + const String content = |
| 40 | + '中文 [love]Extended text help you to build rich text quickly. any special text you will have with extended text. ' |
| 41 | + 'It\'s my pleasure to invite you to join \$FlutterCandies\$ if you want to improve flutter .[love]' |
| 42 | + 'if you meet any problem, please let me know @zmtzawqlp .[sun_glasses]'; |
| 43 | + return Scaffold( |
| 44 | + appBar: AppBar( |
| 45 | + title: const Text('SelectionArea Support'), |
| 46 | + ), |
| 47 | + body: Container( |
| 48 | + alignment: Alignment.center, |
| 49 | + margin: const EdgeInsets.all(20.0), |
| 50 | + child: Column( |
| 51 | + children: <Widget>[ |
| 52 | + TextButton( |
| 53 | + onPressed: () { |
| 54 | + _myFocusNode.hideMenu(); |
| 55 | + }, |
| 56 | + child: const Text('hide menu'), |
| 57 | + ), |
| 58 | + SelectableRegion( |
| 59 | + key: _key, |
| 60 | + child: GestureDetector( |
| 61 | + child: ExtendedText( |
| 62 | + content, |
| 63 | + specialTextSpanBuilder: MySpecialTextSpanBuilder(), |
| 64 | + ), |
| 65 | + behavior: HitTestBehavior.translucent, |
| 66 | + onLongPress: () { |
| 67 | + _key.currentState?.selectAll(SelectionChangedCause.toolbar); |
| 68 | + }, |
| 69 | + ), |
| 70 | + focusNode: _myFocusNode, |
| 71 | + selectionControls: controls, |
| 72 | + contextMenuBuilder: (BuildContext context, |
| 73 | + SelectableRegionState selectableRegionState) { |
| 74 | + return AdaptiveTextSelectionToolbar.selectableRegion( |
| 75 | + selectableRegionState: selectableRegionState, |
| 76 | + ); |
| 77 | + }, |
| 78 | + ), |
| 79 | + const Spacer(), |
| 80 | + const TextField( |
| 81 | + maxLines: 1, |
| 82 | + style: TextStyle(height: 1), |
| 83 | + strutStyle: StrutStyle( |
| 84 | + height: 2.0, |
| 85 | + forceStrutHeight: true, |
| 86 | + ), |
| 87 | + ) |
| 88 | + ], |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// SelectableRegion don't want to hide menu when TextFiled get focus |
| 96 | +/// And when show menu, we don't want to make TextField lose focus |
| 97 | +/// so we need to override FocusNode |
| 98 | +class SelectableRegionFocusNode extends FocusNode { |
| 99 | + SelectableRegionFocusNode(); |
| 100 | + |
| 101 | + @override |
| 102 | + bool get hasFocus => false; |
| 103 | + |
| 104 | + @override |
| 105 | + bool get hasPrimaryFocus => false; |
| 106 | + |
| 107 | + @override |
| 108 | + void requestFocus([FocusNode? node]) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + void hideMenu() { |
| 113 | + notifyListeners(); |
| 114 | + } |
| 115 | +} |
0 commit comments