hero.0.dart 1.86 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 8
/// Flutter code sample for [Hero].

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

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

  @override
  Widget build(BuildContext context) {
16 17
    return const MaterialApp(
      home: HeroExample(),
18 19 20 21
    );
  }
}

22
class HeroExample extends StatelessWidget {
23
  const HeroExample({super.key});
24 25 26

  @override
  Widget build(BuildContext context) {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    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.',
            ),
42
          ),
43 44
        ],
      ),
45 46 47 48 49 50 51
    );
  }

  void _gotoDetailsPage(BuildContext context) {
    Navigator.of(context).push(MaterialPageRoute<void>(
      builder: (BuildContext context) => Scaffold(
        appBar: AppBar(
52
          title: const Text('Second Page'),
53
        ),
54 55 56 57
        body: const Center(
          child: Hero(
            tag: 'hero-rectangle',
            child: BoxWidget(size: Size(200.0, 200.0)),
58 59 60 61 62 63
          ),
        ),
      ),
    ));
  }
}
64 65

class BoxWidget extends StatelessWidget {
66
  const BoxWidget({super.key, required this.size});
67 68 69 70 71 72 73 74 75 76 77 78

  final Size size;

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