Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:audioplayers/audioplayers.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Image and Audio Player',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- visualDensity: VisualDensity.adaptivePlatformDensity,
- ),
- home: ImageGrid(),
- );
- }
- }
- class ImageGrid extends StatelessWidget {
- final List<String> imagePaths = [
- 'assets/image1.png',
- 'assets/image2.png',
- 'assets/image3.png',
- 'assets/image4.png',
- 'assets/image5.png',
- 'assets/image6.png',
- 'assets/image7.png',
- 'assets/image8.png',
- 'assets/image9.png',
- ];
- final List<String> audioPaths = [
- 'assets/audio1.mp3',
- 'assets/audio2.mp3',
- 'assets/audio3.mp3',
- 'assets/audio4.mp3',
- 'assets/audio5.mp3',
- 'assets/audio6.mp3',
- 'assets/audio7.mp3',
- 'assets/audio8.mp3',
- 'assets/audio9.mp3',
- ];
- Future<void> playAudio(int index) async {
- AudioCache player = AudioCache();
- player.fixedPlayer?.stop();
- await player.play(audioPaths[index]);
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Image and Audio Player'),
- ),
- body: GridView.builder(
- itemCount: imagePaths.length,
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 3,
- crossAxisSpacing: 4.0,
- mainAxisSpacing: 4.0,
- ),
- itemBuilder: (BuildContext context, int index) {
- return GestureDetector(
- onTap: () {
- playAudio(index);
- },
- child: Image.asset(
- imagePaths[index],
- fit: BoxFit.cover,
- ),
- );
- },
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement