diff --git a/dart/freeform-gradient/README.md b/dart/freeform-gradient/README.md index 50c0188..0125478 100644 --- a/dart/freeform-gradient/README.md +++ b/dart/freeform-gradient/README.md @@ -1,4 +1,4 @@ -# Flutter freeform gradiant +# Flutter freeform gradient This is a demonstration project to show usage of Flutter with Nix. diff --git a/dart/freeform-gradient/lib/main.dart b/dart/freeform-gradient/lib/main.dart index 807a6ab..f425ce9 100644 --- a/dart/freeform-gradient/lib/main.dart +++ b/dart/freeform-gradient/lib/main.dart @@ -38,18 +38,12 @@ class RootState extends State { theme: ThemeData( primarySwatch: Colors.purple, ), - home: MyHomePage( + home: FreeformGradient( title: 'Multi point gradient', config: MultiGradientConfig( distanceScaling: 1, blendStrength: 2, points: [ - MultiGradientPoint( - color: Colors.yellow, - intensity: 1.0, - position: vector.Vector2( - 1.0 / 2 + cos(angle) * 0.3, 1.0 / 2 + sin(angle) * 0.3), - ), MultiGradientPoint( color: Color(0xff6E10AB), intensity: 1, @@ -71,35 +65,66 @@ class RootState extends State { } } -class MyHomePage extends StatelessWidget { - MyHomePage({Key key, this.title, this.config}) : super(key: key); +class FreeformGradient extends StatefulWidget { + FreeformGradient({Key key, this.title, this.config}) : super(key: key); final String title; final MultiGradientConfig config; + FreeformGradientState createState() => FreeformGradientState(); +} + +class MultiGradientPoint { + double intensity; + vector.Vector4 color; + vector.Vector2 position; + + MultiGradientPoint({Color color, this.position, this.intensity = 1}) { + this.color = vector.Vector4(color.red.toDouble(), color.green.toDouble(), + color.blue.toDouble(), color.opacity); + } +} + +class MultiGradientConfig { + List points; + + double distanceScaling; + double blendStrength; + double opacityMultiplier = 1; + + MultiGradientConfig( + {this.points, + this.distanceScaling = 1, + this.blendStrength = 3, + this.opacityMultiplier = 1}); +} + +class FreeformGradientState extends State { + Future gradient; + + @override + void didChangeDependencies() { + gradient = generateImage(MediaQuery.of(context).size, widget.config); + super.didChangeDependencies(); + } + @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text(title), - ), - body: Center( - child: FutureBuilder( - future: generateImage(MediaQuery.of(context).size, config), - builder: (context, snapshot) { - if (snapshot.hasData) { - MediaQueryData query = MediaQuery.of(context); - return CustomPaint( - child: Container( - width: query.size.width, - height: query.size.height, - ), - painter: ImagePainter(image: snapshot.data)); - } + return FutureBuilder( + future: gradient, + builder: (context, snapshot) { + if (snapshot.hasData) { + MediaQueryData query = MediaQuery.of(context); + return CustomPaint( + child: Container( + width: query.size.width, + height: query.size.height, + ), + painter: ImagePainter(image: snapshot.data)); + } - return Text("Generating image"); - }, - )), + return Text("Generating image"); + }, ); } } @@ -148,31 +173,6 @@ Future generateImage(Size size, MultiGradientConfig config) async { return completer.future; } -class MultiGradientPoint { - double intensity; - vector.Vector4 color; - vector.Vector2 position; - - MultiGradientPoint({Color color, this.position, this.intensity = 1}) { - this.color = vector.Vector4(color.red.toDouble(), color.green.toDouble(), - color.blue.toDouble(), color.opacity); - } -} - -class MultiGradientConfig { - List points; - - double distanceScaling; - double blendStrength; - double opacityMultiplier = 1; - - MultiGradientConfig( - {this.points, - this.distanceScaling = 1, - this.blendStrength = 3, - this.opacityMultiplier = 1}); -} - double clamp(double min, double max, double value) { if (value < min) return min; if (value > max) return max;