text
stringlengths
1
474
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) {
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>
Widget opacity
In UIKit, everything has .opacity or .alpha.
In Flutter, most of the time you need to
wrap a widget in an Opacity widget to accomplish this.<topic_end>
<topic_start>
Custom Widgets
In UIKit, you typically subclass UIView, 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).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
This section of the document discusses navigation
between pages of an app, the push and pop mechanism, and more.<topic_end>
<topic_start>
Navigating between pages
In UIKit, to travel between view controllers, you can use a
UINavigationController that manages the stack of view controllers
to display.Flutter has a similar implementation,
using a Navigator and Routes.
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 a
UIViewController. The navigator works in a similar way to the iOS
UINavigationController, in that it can push() and pop()
routes depending on whether you want to navigate to, or back from, a view.To navigate between pages, you have a couple options:The following example builds a Map.
<code_start>void main() {
runApp(
CupertinoApp(
home: const MyAppHome(), // becomes the route named '/'
routes: <String, WidgetBuilder>{
'/a': (context) => const MyPage(title: 'page A'),
'/b': (context) => const MyPage(title: 'page B'),
'/c': (context) => const MyPage(title: 'page C'),
},
),
);
}<code_end>
Navigate to a route by pushing its name to the Navigator.
<code_start>Navigator.of(context).pushNamed('/b');<code_end>
The Navigator class handles routing in Flutter and is used to get
a result back from a route that you have pushed on the stack.
This is done by awaiting on the Future returned by push().For example, to start a location route that lets the user select their
location, you might do the following:
<code_start>Object? coordinates = await Navigator.of(context).pushNamed('/location');<code_end>
And then, inside your location route, once the user has selected their
location, pop() the stack with the result:
<code_start>Navigator.of(context).pop({'lat': 43.821757, 'long': -79.226392});<code_end>