animated_container.0.dart 1.56 KB
Newer Older
1 2 3 4 5 6
// 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.

import 'package:flutter/material.dart';

7
/// Flutter code sample for [AnimatedContainer].
8

9
void main() => runApp(const AnimatedContainerExampleApp());
10

11 12
class AnimatedContainerExampleApp extends StatelessWidget {
  const AnimatedContainerExampleApp({super.key});
13 14 15 16 17

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
18 19
        appBar: AppBar(title: const Text('AnimatedContainer Sample')),
        body: const AnimatedContainerExample(),
20 21 22 23 24
      ),
    );
  }
}

25 26
class AnimatedContainerExample extends StatefulWidget {
  const AnimatedContainerExample({super.key});
27 28

  @override
29
  State<AnimatedContainerExample> createState() => _AnimatedContainerExampleState();
30 31
}

32
class _AnimatedContainerExampleState extends State<AnimatedContainerExample> {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  bool selected = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          selected = !selected;
        });
      },
      child: Center(
        child: AnimatedContainer(
          width: selected ? 200.0 : 100.0,
          height: selected ? 100.0 : 200.0,
          color: selected ? Colors.red : Colors.blue,
48
          alignment: selected ? Alignment.center : AlignmentDirectional.topCenter,
49 50 51 52 53 54 55 56
          duration: const Duration(seconds: 2),
          curve: Curves.fastOutSlowIn,
          child: const FlutterLogo(size: 75),
        ),
      ),
    );
  }
}