Hero: Add an example for createRectTween (#102650)
This commit is contained in:
@@ -6,29 +6,26 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() => runApp(const HeroApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
class HeroApp extends StatelessWidget {
|
||||
const HeroApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: _title,
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
appBar: AppBar(title: const Text('Hero Sample')),
|
||||
body: const Center(
|
||||
child: MyStatelessWidget(),
|
||||
child: HeroExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyStatelessWidget extends StatelessWidget {
|
||||
const MyStatelessWidget({Key? key}) : super(key: key);
|
||||
class HeroExample extends StatelessWidget {
|
||||
const HeroExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -41,17 +38,18 @@ class MyStatelessWidget extends StatelessWidget {
|
||||
ListTile(
|
||||
leading: Hero(
|
||||
tag: 'hero-rectangle',
|
||||
child: _blueRectangle(const Size(50, 50)),
|
||||
child: _box(const Size(50, 50)),
|
||||
),
|
||||
onTap: () => _gotoDetailsPage(context),
|
||||
title:
|
||||
const Text('Tap on the icon to view hero animation transition.'),
|
||||
title: const Text(
|
||||
'Tap on the icon to view hero animation transition.',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _blueRectangle(Size size) {
|
||||
Widget _box(Size size) {
|
||||
return Container(
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
@@ -63,7 +61,7 @@ class MyStatelessWidget extends StatelessWidget {
|
||||
Navigator.of(context).push(MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('second Page'),
|
||||
title: const Text('Second Page'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
@@ -71,7 +69,7 @@ class MyStatelessWidget extends StatelessWidget {
|
||||
children: <Widget>[
|
||||
Hero(
|
||||
tag: 'hero-rectangle',
|
||||
child: _blueRectangle(const Size(200, 200)),
|
||||
child: _box(const Size(200, 200)),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
110
examples/api/lib/widgets/heroes/hero.1.dart
Normal file
110
examples/api/lib/widgets/heroes/hero.1.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
// 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 {
|
||||
const HeroApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Hero Sample')),
|
||||
body: const Center(
|
||||
child: HeroExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class HeroExample extends StatelessWidget {
|
||||
const HeroExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: <Widget>[
|
||||
ListTile(
|
||||
leading: Hero(
|
||||
tag: 'hero-default-tween',
|
||||
child: _box(size: 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.',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10.0),
|
||||
ListTile(
|
||||
leading: Hero(
|
||||
tag: 'hero-custom-tween',
|
||||
createRectTween: (Rect? begin, Rect? end) {
|
||||
return MaterialRectCenterArcTween(begin: begin, end: end);
|
||||
},
|
||||
child: _box(size: 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.',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton(
|
||||
onPressed: () => _gotoDetailsPage(context),
|
||||
child: const Text('Tap to trigger hero flight'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _box({double? size, Color? color}) {
|
||||
return Container(
|
||||
color: color,
|
||||
child: FlutterLogo(size: size),
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
},
|
||||
child: _box(
|
||||
size: 400.0,
|
||||
color: Colors.blue[700]!.withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
Hero(
|
||||
tag: 'hero-default-tween',
|
||||
child: _box(
|
||||
size: 400.0,
|
||||
color: Colors.red[700]!.withOpacity(0.5),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user