gpt4 book ai didi

mobile - Flutter Hero创建重复的文本

转载 作者:行者123 更新时间:2023-12-03 03:05:37 25 4
gpt4 key购买 nike

我正在尝试编写 flutter 朔迷离的Hero动画以在屏幕之间切换。我使用一种动画将动画从卡片移动到下一页,并使用另一种动画将文本从卡片移动到下一页。不幸的是,在我在下面的代码中放慢了速度的动画中,有一个重复的文本项。

以下是我用来测试点击卡片以将其展开到新页面的代码。我一生都不知道如何防止文本在动画中重复。罪魁祸首是我在页面上有一个英雄(以动画化页面之间的过渡),在页面内有一个英雄。

Card (page-hero)
---> CardText (text-hero)

and

Page (page-hero)
---> PageText (text-hero)

注意:下面的代码是我从另一个问题采用的,用于测试该理论: How to expand a card on tap in flutter?
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new FirstPage(title: 'Color Palette'),
);
}
}

class FirstPage extends StatefulWidget {
FirstPage({Key key, this.title}) : super(key: key);

final String title;

@override
_FirstPageState createState() => new _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
final palette = [
{'#E53935': 0xFFE53935},
{'#D81B60': 0xFFD81B60},
{'#8E24AA': 0xFF8E24AA},
{'#5E35B1': 0xFF5E35B1},
{'#3949AB': 0xFF3949AB},
{'#1E88E5': 0xFF1E88E5},
{'#039BE5': 0xFF039BE5},
{'#00ACC1': 0xFF00ACC1},
{'#00897B': 0xFF00897B},
{'#43A047': 0xFF43A047},
{'#7CB342': 0xFF7CB342},
{'#C0CA33': 0xFFC0CA33},
];

@override
Widget build(BuildContext context) {
timeDilation = 10.0;
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Container(
child: new ListView.builder(
itemCount: palette.length,
itemBuilder: (context, index) => new Container(child: new Hero(
tag: palette[index].keys.first,
child: new GestureDetector(
onTap: () {
Navigator
.of(context)
.push(new ColorPageRoute(palette[index]));
},
child: new Card(
// height: 64.0,
// width: double.infinity,
color: new Color(palette[index].values.first),
child: new Align(
alignment: Alignment.topRight,
child: new Hero(
tag: 'text-${palette[index].keys.first}',
child: SizedBox(
width: 250,
child: new Text(
palette[index].keys.first,
style: Theme.of(context).textTheme.title.copyWith(
color: Colors.white,
),
)),
),
),
),
),
))),
),
);
}
}

class SecondPage extends StatelessWidget {
final Map<String, int> color;

SecondPage({this.color});

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Color'),
),
body: Container( child: new Hero(
tag: color.keys.first,
child: new Container(
color: new Color(color.values.first),
child: new Align(
// alignment: Alignment.topCenter,
child: new Hero(
tag: 'text-${color.keys.first}',
child: new SizedBox(
width: 200,
child: new Text(
color.keys.first,
style:
Theme.of(context).textTheme.title.copyWith(color: Colors.white),
)),
),
),
),
)),
);
}
}

class ColorPageRoute extends MaterialPageRoute {
ColorPageRoute(Map<String, int> color)
: super(
builder: (context) => new SecondPage(
color: color,
), fullscreenDialog: true);

@override
Widget buildTransitions(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(opacity: animation, child: child);
}
}

预期的动画是一小段文字,而不是您在上面的代码中看到的重复文字。我该怎么做,以免得到重复的文本?谢谢!

最佳答案

在英雄中不包含英雄,已解决

body: Container( child: new Hero(
tag: color.keys.first,
child: new Container(
color: new Color(color.values.first),
child: new Align(
alignment: Alignment.topCenter,
child: Hero( // remove this hero
tag: 'text-${color.keys.first}',
child: new SizedBox(
width: 200,
child: new Text(
color.keys.first,
style:
Theme.of(context).textTheme.title.copyWith(color: Colors.white),
)),
),
),
),

关于mobile - Flutter Hero创建重复的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54262847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com