Виджет AnimatedSwitcher
Виджет AnimatedSwitcher анимирует изменения параметров при setState.
Синтаксис
AnimatedSwitcher(
duration: duration,
child: child,
)
Параметры
| Параметр | Описание |
|---|---|
duration |
Параметр AnimatedSwitcher.
|
child |
Параметр AnimatedSwitcher.
|
transitionBuilder |
Параметр AnimatedSwitcher.
|
Пример
Давайте сменим параметры по кнопке:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
bool big = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AnimatedSwitcher(duration:Duration(milliseconds:400), child: Text(big?'12345':'abcde', key: ValueKey(big))),
ElevatedButton(
onPressed: () => setState(() => big = !big),
child: Text('toggle'),
),
],
),
),
),
);
}
}
void main() {
runApp(const Demo());
}
На экране AnimatedSwitcher плавно доедет до новых значений, текст 'abcde' останется внутри.
Пример
Давайте повторим переход для второго состояния:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
const Demo({super.key});
@override
State<Demo> createState() => _DemoState();
}
class _DemoState extends State<Demo> {
bool big = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
AnimatedSwitcher(duration:Duration(milliseconds:400), child: Text(big?'abcde':'12345', key: ValueKey(!big))),
ElevatedButton(
onPressed: () => setState(() => big = !big),
child: Text('toggle'),
),
],
),
),
),
);
}
}
void main() {
runApp(const Demo());
}
На экране после toggle снова сработает AnimatedSwitcher, подпись '12345' внутри виджета.
Смотрите также
-
класс
AnimationController,
который задаётduration -
класс
Curves,
который задаётcurve -
виджет
AnimatedBuilder,
когда нужен ручнойbuilder -
виджет
MaterialApp,
корень примеров