| // Copyright 2014 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // Flutter code sample for AnimatedSwitcher |
| |
| import 'package:flutter/material.dart'; |
| |
| void main() => runApp(const MyApp()); |
| |
| class MyApp extends StatelessWidget { |
| const MyApp({Key? key}) : super(key: key); |
| |
| static const String _title = 'Flutter Code Sample'; |
| |
| @override |
| Widget build(BuildContext context) { |
| return const MaterialApp( |
| title: _title, |
| home: MyStatefulWidget(), |
| ); |
| } |
| } |
| |
| class MyStatefulWidget extends StatefulWidget { |
| const MyStatefulWidget({Key? key}) : super(key: key); |
| |
| @override |
| State<MyStatefulWidget> createState() => _MyStatefulWidgetState(); |
| } |
| |
| class _MyStatefulWidgetState extends State<MyStatefulWidget> { |
| int _count = 0; |
| |
| @override |
| Widget build(BuildContext context) { |
| return Container( |
| color: Colors.white, |
| child: Column( |
| mainAxisAlignment: MainAxisAlignment.center, |
| children: <Widget>[ |
| AnimatedSwitcher( |
| duration: const Duration(milliseconds: 500), |
| transitionBuilder: (Widget child, Animation<double> animation) { |
| return ScaleTransition(scale: animation, child: child); |
| }, |
| child: Text( |
| '$_count', |
| // This key causes the AnimatedSwitcher to interpret this as a "new" |
| // child each time the count changes, so that it will begin its animation |
| // when the count changes. |
| key: ValueKey<int>(_count), |
| style: Theme.of(context).textTheme.headline4, |
| ), |
| ), |
| ElevatedButton( |
| child: const Text('Increment'), |
| onPressed: () { |
| setState(() { |
| _count += 1; |
| }); |
| }, |
| ), |
| ], |
| ), |
| ); |
| } |
| } |