嗨,
我是飞镖和颤振的新手,因此为什么你会发现我的代码很乱(我真的不知道什么时候使用类和小部件)。
我正在尝试使用来自资产的图像生成可点击的卡片,这些卡片在点击时会转身 - 问题是它们作为一个,当点击一个时它们都会转动。当我第二次点击时,什么也没有发生。
import 'dart:typed_data';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'assetsLists.dart';
import 'dart:math';
// AudioPlayer player = AudioPlayer();
void main() => runApp(const MaterialApp(
home: Home(),
));
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
AudioPlayer player = AudioPlayer();
bool isFront = true;
double angle = 0;
void _flip() {
setState(() {
angle = (angle + pi) % (2 * pi);
});
if(isFront) {
isFront = false;
}
else {
isFront = true;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("tap card to play a sound"),
centerTitle: true,
elevation: 5.0,
backgroundColor: Colors.green,
),
body: Padding(
padding: const EdgeInsets.fromLTRB(10, 12, 10, 12),
child: cardGridBuilder()),
);
}
Widget cardGridBuilder() => GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
itemCount: cardAssets.length,
itemBuilder: (context, index) {
return cardBuilder(index);
},
);
Widget cardBuilder(int number) {
return Container(
child: GestureDetector(
onTap: _flip,
child: Center(
child: TweenAnimationBuilder(
tween: Tween<double>(begin: 0, end: angle),
duration: Duration(milliseconds: 500),
builder: (BuildContext context, double val, __) {
String pictureAsset = cardAssets[number][0];
String soundAsset = cardAssets[number][1];
return (Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.001)
..rotateY(val),
child: Container(
width: 200,
height: 350,
child: isFront
? Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/Images/$pictureAsset')
)
)
)
: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/Images/$pictureAsset')
)
)
)
// String audioasset = 'assets/Sounds/$soundAsset';
// ByteData bytes = await rootBundle.load(audioasset); //load sound from assets
// Uint8List soundbytes = bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes);
// int result = await player.playBytes(soundbytes);
)
)
);
}
),
),
)
);
}
}
现在我只有几个样本,但最后会有 104 个:
List<List<String>> cardAssets = [
['IceWizardCard.webp','IceWizardSound.mp3','6'],
['MegaKnightCard.webp','MegaKnightSound.mp3','6'],
['WizardCard.webp','WizardSound.mp3', '4'],
];
我知道映射功能,但我选择了gridview builder,因为我有一个数组数组,其中保存了资产地址。也不要介意注释代码,它用于在特定卡上播放特定音频。
我谢谢大家!