text
stringlengths
1
474
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?>[];
void _onPanUpdate(DragUpdateDetails details) {
setState(() {
final RenderBox referenceBox = context.findRenderObject() as RenderBox;
final Offset localPosition = referenceBox.globalToLocal(
details.globalPosition,
);
_points = List.from(_points)..add(localPosition);
});
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onPanUpdate: _onPanUpdate,
onPanEnd: (details) => _points.add(null),
child: CustomPaint(
painter: SignaturePainter(_points),
size: Size.infinite,
),
);
}
}
class SignaturePainter extends CustomPainter {
const SignaturePainter(this.points);
final List<Offset?> points;
@override
void paint(Canvas canvas, Size size) {
final Paint 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>
Where is the widget’s opacity?
On Xamarin.Forms, all VisualElements have an Opacity.
In Flutter, you need to wrap a widget in an
Opacity widget to accomplish this.<topic_end>
<topic_start>
How do I build custom widgets?
In Xamarin.Forms, you typically subclass VisualElement,
or use a pre-existing VisualElement, 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 control
based off a Grid with numerous VisualElements added in,
while extending with custom 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 {
const CustomButton(this.label, {super.key});
final String label;
@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>
Navigation
<topic_end>
<topic_start>
How do I navigate between pages?
In Xamarin.Forms, the NavigationPage class
provides a hierarchical navigation experience
where the user is able to navigate through pages,
forwards and backwards.Flutter has a similar implementation,