ภาพเคลื่อนไหวโดยนัย
พัฒนาภาพเคลื่อนไหวโดยนัยด้วยวิดเจ็ตอย่าง `AnimatedContainer` และ `AnimatedOpacity` เพื่อสร้างการเปลี่ยนผ่านที่ราบรื่นและเกิดขึ้นโดยอัตโนมัติ
ภาพเคลื่อนไหวโดยนัย เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flutter Mobile Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Easy Animations with Implicit Widgets
Flutter offers an easy way to add smooth animations to your app: Implicit Animations. These are "set-and-forget" animations where you simply tell Flutter the desired end state, and it handles the transition.
You don't need to manage animation controllers or listeners. Just change a property, and Flutter animates it for you!
The Magic Behind Implicit Animations
When you use an implicit animation widget, you provide a new value for one of its animated properties (like size or color). Flutter then automatically interpolates between the old value and the new value over a set duration.
It's like telling Flutter: "Go from A to B in 500 milliseconds." Flutter figures out all the steps in between.
Animating Size & Color with Container
The AnimatedContainer is one of the most versatile implicit widgets. It automatically animates changes to its properties like width, height, color, alignment, and decoration.
You just update the state with new values, and AnimatedContainer does the rest!
Try AnimatedContainer
Tap the container below! Notice how its size and color smoothly transition. We're just changing its properties and calling setState, and AnimatedContainer handles the animation.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AnimatedContainer Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const AnimatedContainerScreen(),
);
}
}
class AnimatedContainerScreen extends StatefulWidget {
const AnimatedContainerScreen({super.key});
@override
State<AnimatedContainerScreen> createState() => _AnimatedContainerScreenState();
}
class _AnimatedContainerScreenState extends State<AnimatedContainerScreen> {
double _width = 100.0;
double _height = 100.0;
Color _color = Colors.blue;
BorderRadiusGeometry _borderRadius = BorderRadius.circular(8.0);
void _updateContainer() {
setState(() {
_width = _width == 100.0 ? 200.0 : 100.0;
_height = _height == 100.0 ? 150.0 : 100.0;
_color = _color == Colors.blue ? Colors.red : Colors.blue;
_borderRadius = _borderRadius == BorderRadius.circular(8.0)
? BorderRadius.circular(50.0)
: BorderRadius.circular(8.0);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AnimatedContainer')),
body: Center(
child: GestureDetector(
onTap: _updateContainer,
child: AnimatedContainer(
width: _width,
height: _height,
decoration: BoxDecoration(
color: _color,
borderRadius: _borderRadius,
),
alignment: Alignment.center,
duration: const Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
child: const Text(
'Tap me!',
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
}Fading Widgets with AnimatedOpacity
The AnimatedOpacity widget is perfect for smoothly showing or hiding parts of your UI. It animates changes to its opacity property.
An opacity of 0.0 means completely transparent (invisible), and 1.0 means fully opaque (visible).
See AnimatedOpacity in Action
Watch the text fade in and out with a smooth transition. We're just toggling a boolean state and updating the opacity value based on it.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AnimatedOpacity Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const AnimatedOpacityScreen(),
);
}
}
class AnimatedOpacityScreen extends StatefulWidget {
const AnimatedOpacityScreen({super.key});
@override
State<AnimatedOpacityScreen> createState() => _AnimatedOpacityScreenState();
}
class _AnimatedOpacityScreenState extends State<AnimatedOpacityScreen> {
bool _isVisible = true;
void _toggleVisibility() {
setState(() {
_isVisible = !_isVisible;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AnimatedOpacity')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedOpacity(
opacity: _isVisible ? 1.0 : 0.0,
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
child: const Text(
'Hello Flutter!',
style: TextStyle(fontSize: 24),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleVisibility,
child: Text(_isVisible ? 'Hide Text' : 'Show Text'),
),
],
),
),
);
}
}Smoothly Cross-Fading Widgets
When you need to switch between two different widgets with a smooth fading effect, AnimatedCrossFade is your go-to. It's great for showing different states, like a loading spinner turning into content.
You provide a firstChild and a secondChild, and control which one is visible using crossFadeState.
Try AnimatedCrossFade
Tap the button to switch between two different text widgets. Notice how one fades out while the other fades in, creating a clean transition.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AnimatedCrossFade Demo',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: const AnimatedCrossFadeScreen(),
);
}
}
class AnimatedCrossFadeScreen extends StatefulWidget {
const AnimatedCrossFadeScreen({super.key});
@override
State<AnimatedCrossFadeScreen> createState() => _AnimatedCrossFadeScreenState();
}
class _AnimatedCrossFadeScreenState extends State<AnimatedCrossFadeScreen> {
bool _showFirst = true;
void _toggleChild() {
setState(() {
_showFirst = !_showFirst;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AnimatedCrossFade')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedCrossFade(
duration: const Duration(milliseconds: 700),
crossFadeState: _showFirst
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
firstChild: Container(
width: 200,
height: 100,
color: Colors.blueAccent,
alignment: Alignment.center,
child: const Text(
'First Widget',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
secondChild: Container(
width: 200,
height: 100,
color: Colors.deepOrange,
alignment: Alignment.center,
child: const Text(
'Second Widget',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
const SizedBox(height: 30),
ElevatedButton(
onPressed: _toggleChild,
child: const Text('Toggle Widget'),
),
],
),
),
);
}
}Fine-Tuning Animations: Duration & Curve
Two crucial properties for all implicit animations are duration and curve:
duration: Defines how long the animation takes to complete. UseDuration(milliseconds: ...)orDuration(seconds: ...).curve: Controls the animation's speed over its duration.Curves.linearis constant speed,Curves.easeOutstarts fast and slows down,Curves.bounceInadds a bouncy effect.
Experiment with different curves to find the perfect feel!
Implicit Animation Check
You want to create a button that, when tapped, makes a text widget smoothly disappear. Which implicit animation widget is best suited for this task?
Implicit Animations Recap
Great job! You've learned about the power of Implicit Animations in Flutter. They allow you to add smooth, automatic transitions by simply changing widget properties.
- We explored
AnimatedContainerfor animating size, color, and shape. AnimatedOpacityfor fading elements in and out.AnimatedCrossFadefor switching between two widgets with a smooth blend.- Remember to set
durationand choose an appropriatecurvefor your animations!
Next, we'll dive into Explicit Animations for more complex, custom control.
คำถามที่พบบ่อย
บทเรียน “ภาพเคลื่อนไหวโดยนัย” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ภาพเคลื่อนไหวโดยนัย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ภาพเคลื่อนไหวโดยนัย”
พัฒนาภาพเคลื่อนไหวโดยนัยด้วยวิดเจ็ตอย่าง `AnimatedContainer` และ `AnimatedOpacity` เพื่อสร้างการเปลี่ยนผ่านที่ราบรื่นและเกิดขึ้นโดยอัตโนมัติ คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ภาพเคลื่อนไหวโดยนัย” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม
ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- CustomPainter และ Canvas
- ภาพเคลื่อนไหวโดยนัย
- ภาพเคลื่อนไหวแบบกำหนดชัดเจนและ Hero
- แอนิเมชันแบบเหลื่อมจังหวะและอิงฟิสิกส์