Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter GestureDetector: How to pinch in/out or zoom in/out Text using two fingers?

I'm creating a text field like Text or RichText. And after that, I want to zoom in/out the size of text using pinching. For now, I tried implementing GestureDetector but it zooms in/out with one finger too. And it is really hard to aim pinching detection. Sometimes is freezing. I added a video that shows when after pinching it freezes and suddenly get bigger. The second video is with the case that image zoom in only when I tap on the text with one finger and move to up left corner. The ideal implementation is to detect pinch and zoom in/out all text area. And disable zooming when I use only one finger. Could you send me some hints, link or code how to solve or where to find the solution?

body: GestureDetector(
  onScaleUpdate: (details) {
    setState(() {
      _textSize =
          _initTextSize + (_initTextSize * (details.scale * .35));
    });
  },
  onScaleEnd: (ScaleEndDetails details) {
    setState(() {
      _initTextSize = _textSize;
    });
  },
  child: Center(
      child: SizedBox(
    height: _textSize,
    child: FittedBox(
      child: Text("Test"),
    ),
  ))),
like image 705
mkubasz Avatar asked Mar 31 '19 10:03

mkubasz


3 Answers

In Stateful widget with these configuration

double _scaleFactor = 1.0;
double _baseScaleFactor = 1.0;

And use setState only on update, using the scaleFactor on textScaleFactor property of RichText.

Only one setState to rebuild widget and store the initial factor when scale starts

GestureDetector(
  onScaleStart: (details) {
    _baseScaleFactor = _scaleFactor;
  },
  onScaleUpdate: (details) {
    setState(() {
      _scaleFactor = _baseScaleFactor * details.scale;
    });
  },
  child: Container(
    height: MediaQuery.of(context).size.height,
    width: MediaQuery.of(context).size.width,
    color: Colors.red,
    child: Center(
      child: Text(
        'Test',
        textScaleFactor: _scaleFactor,
      ),
    ),
  ),
);

The height and width I put just to expand and simulate area of gesture detector.

like image 111
Renê Guilherme Nucci Avatar answered Nov 13 '22 23:11

Renê Guilherme Nucci


Google software engineers Gary Qian and Chris Yang demonstrated this in their Google Developer Days talk. The video is viewable here:

  • Text in Flutter: Building a fancy chat bubble at GDD China

There code is similar to some of the other answers here, but they notably add a clamp so that it doesn't get too big or small.

Here is a summary of their scalable text bubble:

enter image description here

Because scaling still gets called even for a single finger touch, I added a check for scaleUpdateDetails.scale == 1.0. That means the UI won't be updated if there was no change in scale.

class Bubble extends StatefulWidget {
  @override
  _BubbleState createState() => _BubbleState();
}

class _BubbleState extends State<Bubble> {
  double _fontSize = 20;
  final double _baseFontSize = 20;
  double _fontScale = 1;
  double _baseFontScale = 1;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onScaleStart: (ScaleStartDetails scaleStartDetails) {
        _baseFontScale = _fontScale;
      },
      onScaleUpdate: (ScaleUpdateDetails scaleUpdateDetails) {
        // don't update the UI if the scale didn't change
        if (scaleUpdateDetails.scale == 1.0) {
          return;
        }
        setState(() {
          _fontScale = (_baseFontScale * scaleUpdateDetails.scale).clamp(0.5, 5.0);
          _fontSize = _fontScale * _baseFontSize;
        });
      },
      child: ...
        // descendant with a Text widget that uses the _fontSize
    );
  }
}

Notes:

  • Use a StatefulWidget so that you can store the current font size and scale at all times
  • Use two additional variables to remember the original font size and also the scale at the start of the pinch
  • Wrap the Text widget in a GestureDetector
  • Save the original scale in onScaleStart
  • Calculate the new font size onScaleUpdate
  • Use setState to rebuild the widget with the new size
like image 34
Suragch Avatar answered Nov 14 '22 01:11

Suragch


Solution: Two finger zoom-in and zoom-out.

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

  class TransformText extends StatefulWidget {
    TransformText({Key key}) : super(key: key); // changed

    @override
    _TransformTextState createState() => _TransformTextState();
  }

  class _TransformTextState extends State<TransformText> {
    double scale = 0.0;

    @override
    Widget build(BuildContext context) {
      final ValueNotifier<Matrix4> notifier = ValueNotifier(Matrix4.identity());

      return Scaffold(
        appBar: AppBar(
          title: Text('Single finger Rotate text'), // changed
        ),
        body: Center(
          child: MatrixGestureDetector(
            onMatrixUpdate: (m, tm, sm, rm) {
              notifier.value = m;
            },
            child: AnimatedBuilder(
              animation: notifier,
              builder: (ctx, child) {
                return Transform(
                  transform: notifier.value,
                  child: Center(
                    child: Stack(
                      children: <Widget>[
                        Container(
                          color: Colors.red,
                          padding: EdgeInsets.all(10),
                          margin: EdgeInsets.only(top: 50),
                          child: Transform.scale(
                            scale:
                                1, // make this dynamic to change the scaling as in the basic demo
                            origin: Offset(0.0, 0.0),
                            child: Container(
                              height: 100,
                              child: Text(
                                "Two finger to zoom!!",
                                style:
                                    TextStyle(fontSize: 26, color: Colors.white),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ),
      );
    }
  }
like image 2
jazzbpn Avatar answered Nov 13 '22 23:11

jazzbpn