Skip to content

Commit

Permalink
add magnifier update methods in btf
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder committed Dec 24, 2021
1 parent cbd0bdf commit 439f073
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
2 changes: 2 additions & 0 deletions lib/constants/global_singleton.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:better_textfield/services/magnifier_service.dart';
import 'package:flutter/material.dart';

final MagnifierService magnifierService = MagnifierService();
final GlobalKey globalKey = GlobalKey();
30 changes: 30 additions & 0 deletions lib/controllers/magnifier_controller.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:better_textfield/constants/global_singleton.dart' as globals;
import 'package:better_textfield/services/magnifier_service.dart';
import 'package:flutter/material.dart';
Expand All @@ -13,9 +15,19 @@ class MagnifierController extends ChangeNotifier {
double get textFieldHeight => _magnifierService.textFieldHeight;
Size get size => _magnifierService.size;
BorderRadius get radius => _magnifierService.radius;
Timer? _timer;

set enabled(bool enabled) {
_magnifierService.enabled = enabled;
if (enabled) {
if (_timer?.isActive ?? false) {
_timer?.cancel();
}
_timer = Timer(const Duration(milliseconds: 1000), () {
_magnifierService.enabled = false;
notifyListeners();
});
}
notifyListeners();
}

Expand All @@ -38,4 +50,22 @@ class MagnifierController extends ChangeNotifier {
_magnifierService.radius = radius;
notifyListeners();
}

void toggleVisbility() {
_magnifierService.enabled = !_magnifierService.enabled;
notifyListeners();
}

void updatePosition(int baseOffset) {
final keyContext = globals.globalKey.currentContext;
if (keyContext != null) {
// widget is visible
final box = keyContext.findRenderObject() as RenderBox;
_magnifierService.textFieldHeight = box.size.height;
_magnifierService.magnifierPosition = box.localToGlobal(Offset(
0 - _magnifierService.size.width / 2 + baseOffset * 9.7,
0 - box.size.height));
notifyListeners();
}
}
}
21 changes: 2 additions & 19 deletions lib/views/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:developer';

import 'package:better_textfield/constants/global_singleton.dart';
import 'package:better_textfield/widgets/b_text_field.dart' as btf;
import 'package:better_textfield/widgets/b_text_selection_controls.dart';
import 'package:better_textfield/widgets/magnifier.dart';
Expand All @@ -14,30 +15,12 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
GlobalKey stickyKey = GlobalKey();
late TextEditingController _textController;
Offset pos = Offset.zero;
double height = 0;

@override
void initState() {
super.initState();
_textController = TextEditingController();
_textController.addListener(() {
log('Text changed: ${_textController.selection.baseOffset}');
final keyContext = stickyKey.currentContext;
if (keyContext != null) {
// widget is visible
final box = keyContext.findRenderObject() as RenderBox;
setState(() {
pos = box.localToGlobal(Offset(
0 - 110 / 2 + (_textController.selection.baseOffset * 9.7),
0 - box.size.height));
height = box.size.height;
});
log(pos.toString());
}
});
}

@override
Expand Down Expand Up @@ -97,7 +80,7 @@ class _HomePageState extends State<HomePage> {
color: Colors.black.withOpacity(0.1),
borderRadius: BorderRadius.circular(8)),
child: btf.TextField(
key: stickyKey,
key: globalKey,
selectionControls: BetterTextSelectionControls(),
controller: _textController,
),
Expand Down
28 changes: 17 additions & 11 deletions lib/widgets/b_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle;

import 'package:better_textfield/controllers/magnifier_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

export 'package:flutter/services.dart'
show
Expand Down Expand Up @@ -1098,12 +1100,13 @@ class _TextFieldState extends State<TextField>
// }
// }

@override
String? get restorationId => widget.restorationId;
// @override
// String? get restorationId => widget.restorationId;

@override
void dispose() {
_effectiveFocusNode.removeListener(_handleFocusChanged);
context.read<MagnifierController>().enabled = false;
_focusNode?.dispose();
// _controller?.dispose();
super.dispose();
Expand All @@ -1118,6 +1121,7 @@ class _TextFieldState extends State<TextField>
bool _shouldShowSelectionHandles(SelectionChangedCause? cause) {
// When the text field is activated by something that doesn't trigger the
// selection overlay, we shouldn't show the handles either.
if (cause == SelectionChangedCause.drag) return true;
if (!_selectionGestureDetectorBuilder.shouldShowSelectionToolbar)
return false;

Expand All @@ -1132,7 +1136,7 @@ class _TextFieldState extends State<TextField>

if (_effectiveController.text.isNotEmpty) return true;

return false;
return true;
}

void _handleFocusChanged() {
Expand All @@ -1144,7 +1148,15 @@ class _TextFieldState extends State<TextField>

void _handleSelectionChanged(
TextSelection selection, SelectionChangedCause? cause) {
context
.read<MagnifierController>()
.updatePosition(_effectiveController.selection.baseOffset);
final bool willShowSelectionHandles = _shouldShowSelectionHandles(cause);
if (willShowSelectionHandles) {
context.read<MagnifierController>().enabled = true;
} else {
context.read<MagnifierController>().enabled = false;
}
if (willShowSelectionHandles != _showSelectionHandles) {
setState(() {
_showSelectionHandles = willShowSelectionHandles;
Expand Down Expand Up @@ -1340,18 +1352,12 @@ class _TextFieldState extends State<TextField>
selectionColor: focusNode.hasFocus ? selectionColor : null,
selectionControls:
widget.selectionEnabled ? textSelectionControls : null,
onChanged: (_) {
//TODO:: update magnifier position when text changes
if (widget.onChanged != null) widget.onChanged!(_);
},
onChanged: widget.onChanged,
onSelectionChanged: _handleSelectionChanged,
onEditingComplete: widget.onEditingComplete,
onSubmitted: widget.onSubmitted,
onAppPrivateCommand: widget.onAppPrivateCommand,
onSelectionHandleTapped: () {
//TODO:: toggle text magnifier
_handleSelectionHandleTapped();
},
onSelectionHandleTapped: _handleSelectionHandleTapped,
inputFormatters: formatters,
rendererIgnoresPointer: true,
mouseCursor: MouseCursor.defer, // TextField will handle the cursor
Expand Down

0 comments on commit 439f073

Please sign in to comment.