我要翻译的内容是:“如何在我的卡片背面以正确的书写方向显示文本?”

huangapple go评论50阅读模式
英文:

How can I display text on the back of my card in the correct writing direction?

问题

我有一个Flutter容器,用GestureDetector切换其前后两面。该容器通过Transform方法进行动画处理,围绕Y轴旋转以切换前后两面。

我的问题是容器背面的文本未正确对齐。文本显示为镜像。我尝试更改Text小部件的textDirection属性以使文本从右到左或从左到右对齐,但未成功。

旋转后,有没有另一种方法正确对齐容器背面的文本?

这是我的当前代码:

import 'dart:math';

import 'package:flutter/material.dart';

class FlipCard extends StatefulWidget {
  const FlipCard({Key? key}) : super(key: key);

  @override
  State<FlipCard> createState() => _FlipCardState();
}

class _FlipCardState extends State<FlipCard>
    with SingleTickerProviderStateMixin {
  bool _isFrontVisible = true;
  late AnimationController _flipAnimationController;
  late Animation<double> _flipAnimation;

  @override
  void initState() {
    super.initState();
    _flipAnimationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    _flipAnimation =
        Tween<double>(begin: 0.0, end: 1.0).animate(_flipAnimationController);
  }

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

  void _toggleCard() {
    if (_isFrontVisible) {
      _flipAnimationController.forward();
    } else {
      _flipAnimationController.reverse();
    }
    _isFrontVisible = !_isFrontVisible;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: GestureDetector(
          onTap: () => _toggleCard(),
          child: AnimatedBuilder(
            animation: _flipAnimation,
            builder: (context, child) {
              final value = _flipAnimation.value;
              final perspective = 0.002;
              final angle = value * pi;
              final isFrontFacing = angle <= pi / 2;
              return Transform(
                transform: Matrix4.identity()
                  ..setEntry(3, 2, perspective)
                  ..rotateY(angle),
                alignment: Alignment.center,
                child: Container(
                  height: 200,
                  width: 300,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: isFrontFacing ? Colors.white : Colors.blueGrey,
                  ),
                  child: Center(
                    child: Text(
                      isFrontFacing ? "Vorderseite" : "Rückseite",
                      style: TextStyle(
                        fontSize: 20,
                        color: isFrontFacing ? Colors.black : Colors.white,
                      ),
                      textDirection:
                          isFrontFacing ? TextDirection.rtl : TextDirection.ltr,
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
英文:

I have a Flutter container that is toggled with a GestureDetector to switch between its front and back sides. The container is animated with the Transform method, rotating around the Y-axis to switch between the front and back sides.

The problem I have is that the text on the back side of the container is not properly aligned. The text appears mirrored. I have tried changing the textDirection property of the Text widget to align the text from right to left or left to right, but it didn't work.

Is there another way to properly align the text on the back of the container after it has been rotated?

Here is my current code:

import &#39;dart:math&#39;;
import &#39;package:flutter/material.dart&#39;;
class FlipCard extends StatefulWidget {
const FlipCard({Key? key}) : super(key: key);
@override
State&lt;FlipCard&gt; createState() =&gt; _FlipCardState();
}
class _FlipCardState extends State&lt;FlipCard&gt;
with SingleTickerProviderStateMixin {
bool _isFrontVisible = true;
late AnimationController _flipAnimationController;
late Animation&lt;double&gt; _flipAnimation;
@override
void initState() {
super.initState();
_flipAnimationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
_flipAnimation =
Tween&lt;double&gt;(begin: 0.0, end: 1.0).animate(_flipAnimationController);
}
@override
void dispose() {
_flipAnimationController.dispose();
super.dispose();
}
void _toggleCard() {
if (_isFrontVisible) {
_flipAnimationController.forward();
} else {
_flipAnimationController.reverse();
}
_isFrontVisible = !_isFrontVisible;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
onTap: () =&gt; _toggleCard(),
child: AnimatedBuilder(
animation: _flipAnimation,
builder: (context, child) {
final value = _flipAnimation.value;
final perspective = 0.002;
final angle = value * pi;
final isFrontFacing = angle &lt;= pi / 2;
return Transform(
transform: Matrix4.identity()
..setEntry(3, 2, perspective)
..rotateY(angle),
alignment: Alignment.center,
child: Container(
height: 200,
width: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: isFrontFacing ? Colors.white : Colors.blueGrey,
),
child: Center(
child: Text(
isFrontFacing ? &quot;Vorderseite&quot; : &quot;R&#252;ckseite&quot;,
style: TextStyle(
fontSize: 20,
color: isFrontFacing ? Colors.black : Colors.white,
),
textDirection:
isFrontFacing ? TextDirection.rtl : TextDirection.ltr,
),
),
),
);
},
),
),
),
);
}
}

Thank you in advance for your help!

答案1

得分: 1

将卡片内部的文本小部件进行转换(用于“背面”)是一种可能性。

下面我们将正面和背面的卡片拆分为各自的小部件,然后根据 isFrontFacing 调用相应的小部件。

class CardTextBack extends StatelessWidget {
  const CardTextBack();

  @override
  Widget build(BuildContext context) {
    return Transform(
        alignment: Alignment.center,
        transform: Matrix4.rotationY(pi),
        child: const Text("Rückseite",
            style: TextStyle(
              fontSize: 20,
              color: Colors.white,
            )));
  }
}

class CardTextFront extends StatelessWidget {
  const CardTextFront();

  @override
  Widget build(BuildContext context) {
    return const Text("Vorderseite",
        style: TextStyle(
          fontSize: 20,
          color: Colors.black,
        ));
  }
}

class CardText extends StatelessWidget {
  const CardText(this.isFrontFacing);

  final bool isFrontFacing;

  @override
  Widget build(BuildContext context) {
    if (isFrontFacing) {
      return const CardTextFront();
    }
    return const CardTextBack();
  }
}

在你的 _FlipCardState 中,将最终的 child: 替换为:

child: Center(child: CardText(isFrontFacing)),
英文:

Transforming the Text widget inside the card (for the "backside") is one possibility.

Below we've split the front & back cards into their own Widgets, then call either Widget depending on isFrontFacing.

class CardTextBack extends StatelessWidget {
const CardTextBack();
@override
Widget build(BuildContext context) {
return Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(pi),
child: const Text(&quot;R&#252;ckseite&quot;,
style: TextStyle(
fontSize: 20,
color: Colors.white,
)));
}
}
class CardTextFront extends StatelessWidget {
const CardTextFront();
@override
Widget build(BuildContext context) {
return const Text(&quot;Vorderseite&quot;,
style: TextStyle(
fontSize: 20,
color: Colors.black,
));
}
}
class CardText extends StatelessWidget {
const CardText(this.isFrontFacing);
final bool isFrontFacing;
@override
Widget build(BuildContext context) {
if (isFrontFacing) {
return const CardTextFront();
}
return const CardTextBack();
}
}

In your _FlipCardState we replace your final child: with:

child: Center(child: CardText(isFrontFacing)),

huangapple
  • 本文由 发表于 2023年4月17日 07:18:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定