|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/foundation.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | + |
| 8 | +/// Flutter code sample for [SelectionArea]. |
| 9 | +
|
| 10 | +void main() => runApp(const SelectionAreaSelectionListenerExampleApp()); |
| 11 | + |
| 12 | +class SelectionAreaSelectionListenerExampleApp extends StatelessWidget { |
| 13 | + const SelectionAreaSelectionListenerExampleApp({super.key}); |
| 14 | + |
| 15 | + @override |
| 16 | + Widget build(BuildContext context) { |
| 17 | + return MaterialApp( |
| 18 | + title: 'Flutter Demo', |
| 19 | + theme: ThemeData( |
| 20 | + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
| 21 | + useMaterial3: true, |
| 22 | + ), |
| 23 | + home: const MyHomePage(title: 'Flutter Demo Home Page'), |
| 24 | + ); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +class MyHomePage extends StatefulWidget { |
| 29 | + const MyHomePage({super.key, required this.title}); |
| 30 | + |
| 31 | + final String title; |
| 32 | + |
| 33 | + @override |
| 34 | + State<MyHomePage> createState() => _MyHomePageState(); |
| 35 | +} |
| 36 | + |
| 37 | +class _MyHomePageState extends State<MyHomePage> { |
| 38 | + final SelectionListenerNotifier _selectionNotifier = SelectionListenerNotifier(); |
| 39 | + SelectableRegionSelectionStatus? _selectableRegionStatus; |
| 40 | + |
| 41 | + void _handleOnSelectionStateChanged(SelectableRegionSelectionStatus status) { |
| 42 | + setState(() { |
| 43 | + _selectableRegionStatus = status; |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + void dispose() { |
| 49 | + _selectionNotifier.dispose(); |
| 50 | + _selectableRegionStatus = null; |
| 51 | + super.dispose(); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + Widget build(BuildContext context) { |
| 56 | + return Scaffold( |
| 57 | + appBar: AppBar( |
| 58 | + backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
| 59 | + title: Text(widget.title), |
| 60 | + ), |
| 61 | + body: Center( |
| 62 | + child: Column( |
| 63 | + children: <Widget>[ |
| 64 | + Column( |
| 65 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 66 | + children: <Widget>[ |
| 67 | + for (final (int? offset, String label) in <(int? offset, String label)>[ |
| 68 | + (_selectionNotifier.registered ? _selectionNotifier.selection.range?.startOffset : null, 'StartOffset'), |
| 69 | + (_selectionNotifier.registered ? _selectionNotifier.selection.range?.endOffset : null, 'EndOffset'), |
| 70 | + ]) |
| 71 | + Text('Selection $label: $offset'), |
| 72 | + Text('Selection Status: ${_selectionNotifier.registered ? _selectionNotifier.selection.status : 'SelectionListenerNotifier not registered.'}'), |
| 73 | + Text('Selectable Region Status: $_selectableRegionStatus'), |
| 74 | + ], |
| 75 | + ), |
| 76 | + const SizedBox(height: 15.0,), |
| 77 | + SelectionArea( |
| 78 | + child: MySelectableText( |
| 79 | + selectionNotifier: _selectionNotifier, |
| 80 | + onChanged: _handleOnSelectionStateChanged, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ], |
| 84 | + ), |
| 85 | + ), |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class MySelectableText extends StatefulWidget { |
| 91 | + const MySelectableText({ |
| 92 | + super.key, |
| 93 | + required this.selectionNotifier, |
| 94 | + required this.onChanged, |
| 95 | + }); |
| 96 | + |
| 97 | + final SelectionListenerNotifier selectionNotifier; |
| 98 | + final ValueChanged<SelectableRegionSelectionStatus> onChanged; |
| 99 | + |
| 100 | + @override |
| 101 | + State<MySelectableText> createState() => _MySelectableTextState(); |
| 102 | +} |
| 103 | + |
| 104 | +class _MySelectableTextState extends State<MySelectableText> { |
| 105 | + ValueListenable<SelectableRegionSelectionStatus>? _selectableRegionScope; |
| 106 | + |
| 107 | + void _handleOnSelectableRegionChanged() { |
| 108 | + if (_selectableRegionScope == null) { |
| 109 | + return; |
| 110 | + } |
| 111 | + widget.onChanged.call(_selectableRegionScope!.value); |
| 112 | + } |
| 113 | + |
| 114 | + @override |
| 115 | + void didChangeDependencies() { |
| 116 | + super.didChangeDependencies(); |
| 117 | + _selectableRegionScope?.removeListener(_handleOnSelectableRegionChanged); |
| 118 | + _selectableRegionScope = SelectableRegionSelectionStatusScope.maybeOf(context); |
| 119 | + _selectableRegionScope?.addListener(_handleOnSelectableRegionChanged); |
| 120 | + } |
| 121 | + |
| 122 | + @override |
| 123 | + void dispose() { |
| 124 | + _selectableRegionScope?.removeListener(_handleOnSelectableRegionChanged); |
| 125 | + _selectableRegionScope = null; |
| 126 | + super.dispose(); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Widget build(BuildContext context) { |
| 131 | + return SelectionListener( |
| 132 | + selectionNotifier: widget.selectionNotifier, |
| 133 | + child: const Text('This is some text under a SelectionArea that can be selected.'), |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments