teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

よりよい方法の追記

2021/12/08 05:56

投稿

YashaWedyue
YashaWedyue

スコア830

answer CHANGED
@@ -1,3 +1,92 @@
1
+ **// 2021/12/8 追記**
2
+
3
+ 以前の内容ですと、画像の縦幅が画面のサイズを超えるとき、縦幅が画面サイズに合わせて小さくなり、
4
+ それに合わせて横幅も小さくなってループ時にずれることがありました。
5
+ ずれない方法を考えましたので、追記しておきます。
6
+
7
+ ```lang-dart
8
+ // デバイスの画面サイズ(高さ)
9
+ late double deviceHeight;
10
+
11
+ // 画像の横幅
12
+ late double imageWidth;
13
+
14
+ // アニメーションコントローラー
15
+ late AnimationController controller;
16
+
17
+ // アニメーション
18
+ late Animation<RelativeRect> rectAnimation;
19
+
20
+ // contextを受け取るために、didChangeDependenciesで初期化する
21
+ @override
22
+ void didChangeDependencies() {
23
+ super.didChangeDependencies();
24
+
25
+ // デバイスの画面サイズ(高さ)取得
26
+ deviceHeight = MediaQuery.of(context).size.height;
27
+
28
+ // 画像の縦横比
29
+ double aspectRatio = 1.8;
30
+
31
+ // 画像の横幅計算
32
+ imageWidth = deviceHeight * aspectRatio;
33
+
34
+ // コントローラー初期化
35
+ controller = AnimationController(
36
+ vsync: this,
37
+ duration: const Duration(seconds: 10),
38
+ )..repeat();
39
+
40
+ // 1枚目が終わったら止めてリピートする
41
+ rectAnimation = RelativeRectTween(
42
+ begin: RelativeRect.fromLTRB(imageWidth, 0, 0, 0),
43
+ end: RelativeRect.fromLTRB(0, 0, imageWidth, 0),
44
+ ).animate(
45
+ CurvedAnimation(parent: controller, curve: Curves.linear),
46
+ );
47
+ }
48
+
49
+ @override
50
+ void dispose() {
51
+ controller.dispose();
52
+ super.dispose();
53
+ }
54
+
55
+ @override
56
+ Widget build(BuildContext context) {
57
+ return Scaffold(
58
+ body: PositionedTransition(
59
+ rect: rectAnimation,
60
+ child: OverflowBox(
61
+ maxWidth: imageWidth * 2, // 画像2枚分
62
+ maxHeight: deviceHeight, // 画面の高さに合わせる
63
+ child: Row(
64
+ children: <Widget>[
65
+ Image(
66
+ height: deviceHeight, // 画面の高さに合わせる
67
+ fit: BoxFit.fitHeight,
68
+ image: const AssetImage('画像のパス'),
69
+ ),
70
+ Image(
71
+ height: deviceHeight, // 画面の高さに合わせる
72
+ fit: BoxFit.fitHeight,
73
+ image: const AssetImage('画像のパス'),
74
+ ),
75
+ ],
76
+ ),
77
+ ),
78
+ ),
79
+ );
80
+ }
81
+ ```
82
+
83
+ この方法は、画像が画面全体を覆うようなものである場合に有用です。
84
+ 縦幅が絶対に画面サイズを超えない場合は、以前の方法で問題ないかと思います。
85
+
86
+
87
+
88
+ **// 以前の自己解決時の内容**
89
+
1
90
  `PositionedTransition`を使ってなんとかそれっぽくなったので自己解決とします。
2
91
 
3
92
  ```dart