简体   繁体   中英

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),
        ),
      ),
    );
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM