blob: 2c6772ee288d102cd886cca103ed7c96b856754e [file] [log] [blame]
Greg Spencer33403bd2021-08-25 09:45:12 -07001// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Greg Spencer33403bd2021-08-25 09:45:12 -07005import 'package:flutter/material.dart';
6
Greg Spencere3bc8ef2023-04-04 13:34:29 -07007/// Flutter code sample for [Hero].
8
Taha Tessereef4aa72022-04-29 22:39:09 +03009void main() => runApp(const HeroApp());
Greg Spencer33403bd2021-08-25 09:45:12 -070010
Taha Tessereef4aa72022-04-29 22:39:09 +030011class HeroApp extends StatelessWidget {
Michael Goderbauer2cbad4b2022-05-17 15:35:07 -070012 const HeroApp({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070013
14 @override
15 Widget build(BuildContext context) {
Taha Tessercd6b2a32022-05-06 09:39:10 +030016 return const MaterialApp(
17 home: HeroExample(),
Greg Spencer33403bd2021-08-25 09:45:12 -070018 );
19 }
20}
21
Taha Tessereef4aa72022-04-29 22:39:09 +030022class HeroExample extends StatelessWidget {
Michael Goderbauer2cbad4b2022-05-17 15:35:07 -070023 const HeroExample({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070024
25 @override
Greg Spencer33403bd2021-08-25 09:45:12 -070026 Widget build(BuildContext context) {
Taha Tessercd6b2a32022-05-06 09:39:10 +030027 return Scaffold(
28 appBar: AppBar(title: const Text('Hero Sample')),
29 body: Column(
30 crossAxisAlignment: CrossAxisAlignment.start,
31 children: <Widget>[
32 const SizedBox(height: 20.0),
33 ListTile(
34 leading: const Hero(
35 tag: 'hero-rectangle',
36 child: BoxWidget(size: Size(50.0, 50.0)),
37 ),
38 onTap: () => _gotoDetailsPage(context),
39 title: const Text(
40 'Tap on the icon to view hero animation transition.',
41 ),
Greg Spencer33403bd2021-08-25 09:45:12 -070042 ),
Taha Tessercd6b2a32022-05-06 09:39:10 +030043 ],
44 ),
Greg Spencer33403bd2021-08-25 09:45:12 -070045 );
46 }
47
48 void _gotoDetailsPage(BuildContext context) {
49 Navigator.of(context).push(MaterialPageRoute<void>(
50 builder: (BuildContext context) => Scaffold(
51 appBar: AppBar(
Taha Tessereef4aa72022-04-29 22:39:09 +030052 title: const Text('Second Page'),
Greg Spencer33403bd2021-08-25 09:45:12 -070053 ),
Taha Tessercd6b2a32022-05-06 09:39:10 +030054 body: const Center(
55 child: Hero(
56 tag: 'hero-rectangle',
57 child: BoxWidget(size: Size(200.0, 200.0)),
Greg Spencer33403bd2021-08-25 09:45:12 -070058 ),
59 ),
60 ),
61 ));
62 }
Greg Spencer33403bd2021-08-25 09:45:12 -070063}
Taha Tessercd6b2a32022-05-06 09:39:10 +030064
65class BoxWidget extends StatelessWidget {
Michael Goderbauer2cbad4b2022-05-17 15:35:07 -070066 const BoxWidget({super.key, required this.size});
Taha Tessercd6b2a32022-05-06 09:39:10 +030067
68 final Size size;
69
70 @override
71 Widget build(BuildContext context) {
72 return Container(
73 width: size.width,
74 height: size.height,
75 color: Colors.blue,
76 );
77 }
78}