Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active June 23, 2024 23:30
Show Gist options
  • Save Miqueas/b66297d8de4a29000e9cb4d3f9cdc3f5 to your computer and use it in GitHub Desktop.
Save Miqueas/b66297d8de4a29000e9cb4d3f9cdc3f5 to your computer and use it in GitHub Desktop.
Basic audio waveform in Flutter

Basic audio waveform in Flutter

A simple example I made while learning about custom paints in Flutter with CustomPaint and CustomPainter.

This is part of an article I wrote in DEV.to and Medium, check them out!

image

import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(App());
}
final class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
alignment: Alignment.center,
color: Color(0xFF16161B),
child: FittedBox(
child: Container(
width: 400,
height: 100,
child: CustomPaint(
painter: WavePainter(),
)
)
),
),
),
);
}
}
final class WavePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final brush = Paint()
..color = Color(0xFFFAFAFA)
..strokeWidth = 3
..strokeCap = StrokeCap.round;
var shift = 0.0;
final verticalCenter = size.height / 2;
final values = List<double>.generate(100, (_) {
return Random().nextDouble() * verticalCenter;
});
for (var i = 0; i < values.length && shift < size.width; i++) {
canvas.drawLine(
Offset(shift, verticalCenter - values[i]),
Offset(shift, verticalCenter + values[i]),
brush
);
shift += 6;
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment