mirror of
https://github.com/flutter/samples.git
synced 2025-11-09 06:18:49 +00:00
Add simple_shader (#1668)
* Add `simple_shader` * Add NL at EOF * Adopt `analysis_defaults` * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Update simple_shader/shaders/simple.frag Co-authored-by: Jonah Williams <jonahwilliams@google.com> * Apply `clang-format` to `shaders/simple.frag` --------- Co-authored-by: Jonah Williams <jonahwilliams@google.com>
This commit is contained in:
88
simple_shader/lib/main.dart
Normal file
88
simple_shader/lib/main.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Simple Shader Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const MyHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key});
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
late Future<ui.FragmentProgram> program;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
program = ui.FragmentProgram.fromAsset('shaders/simple.frag');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Simple Shader'),
|
||||
),
|
||||
body: FutureBuilder<ui.FragmentProgram>(
|
||||
future: program,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return CustomPaint(
|
||||
size: MediaQuery.of(context).size,
|
||||
painter: ShaderPainter(
|
||||
shader: snapshot.data!.fragmentShader(),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('${snapshot.error}'));
|
||||
}
|
||||
return 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user