hero.1.dart 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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 Hero

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

void main() {
  // Slow down time to see Hero flight animation.
  timeDilation = 15.0;
  runApp(const HeroApp());
}

class HeroApp extends StatelessWidget {
17
  const HeroApp({super.key});
18 19 20

  @override
  Widget build(BuildContext context) {
21 22
    return const MaterialApp(
      home: HeroExample(),
23 24 25 26 27
    );
  }
}

class HeroExample extends StatelessWidget {
28
  const HeroExample({super.key});
29 30 31

  @override
  Widget build(BuildContext context) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    return Scaffold(
      appBar: AppBar(title: const Text('Hero Sample')),
      body: Column(
        children: <Widget>[
          ListTile(
            leading: Hero(
              tag: 'hero-default-tween',
              child: BoxWidget(
                size: const Size(50.0, 50.0),
                color:Colors.red[700]!.withOpacity(0.5),
              ),
            ),
            title: const Text(
              'This red icon will use a default rect tween during the hero flight.',
            ),
47
          ),
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
          const SizedBox(height: 10.0),
          ListTile(
            leading: Hero(
              tag: 'hero-custom-tween',
              createRectTween: (Rect? begin, Rect? end) {
                return MaterialRectCenterArcTween(begin: begin, end: end);
              },
              child: BoxWidget(
                size: const Size(50.0, 50.0),
                color:Colors.blue[700]!.withOpacity(0.5),
              ),
            ),
            title: const Text(
              'This blue icon will use a custom rect tween during the hero flight.',
            ),
63
          ),
64 65 66 67
          const SizedBox(height: 10),
          ElevatedButton(
            onPressed: () => _gotoDetailsPage(context),
            child: const Text('Tap to trigger hero flight'),
68
          ),
69 70
        ],
      ),
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    );
  }

  void _gotoDetailsPage(BuildContext context) {
    Navigator.of(context).push(MaterialPageRoute<void>(
      builder: (BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('Second Page'),
        ),
        body: Align(
          alignment: Alignment.bottomRight,
          child: Stack(
            children: <Widget>[
              Hero(
                tag: 'hero-custom-tween',
                createRectTween: (Rect? begin, Rect? end) {
                  return MaterialRectCenterArcTween(begin: begin, end: end);
                },
89 90
                child: BoxWidget(
                  size: const Size(400.0, 400.0),
91 92 93 94 95
                  color: Colors.blue[700]!.withOpacity(0.5),
                ),
              ),
              Hero(
                tag: 'hero-default-tween',
96 97
                child: BoxWidget(
                  size: const Size(400.0, 400.0),
98 99 100 101 102 103 104 105 106 107
                  color: Colors.red[700]!.withOpacity(0.5),
                ),
              ),
            ],
          ),
        ),
      ),
    ));
  }
}
108 109 110

class BoxWidget extends StatelessWidget {
  const BoxWidget({
111
    super.key,
112 113
    required this.size,
    required this.color,
114
  });
115 116 117 118 119 120 121 122 123 124 125 126 127

  final Size size;
  final Color color;

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