Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 1 | // 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 5 | import 'package:flutter/material.dart'; |
| 6 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 7 | /// Flutter code sample for [Hero]. |
| 8 | |
Taha Tesser | eef4aa7 | 2022-04-29 22:39:09 +0300 | [diff] [blame] | 9 | void main() => runApp(const HeroApp()); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 10 | |
Taha Tesser | eef4aa7 | 2022-04-29 22:39:09 +0300 | [diff] [blame] | 11 | class HeroApp extends StatelessWidget { |
Michael Goderbauer | 2cbad4b | 2022-05-17 15:35:07 -0700 | [diff] [blame] | 12 | const HeroApp({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 13 | |
| 14 | @override |
| 15 | Widget build(BuildContext context) { |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 16 | return const MaterialApp( |
| 17 | home: HeroExample(), |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 18 | ); |
| 19 | } |
| 20 | } |
| 21 | |
Taha Tesser | eef4aa7 | 2022-04-29 22:39:09 +0300 | [diff] [blame] | 22 | class HeroExample extends StatelessWidget { |
Michael Goderbauer | 2cbad4b | 2022-05-17 15:35:07 -0700 | [diff] [blame] | 23 | const HeroExample({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 24 | |
| 25 | @override |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 26 | Widget build(BuildContext context) { |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 27 | 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 42 | ), |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 43 | ], |
| 44 | ), |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 45 | ); |
| 46 | } |
| 47 | |
| 48 | void _gotoDetailsPage(BuildContext context) { |
| 49 | Navigator.of(context).push(MaterialPageRoute<void>( |
| 50 | builder: (BuildContext context) => Scaffold( |
| 51 | appBar: AppBar( |
Taha Tesser | eef4aa7 | 2022-04-29 22:39:09 +0300 | [diff] [blame] | 52 | title: const Text('Second Page'), |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 53 | ), |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 54 | body: const Center( |
| 55 | child: Hero( |
| 56 | tag: 'hero-rectangle', |
| 57 | child: BoxWidget(size: Size(200.0, 200.0)), |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | )); |
| 62 | } |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 63 | } |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 64 | |
| 65 | class BoxWidget extends StatelessWidget { |
Michael Goderbauer | 2cbad4b | 2022-05-17 15:35:07 -0700 | [diff] [blame] | 66 | const BoxWidget({super.key, required this.size}); |
Taha Tesser | cd6b2a3 | 2022-05-06 09:39:10 +0300 | [diff] [blame] | 67 | |
| 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 | } |