mirror of
https://github.com/flutter/samples.git
synced 2026-04-27 09:58:50 +00:00
Add a new sample, `simple_sdf`. This is similar to `simple_shader`, but it focuses on using Signed Distance Functions within the fragment shader. Relates to issue [#183043](https://github.com/flutter/flutter/issues/183043) ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [ ] I have added sample code updates to the [changelog]. - [x] I updated/added relevant documentation (doc comments with `///`). If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md [changelog]: ../CHANGELOG.md
60 lines
1.4 KiB
Dart
60 lines
1.4 KiB
Dart
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_shaders/flutter_shaders.dart';
|
|
|
|
void main() {
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Simple SDF Demo',
|
|
theme: ThemeData(colorSchemeSeed: Colors.blue),
|
|
home: const MyHomePage(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MyHomePage extends StatelessWidget {
|
|
const MyHomePage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Simple SDF Demo')),
|
|
body: ShaderBuilder(
|
|
assetKey: 'shaders/SDF.frag',
|
|
(context, shader, child) => CustomPaint(
|
|
size: MediaQuery.of(context).size,
|
|
painter: ShaderPainter(shader: shader),
|
|
),
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class ShaderPainter extends CustomPainter {
|
|
ShaderPainter({required this.shader});
|
|
ui.FragmentShader shader;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
shader.setFloat(0, size.width);
|
|
shader.setFloat(1, size.height);
|
|
|
|
final paint = Paint()..shader = shader;
|
|
canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(covariant CustomPainter oldDelegate) {
|
|
return false;
|
|
}
|
|
}
|