我正在尝试使GestureDetector缩放文本小部件,但它根本无法工作,甚至没有错误...
请注意,我尝试了很多事情,例如用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),
),
),
);
}
}
答案 0 :(得分:1)
这是因为您正在NaN
函数中调用double size = 70;
。调用build()
时,将调用setState()
函数并将大小设置回70。只需将build()
移动到size
函数外部,它将起作用