text
stringlengths
1
80
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: text(widget.title),
),
body: center(
child: FadeTransition(
opacity: curve,
child: const FlutterLogo(
size: 100,
),
),
),
floatingActionButton: FloatingActionButton(
tooltip: 'fade',
onPressed: () {
controller.forward();
},
child: const Icon(Icons.brush),
),
);
}
}
<code_end>
for more information, see
animation & motion widgets,
the animations tutorial,
and the animations overview.
<topic_end>
<topic_start>
how do i use a canvas to draw/paint?
in android, you would use the canvas and drawable
to draw images and shapes to the screen.
flutter has a similar canvas API as well,
since it鈥檚 based on the same low-level rendering engine, skia.
as a result, painting to a canvas in flutter
is a very familiar task for android developers.
flutter has two classes that help you draw to the canvas: CustomPaint
and CustomPainter,
the latter of which implements your algorithm to draw to the canvas.
to learn how to implement a signature painter in flutter,
see collin鈥檚 answer on custom paint.
<code_start>
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: DemoApp()));
class DemoApp extends StatelessWidget {
const DemoApp({super.key});
@override
widget build(BuildContext context) => const scaffold(body: signature());
}
class signature extends StatefulWidget {
const signature({super.key});
@override
SignatureState createState() => SignatureState();
}
class SignatureState extends State<Signature> {
List<Offset?> _points = <offset>[];
@override
widget build(BuildContext context) {
return GestureDetector(
onPanUpdate: (details) {
setState(() {
RenderBox? referenceBox = context.findRenderObject() as RenderBox;
offset localPosition =
referenceBox.globalToLocal(details.globalPosition);
_points = List.from(_points)..add(localPosition);
});
},
onPanEnd: (details) => _points.add(null),
child: CustomPaint(
painter: SignaturePainter(_points),
size: size.infinite,
),
);
}
}
class SignaturePainter extends CustomPainter {
SignaturePainter(this.points);
final List<Offset?> points;
@override
void paint(Canvas canvas, size size) {
var paint = paint()
..color = colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5;
for (int i = 0; i < points.length - 1; i++) {
if (points[i] != null && points[i + 1] != null) {
canvas.drawLine(points[i]!, points[i + 1]!, paint);
}
}
}
@override
bool shouldRepaint(SignaturePainter oldDelegate) =>
oldDelegate.points != points;
}
<code_end>
<topic_end>
<topic_start>
how do i build custom widgets?