text
stringlengths
1
474
@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?
In Android, you typically subclass View, or use a pre-existing view,
to override and implement methods that achieve the desired behavior.In Flutter, build a custom widget by composing
smaller widgets (instead of extending them).
It is somewhat similar to implementing a custom ViewGroup
in Android, where all the building blocks are already existing,
but you provide a different behavior—for example,
custom layout logic.For example, how do you build a CustomButton that takes a label in
the constructor? Create a CustomButton that composes a ElevatedButton with
a label, rather than by extending ElevatedButton:
<code_start>class CustomButton extends StatelessWidget {
final String label;
const CustomButton(this.label, {super.key});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
child: Text(label),
);
}
}<code_end>
Then use CustomButton, just as you’d use any other Flutter widget:
<code_start>@override
Widget build(BuildContext context) {
return const Center(
child: CustomButton('Hello'),
);
}<code_end>
<topic_end>
<topic_start>
Intents
<topic_end>
<topic_start>
What is the equivalent of an Intent in Flutter?
In Android, there are two main use cases for Intents: navigating between
Activities, and communicating with components. Flutter, on the other hand,
does not have the concept of intents, although you can still start intents
through native integrations (using a plugin).Flutter doesn’t really have a direct equivalent to activities and fragments;
rather, in Flutter you navigate between screens, using a Navigator and
Routes, all within the same Activity.A Route is an abstraction for a “screen” or “page” of an app, and a
Navigator is a widget that manages routes. A route roughly maps to an
Activity, but it does not carry the same meaning. A navigator can push
and pop routes to move from screen to screen. Navigators work like a stack
on which you can push() new routes you want to navigate to, and from
which you can pop() routes when you want to “go back”.In Android, you declare your activities inside the app’s AndroidManifest.xml.In Flutter, you have a couple options to navigate between pages:The following example builds a Map.
<code_start>void main() {
runApp(MaterialApp(
home: const MyAppHome(), // Becomes the route named '/'.
routes: <String, WidgetBuilder>{
'/a': (context) => const MyPage(title: 'page A'),