text
stringlengths 1
474
|
---|
child: Column(
|
mainAxisAlignment: MainAxisAlignment.center,
|
children: [
|
Text('$_counter'),
|
TextButton(
|
onPressed: () => setState(() {
|
_counter++;
|
}),
|
child: const Text('+'),
|
),
|
],
|
),
|
),
|
);
|
}
|
}<code_end>
|
To learn more ways to manage state, check out State management.<topic_end>
|
<topic_start>
|
Animations
|
Two main types of UI animations exist.<topic_end>
|
<topic_start>Implicit Animation
|
SwiftUI and Flutter take a similar approach to animation.
|
In both frameworks, you specify parameters like duration, and curve.In SwiftUI, you use the animate() modifier to handle implicit
|
animation.Flutter includes widgets for implicit animation.
|
This simplifies animating common widgets.
|
Flutter names these widgets with the following format: AnimatedFoo.For example: To rotate a button, use the AnimatedRotation class.
|
This animates the Transform.rotate widget.
|
<code_start> AnimatedRotation(
|
duration: const Duration(seconds: 1),
|
turns: turns,
|
curve: Curves.easeIn,
|
child: TextButton(
|
onPressed: () {
|
setState(() {
|
turns += .125;
|
});
|
},
|
child: const Text('Tap me!')),
|
),<code_end>
|
Flutter allows you to create custom implicit animations.
|
To compose a new animated widget, use the TweenAnimationBuilder.<topic_end>
|
<topic_start>Explicit Animation
|
For explicit animations, SwiftUI uses the withAnimation() function.Flutter includes explicitly animated widgets with names formatted
|
like FooTransition.
|
One example would be the RotationTransition class.Flutter also allows you to create a custom explicit animation using
|
AnimatedWidget or AnimatedBuilder.To learn more about animations in Flutter, see Animations overview.<topic_end>
|
<topic_start>
|
Drawing on the Screen
|
In SwiftUI, you use CoreGraphics to draw lines and shapes to the
|
screen.Flutter has an API based on the Canvas class,
|
with two classes that help you draw:CustomPaint that requires a painter:
|
<code_start> CustomPaint(
|
painter: SignaturePainter(_points),
|
size: Size.infinite,
|
),<code_end>
|
CustomPainter that implements your algorithm to draw to the canvas.
|
<code_start>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>
|
Navigation
|
This section explains how to navigate between pages of an app,
|
the push and pop mechanism, and more.<topic_end>
|
<topic_start>
|
Navigating between pages
|
Developers build iOS and macOS apps with different pages called
|
navigation routes.In SwiftUI, the NavigationStack represents this stack of pages.The following example creates an app that displays a list of persons.
|
To display a person’s details in a new navigation link,
|
tap on that person.If you have a small Flutter app without complex linking,
|
use Navigator with named routes.
|
After defining your navigation routes,
|
call your navigation routes using their names.Name each route in the class passed to the runApp() function.
|
The following example uses App:
|
<code_start>// Defines the route name as a constant
|
// so that it's reusable.
|
const detailsPageRouteName = '/details';
|
class App extends StatelessWidget {
|
const App({
|
super.key,
|
});
|
@override
|
Widget build(BuildContext context) {
|
return CupertinoApp(
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.