You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中同时动画Opacity与字号时文本被截断的问题求助

Flutter文本动画在三星S21设备上出现截断问题

我在Flutter中尝试使用不同Curve同时对文本的Opacity(透明度)和字号执行动画,但动画过程中文本的顶部和底部会被截断。该问题仅在我的三星S21物理设备上出现,模拟器无此情况,且并非所有系统默认字号配置下都会触发。

以下是我的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{

  late AnimationController _controller;
  late Animation<double> _fontSizeAnimation;
  late Animation<double> _opacityAnimation;
  late TextStyleTween _styleTween;
  late Tween<double> _opacityTween;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(duration: const Duration(seconds: 2), vsync: this);
    _fontSizeAnimation = CurvedAnimation(parent: _controller, curve: Curves.elasticOut);
    _opacityAnimation = CurvedAnimation(parent: _controller, curve: Curves.easeOutCirc)
      ..addListener(() {
        setState(() {

        });
      });
    _styleTween = TextStyleTween(
      begin: const TextStyle(fontSize: 0, color: Colors.black), end: const TextStyle(fontSize: 50, color: Colors.black),
    );
    _opacityTween = Tween<double>(begin: 0.0, end: 1.0);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Opacity(
          opacity: _opacityTween.evaluate(_opacityAnimation),
          child: DefaultTextStyleTransition(
              style: _styleTween.animate(_fontSizeAnimation),
              child: const Text('2'),
          ),
        )
      ),
      floatingActionButton: FloatingActionButton(onPressed: () {
        _controller.forward(from: 0.0);
      },
      ),
    );
  }
}

我尝试了不同的动画实现方式,例如使用AnimatedBuilder,还尝试了不同的容器组件(Center、Container、SizedBox)以排除裁剪问题,但问题依旧存在。

内容的提问来源于stack exchange,提问作者Floorian93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:39:50