Michael Amir
Michael Amir

Reputation: 255

Flutter GestureDetector isn't working at all

I am trying to make a Text widget zoomable with GestureDetector but it isn't working at all and i get no error even...

Note that i tried many things like wrapping the scaffold itself with the GestureDetector..

main.dart

import 'package:flutter/material.dart';
import 'package:testy/zoomable.dart';

void main() => runApp(
  MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Testy',
    theme: ThemeData(
      primarySwatch: Colors.green,
      primaryColor: Colors.green
    ),
    home: Zoomable(),
  )
);

zoomable.dart

import 'package:flutter/material.dart';

class Zoomable extends StatefulWidget {
  @override
  _ZoomableState createState() => _ZoomableState();
}

class _ZoomableState extends State<Zoomable> {
  @override
  Widget build(BuildContext context) {
    double size = 70;
    return Scaffold(
      appBar: AppBar(
        title: Text('Zoomable'),
      ),
      body: GestureDetector(
        onScaleStart: (details) {},
        onScaleUpdate: (ScaleUpdateDetails details) {
          if (size < 150.0 && size > 50.0) {
            setState(() {
              size = size + details.scale;
            });
          }
        },
        onTap: () {
          if (size < 150.0 && size > 50.0) {
            setState(() {
              size = size + 1;
            });
          }
        },
        child: Text(
          'Zoomable',
          style: TextStyle(fontSize: size),
        ),
      ),
    );
  }
}

Upvotes: 1

Views: 2228

Answers (1)

Ovidiu
Ovidiu

Reputation: 8714

It's because you are calling double size = 70; within the build() function. When you are calling setState(), the build() function is called and the size is set back to 70. Simply move the size to outside of the build() function and it will work.

Upvotes: 2

Related Questions