Advertisement
shojeeb1943

Ans to the qsn no 3

Nov 29th, 2023
835
0
277 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.94 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:audioplayers/audioplayers.dart';
  3.  
  4.  
  5. void main() {
  6.   runApp(MyApp());
  7. }
  8.  
  9. class MyApp extends StatelessWidget {
  10.   @override
  11.   Widget build(BuildContext context) {
  12.     return MaterialApp(
  13.       title: 'Image and Audio Player',
  14.       theme: ThemeData(
  15.         primarySwatch: Colors.blue,
  16.         visualDensity: VisualDensity.adaptivePlatformDensity,
  17.       ),
  18.       home: ImageGrid(),
  19.     );
  20.   }
  21. }
  22.  
  23. class ImageGrid extends StatelessWidget {
  24.   final List<String> imagePaths = [
  25.     'assets/image1.png',
  26.     'assets/image2.png',
  27.     'assets/image3.png',
  28.     'assets/image4.png',
  29.     'assets/image5.png',
  30.     'assets/image6.png',
  31.     'assets/image7.png',
  32.     'assets/image8.png',
  33.     'assets/image9.png',
  34.  
  35.  
  36.   ];
  37.  
  38.   final List<String> audioPaths = [
  39.     'assets/audio1.mp3',
  40.     'assets/audio2.mp3',
  41.     'assets/audio3.mp3',
  42.     'assets/audio4.mp3',
  43.     'assets/audio5.mp3',
  44.     'assets/audio6.mp3',
  45.     'assets/audio7.mp3',
  46.     'assets/audio8.mp3',
  47.     'assets/audio9.mp3',
  48.  
  49.   ];
  50.  
  51.   Future<void> playAudio(int index) async {
  52.     AudioCache player = AudioCache();
  53.     player.fixedPlayer?.stop();
  54.     await player.play(audioPaths[index]);
  55.   }
  56.  
  57.   @override
  58.   Widget build(BuildContext context) {
  59.     return Scaffold(
  60.       appBar: AppBar(
  61.         title: Text('Image and Audio Player'),
  62.       ),
  63.       body: GridView.builder(
  64.         itemCount: imagePaths.length,
  65.         gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  66.           crossAxisCount: 3,
  67.           crossAxisSpacing: 4.0,
  68.           mainAxisSpacing: 4.0,
  69.         ),
  70.         itemBuilder: (BuildContext context, int index) {
  71.           return GestureDetector(
  72.             onTap: () {
  73.               playAudio(index);
  74.             },
  75.             child: Image.asset(
  76.               imagePaths[index],
  77.               fit: BoxFit.cover,
  78.             ),
  79.           );
  80.         },
  81.       ),
  82.     );
  83.   }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement