|
| 1 | +# Multi Trigger Autocomplete Plus |
| 2 | + |
| 3 | +[](https://opensource.org/licenses/MIT) [](https://github.com/xsahil03x/multi_trigger_autocomplete/blob/master/LICENSE) [](https://github.com/xsahil03x/multi_trigger_autocomplete/actions) [](https://codecov.io/gh/xsahil03x/multi_trigger_autocomplete) [](https://pub.dartlang.org/packages/multi_trigger_autocomplete) |
| 4 | + |
| 5 | +> This is a fork from [multi_trigger_autocomplete](https://pub.dev/packages/multi_trigger_autocomplete) |
| 6 | +
|
| 7 | +A flutter widget to add trigger based autocomplete functionality to your app. |
| 8 | + |
| 9 | +**Show some ❤️ and star the repo to support the project** |
| 10 | + |
| 11 | +<p> |
| 12 | + <img src="https://github.com/DenserMeerkat/multi_trigger_autocomplete_plus/blob/master/asset/package_demo.gif?raw=true" alt="An animated image of the MultiTriggerAutocomplete" height="400"/> |
| 13 | +</p> |
| 14 | + |
| 15 | +## Improvements |
| 16 | + |
| 17 | +This fork includes the following improvements over the original package: |
| 18 | + |
| 19 | +- **AutocompleteNoTrigger**: A Special trigger which allows allows autcomplete suggestions without a trigger character/string. |
| 20 | +- **Enhanced Customization**: Allows customization of `triggerEnd` instead of using a hardcoded space (`' '`). |
| 21 | +- **Prefix Triggers Handling**: Correctly identifies and handles triggers that share a common prefix, such as `{` and `{{`. |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +Add the following to your `pubspec.yaml` and replace `[version]` with the latest version: |
| 26 | + |
| 27 | +```yaml |
| 28 | +dependencies: |
| 29 | + multi_trigger_autocomplete_plus: ^[version] |
| 30 | +``` |
| 31 | +
|
| 32 | +## Usage |
| 33 | +
|
| 34 | +To use this package you must first wrap your top most widget |
| 35 | +with [Portal](https://pub.dev/documentation/flutter_portal/latest/flutter_portal/Portal-class.html) as this package |
| 36 | +uses [flutter_portal](https://pub.dev/packages/flutter_portal) |
| 37 | +to show the options view. |
| 38 | +
|
| 39 | +(Credits to: [Remi Rousselet](https://github.com/rrousselGit)) |
| 40 | +
|
| 41 | +> `Portal`, is the equivalent of [Overlay]. |
| 42 | +> |
| 43 | +> This widget will need to be inserted above the widget that needs to render |
| 44 | +> _under_ your overlays. |
| 45 | +> |
| 46 | +> If you want to display your overlays on the top of _everything_, a good place |
| 47 | +> to insert that `Portal` is above `MaterialApp`: |
| 48 | +> |
| 49 | +> ```dart |
| 50 | +> Portal( |
| 51 | +> child: MaterialApp( |
| 52 | +> ... |
| 53 | +> ) |
| 54 | +> ); |
| 55 | +> ``` |
| 56 | +> |
| 57 | +> (works for `CupertinoApp` too) |
| 58 | +> |
| 59 | +> This way `Portal` will render above everything. But you could place it |
| 60 | +> somewhere else to change the clip behavior. |
| 61 | + |
| 62 | +Import the package: |
| 63 | + |
| 64 | +```dart |
| 65 | +import 'package:multi_trigger_autocomplete_plus/multi_trigger_autocomplete.dart'; |
| 66 | +``` |
| 67 | + |
| 68 | +Use the widget: |
| 69 | + |
| 70 | +```dart |
| 71 | +MultiTriggerAutocomplete( |
| 72 | + optionsAlignment: OptionsAlignment.topStart, |
| 73 | + autocompleteTriggers: [ |
| 74 | + // Add the triggers you want to use for autocomplete |
| 75 | + AutocompleteTrigger( |
| 76 | + trigger: '@', |
| 77 | + optionsViewBuilder: (context, autocompleteQuery, controller) { |
| 78 | + return MentionAutocompleteOptions( |
| 79 | + query: autocompleteQuery.query, |
| 80 | + onMentionUserTap: (user) { |
| 81 | + final autocomplete = MultiTriggerAutocomplete.of(context); |
| 82 | + return autocomplete.acceptAutocompleteOption(user.id); |
| 83 | + }, |
| 84 | + ); |
| 85 | + }, |
| 86 | + ), |
| 87 | + AutocompleteTrigger( |
| 88 | + trigger: '#', |
| 89 | + optionsViewBuilder: (context, autocompleteQuery, controller) { |
| 90 | + return HashtagAutocompleteOptions( |
| 91 | + query: autocompleteQuery.query, |
| 92 | + onHashtagTap: (hashtag) { |
| 93 | + final autocomplete = MultiTriggerAutocomplete.of(context); |
| 94 | + return autocomplete |
| 95 | + .acceptAutocompleteOption(hashtag.name); |
| 96 | + }, |
| 97 | + ); |
| 98 | + }, |
| 99 | + ), |
| 100 | + AutocompleteTrigger( |
| 101 | + trigger: ':', |
| 102 | + optionsViewBuilder: (context, autocompleteQuery, controller) { |
| 103 | + return EmojiAutocompleteOptions( |
| 104 | + query: autocompleteQuery.query, |
| 105 | + onEmojiTap: (emoji) { |
| 106 | + final autocomplete = MultiTriggerAutocomplete.of(context); |
| 107 | + return autocomplete.acceptAutocompleteOption( |
| 108 | + emoji.char, |
| 109 | + // Passing false as we don't want the trigger [:] to |
| 110 | + // get prefixed to the option in case of emoji. |
| 111 | + keepTrigger: false, |
| 112 | + ); |
| 113 | + }, |
| 114 | + ); |
| 115 | + }, |
| 116 | + ), |
| 117 | + ], |
| 118 | + // Add the text field widget you want to use for autocomplete |
| 119 | + fieldViewBuilder: (context, controller, focusNode) { |
| 120 | + return Padding( |
| 121 | + padding: const EdgeInsets.all(8.0), |
| 122 | + child: ChatMessageTextField( |
| 123 | + focusNode: focusNode, |
| 124 | + controller: controller, |
| 125 | + ), |
| 126 | + ); |
| 127 | + }, |
| 128 | +), |
| 129 | +``` |
| 130 | + |
| 131 | +## Demo |
| 132 | + |
| 133 | +| Mention Autocomplete | Hashtag Autocomplete | Emoji Autocomplete | |
| 134 | +| ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 135 | +| <img src="https://github.com/DenserMeerkat/multi_trigger_autocomplete_plus/blob/master/asset/mention_demo.gif?raw=true" height="400" alt="Mention Autocomplete"/> | <img src="https://github.com/DenserMeerkat/multi_trigger_autocomplete_plus/blob/master/asset/hashtag_demo.gif?raw=true" height="400" alt="Hashtag Autocomplete"/> | <img src="https://github.com/DenserMeerkat/multi_trigger_autocomplete_plus/blob/master/asset/emoji_demo.gif?raw=true" height="400" alt="Emoji Autocomplete"/> | |
| 136 | + |
| 137 | +## Customization |
| 138 | + |
| 139 | +### MultiTriggerAutocomplete |
| 140 | + |
| 141 | +```dart |
| 142 | +MultiTriggerAutocomplete( |
| 143 | + // Defines the autocomplete trigger that will be used to match the |
| 144 | + // text. |
| 145 | + autocompleteTriggers: autocompleteTriggers, |
| 146 | +
|
| 147 | + // Defines the alignment of the options view relative to the |
| 148 | + // fieldView. |
| 149 | + // |
| 150 | + // By default, the options view is aligned to the bottom of the |
| 151 | + // fieldView. |
| 152 | + optionsAlignment: OptionsAlignment.topStart, |
| 153 | +
|
| 154 | + // Defines the width to make the options as a multiple of the width |
| 155 | + // of the fieldView. |
| 156 | + // |
| 157 | + // Setting this to 1 makes the options view width matches the width |
| 158 | + // of the fieldView. |
| 159 | + // |
| 160 | + // Use null to remove this constraint. |
| 161 | + optionsWidthFactor: 1.0, |
| 162 | +
|
| 163 | + // Defines the duration of the debounce period for the |
| 164 | + // [TextEditingController]. |
| 165 | + // |
| 166 | + // This is the time between the last character typed and the matching |
| 167 | + // is performed. |
| 168 | + debounceDuration: const Duration(milliseconds: 350), |
| 169 | +
|
| 170 | + // Defines the initial value to set in the internal |
| 171 | + // [TextEditingController]. |
| 172 | + // |
| 173 | + // This value will be ignored if [TextEditingController] is provided. |
| 174 | + initialValue: const TextEditingValue(text: 'Hello'), |
| 175 | +
|
| 176 | + // Defines the [TextEditingController] that will be used for the |
| 177 | + // fieldView. |
| 178 | + // |
| 179 | + // If this parameter is provided, then [focusNode] must also be |
| 180 | + // provided. |
| 181 | + textEditingController: TextEditingController(text: 'Hello'), |
| 182 | +
|
| 183 | + // Defines the [FocusNode] that will be used for the fieldView. |
| 184 | + // |
| 185 | + // If this parameter is provided, then [textEditingController] must |
| 186 | + // also be provided. |
| 187 | + focusNode: FocusNode(), |
| 188 | +
|
| 189 | + // Defines the fieldView that will be used to input the text. |
| 190 | + // |
| 191 | + // By default, a [TextFormField] is used. |
| 192 | + fieldViewBuilder: (context, controller, focusNode) { |
| 193 | + return TextField( |
| 194 | + controller: controller, |
| 195 | + focusNode: focusNode, |
| 196 | + ); |
| 197 | + }, |
| 198 | +), |
| 199 | +``` |
| 200 | + |
| 201 | +### AutocompleteTrigger |
| 202 | + |
| 203 | +```dart |
| 204 | +AutocompleteTrigger( |
| 205 | + // The trigger string/character that will be used to trigger the |
| 206 | + // autocomplete. |
| 207 | + trigger: '@', |
| 208 | +
|
| 209 | + // The trigger end string/character that will be used to determine |
| 210 | + // the end of the trigger. |
| 211 | + // By default, it's a space. |
| 212 | + triggerEnd: ' ', |
| 213 | +
|
| 214 | + // If true, the [trigger] should only be recognised at |
| 215 | + // the start of the input text. |
| 216 | + // |
| 217 | + // valid example: "@luke hello" |
| 218 | + // invalid example: "Hello @luke" |
| 219 | + triggerOnlyAtStart: false, |
| 220 | +
|
| 221 | + // If true, the [trigger] should only be recognised after |
| 222 | + // a space. |
| 223 | + // |
| 224 | + // valid example: "@luke", "Hello @luke" |
| 225 | + // invalid example: "Hello@luke" |
| 226 | + triggerOnlyAfterSpace: true, |
| 227 | +
|
| 228 | + // A minimum number of characters can be provided to only show |
| 229 | + // suggestions after the user has input enough characters. |
| 230 | + // |
| 231 | + // example: |
| 232 | + // "Hello @l" -> Shows zero suggestions. |
| 233 | + // "Hello @lu" -> Shows suggestions for @lu. |
| 234 | + minimumRequiredCharacters: 2, |
| 235 | +
|
| 236 | + // The options view builder is used to build the options view |
| 237 | + // that will be shown when the [trigger] is detected. |
| 238 | + optionsViewBuilder: (context, autocompleteQuery, controller) { |
| 239 | + return MentionAutocompleteOptions( |
| 240 | + query: autocompleteQuery.query, |
| 241 | + onMentionUserTap: (user) { |
| 242 | + // Accept the autocomplete option. |
| 243 | + final autocomplete = MultiTriggerAutocomplete.of(context); |
| 244 | + return autocomplete.acceptAutocompleteOption(user.id); |
| 245 | + }, |
| 246 | + ); |
| 247 | + }, |
| 248 | +) |
| 249 | +``` |
| 250 | + |
| 251 | +### AutocompleteNoTrigger |
| 252 | + |
| 253 | +Can be used to display autocomplete suggestions without requiring a trigger string or character. |
| 254 | + |
| 255 | +```dart |
| 256 | +AutocompleteNoTrigger( |
| 257 | + // A minimum number of characters can be provided to only show |
| 258 | + // suggestions after the user has input enough characters. |
| 259 | + minimumRequiredCharacters: 2, |
| 260 | +
|
| 261 | + // The options view builder is used to build the options view |
| 262 | + // that will be shown when the [trigger] is detected. |
| 263 | + optionsViewBuilder: (context, autocompleteQuery, controller) { |
| 264 | + return MentionAutocompleteOptions( |
| 265 | + query: autocompleteQuery.query, |
| 266 | + onMentionUserTap: (user) { |
| 267 | + // Accept the autocomplete option. |
| 268 | + final autocomplete = MultiTriggerAutocomplete.of(context); |
| 269 | + return autocomplete.acceptAutocompleteOption(user.id); |
| 270 | +
|
| 271 | + // Handle field unfocusing manually using a FocusNode |
| 272 | + focusNode.unfocus(); |
| 273 | + }, |
| 274 | + ); |
| 275 | + }, |
| 276 | +) |
| 277 | +``` |
| 278 | + |
| 279 | +## License |
| 280 | + |
| 281 | +[MIT License](LICENSE) |
0 commit comments