[Flutter] Hero

Bumhyeon Baek·2023년 5월 8일
0

Flutter

목록 보기
4/14

Hero

자식을 Hero animation의 후보로 표시하는 위젯
양쪽 route에 공통적인 시각적 특징이 있는 경우 그 route가 전환되는 동안 그 특징이 한 페이지에서 다른 페이지로 물리적으로 이동하도록 사용자를
안내하는 것은 유용할 수 있다. 그런 애니메이션을 hero animation이라고 부른다. hero 위젯들은 전환하는 동안 네비게이터의 오버레이 안에서 "비행"하며 비행 중에는 기본적으로 기존 route와 새 route의 원래 위치에 표시되지 않는다.
동일한 태그가 있는 각 Hero 위젯 쌍에 대해 Hero 애니메이션이 트리거 된다. Route들은 각 태그에 대해 하나 이상의 Hero를 포함해서는 안된다.

import 'package:flutter/material.dart';

void main() => runApp(const HeroApp());

class HeroApp extends StatelessWidget {
  const HeroApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HeroExample(),
    );
  }
}

class HeroExample extends StatelessWidget {
  const HeroExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Hero Sample')),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          const SizedBox(height: 20.0),
          ListTile(
            leading: const Hero(
              tag: 'hero-rectangle',
              child: BoxWidget(size: Size(50.0, 50.0)),
            ),
            onTap: () => _gotoDetailsPage(context),
            title: const Text(
              'Tap on the icon to view hero animation transition.',
            ),
          ),
        ],
      ),
    );
  }

  void _gotoDetailsPage(BuildContext context) {
    Navigator.of(context).push(MaterialPageRoute<void>(
      builder: (BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('Second Page'),
        ),
        body: const Center(
          child: Hero(
            tag: 'hero-rectangle',
            child: BoxWidget(size: Size(200.0, 200.0)),
          ),
        ),
      ),
    ));
  }
}

class BoxWidget extends StatelessWidget {
  const BoxWidget({super.key, required this.size});

  final Size size;

  @override
  Widget build(BuildContext context) {
    return Container(
      width: size.width,
      height: size.height,
      color: Colors.blue,
    );
  }
}
profile
Cool Leader

0개의 댓글