mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Landing beta changes in master for the new stable release (#747)
This commit is contained in:
@@ -31,7 +31,11 @@ class Demo {
|
||||
final String route;
|
||||
final WidgetBuilder builder;
|
||||
|
||||
const Demo({this.name, this.route, this.builder});
|
||||
const Demo({
|
||||
required this.name,
|
||||
required this.route,
|
||||
required this.builder,
|
||||
});
|
||||
}
|
||||
|
||||
final basicDemos = [
|
||||
|
||||
@@ -18,9 +18,9 @@ class AnimatedContainerDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AnimatedContainerDemoState extends State<AnimatedContainerDemo> {
|
||||
Color color;
|
||||
double borderRadius;
|
||||
double margin;
|
||||
late Color color;
|
||||
late double borderRadius;
|
||||
late double margin;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -20,7 +20,7 @@ class _AnimationControllerDemoState extends State<AnimationControllerDemo>
|
||||
// Widget is not visible.
|
||||
|
||||
static const Duration _duration = Duration(seconds: 1);
|
||||
AnimationController controller;
|
||||
late final AnimationController controller;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -15,8 +15,8 @@ class _TweenDemoState extends State<TweenDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const Duration _duration = Duration(seconds: 1);
|
||||
static const double accountBalance = 1000000;
|
||||
AnimationController controller;
|
||||
Animation<double> animation;
|
||||
late final AnimationController controller;
|
||||
late final Animation<double> animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -16,8 +16,8 @@ class _AnimatedBuilderDemoState extends State<AnimatedBuilderDemo>
|
||||
static const Color beginColor = Colors.deepPurple;
|
||||
static const Color endColor = Colors.deepOrange;
|
||||
Duration duration = Duration(milliseconds: 800);
|
||||
AnimationController controller;
|
||||
Animation<Color> animation;
|
||||
late AnimationController controller;
|
||||
late Animation<Color?> animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TypewriterTween extends Tween<String> {
|
||||
TypewriterTween({String begin = '', String end})
|
||||
TypewriterTween({String begin = '', String end = ''})
|
||||
: super(begin: begin, end: end);
|
||||
|
||||
@override
|
||||
String lerp(double t) {
|
||||
var cutoff = (end.length * t).round();
|
||||
return end.substring(0, cutoff);
|
||||
var cutoff = (end!.length * t).round();
|
||||
return end!.substring(0, cutoff);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ class _CustomTweenDemoState extends State<CustomTweenDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const Duration _duration = Duration(seconds: 3);
|
||||
static const String message = loremIpsum;
|
||||
AnimationController controller;
|
||||
Animation<String> animation;
|
||||
late final AnimationController controller;
|
||||
late final Animation<String> animation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -14,8 +14,8 @@ class TweenSequenceDemo extends StatefulWidget {
|
||||
class _TweenSequenceDemoState extends State<TweenSequenceDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
static const Duration duration = Duration(seconds: 3);
|
||||
AnimationController controller;
|
||||
Animation<Color> animation;
|
||||
late final AnimationController controller;
|
||||
late final Animation<Color?> animation;
|
||||
|
||||
static final colors = [
|
||||
Colors.red,
|
||||
@@ -31,7 +31,7 @@ class _TweenSequenceDemoState extends State<TweenSequenceDemo>
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
final sequenceItems = <TweenSequenceItem<Color>>[];
|
||||
final sequenceItems = <TweenSequenceItem<Color?>>[];
|
||||
|
||||
for (var i = 0; i < colors.length; i++) {
|
||||
final beginColor = colors[i];
|
||||
@@ -39,7 +39,7 @@ class _TweenSequenceDemoState extends State<TweenSequenceDemo>
|
||||
final weight = 1 / colors.length;
|
||||
|
||||
sequenceItems.add(
|
||||
TweenSequenceItem<Color>(
|
||||
TweenSequenceItem<Color?>(
|
||||
tween: ColorTween(begin: beginColor, end: endColor),
|
||||
weight: weight,
|
||||
),
|
||||
@@ -47,7 +47,7 @@ class _TweenSequenceDemoState extends State<TweenSequenceDemo>
|
||||
}
|
||||
|
||||
controller = AnimationController(duration: duration, vsync: this);
|
||||
animation = TweenSequence<Color>(sequenceItems).animate(controller);
|
||||
animation = TweenSequence<Color?>(sequenceItems).animate(controller);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -15,9 +15,9 @@ class FadeTransitionDemo extends StatefulWidget {
|
||||
|
||||
class _FadeTransitionDemoState extends State<FadeTransitionDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
Animation<double> _animation;
|
||||
CurvedAnimation _curve;
|
||||
late final AnimationController _controller;
|
||||
late final Animation<double> _animation;
|
||||
late final CurvedAnimation _curve;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
@@ -14,23 +14,31 @@ class AnimatedListDemo extends StatefulWidget {
|
||||
|
||||
class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
||||
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
|
||||
final listData = initialListData;
|
||||
final listData = [
|
||||
UserModel(0, 'Govind', 'Dixit'),
|
||||
UserModel(1, 'Greta', 'Stoll'),
|
||||
UserModel(2, 'Monty', 'Carlo'),
|
||||
UserModel(3, 'Petey', 'Cruiser'),
|
||||
UserModel(4, 'Barry', 'Cade'),
|
||||
];
|
||||
final initialListSize = 5;
|
||||
|
||||
void addUser() {
|
||||
setState(() {
|
||||
var index = listData.length;
|
||||
listData.add(
|
||||
UserModel(firstName: 'New', lastName: 'Person'),
|
||||
UserModel(++_maxIdValue, 'New', 'Person'),
|
||||
);
|
||||
_listKey.currentState
|
||||
_listKey.currentState!
|
||||
.insertItem(index, duration: Duration(milliseconds: 300));
|
||||
});
|
||||
}
|
||||
|
||||
void deleteUser(int index) {
|
||||
void deleteUser(int id) {
|
||||
setState(() {
|
||||
final index = listData.indexWhere((u) => u.id == id);
|
||||
var user = listData.removeAt(index);
|
||||
_listKey.currentState.removeItem(
|
||||
_listKey.currentState!.removeItem(
|
||||
index,
|
||||
(context, animation) {
|
||||
return FadeTransition(
|
||||
@@ -49,7 +57,7 @@ class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildItem(UserModel user, [int index]) {
|
||||
Widget _buildItem(UserModel user) {
|
||||
return ListTile(
|
||||
key: ValueKey<UserModel>(user),
|
||||
title: Text(user.firstName),
|
||||
@@ -59,7 +67,7 @@ class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: Icon(Icons.delete),
|
||||
onPressed: () => deleteUser(index),
|
||||
onPressed: () => deleteUser(user.id),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -79,11 +87,11 @@ class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
||||
body: SafeArea(
|
||||
child: AnimatedList(
|
||||
key: _listKey,
|
||||
initialItemCount: initialListData.length,
|
||||
initialItemCount: 5,
|
||||
itemBuilder: (context, index, animation) {
|
||||
return FadeTransition(
|
||||
opacity: animation,
|
||||
child: _buildItem(listData[index], index),
|
||||
child: _buildItem(listData[index]),
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -93,31 +101,15 @@ class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
||||
}
|
||||
|
||||
class UserModel {
|
||||
const UserModel({this.firstName, this.lastName});
|
||||
UserModel(
|
||||
this.id,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
);
|
||||
|
||||
final int id;
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
}
|
||||
|
||||
List<UserModel> initialListData = [
|
||||
UserModel(
|
||||
firstName: 'Govind',
|
||||
lastName: 'Dixit',
|
||||
),
|
||||
UserModel(
|
||||
firstName: 'Greta',
|
||||
lastName: 'Stoll',
|
||||
),
|
||||
UserModel(
|
||||
firstName: 'Monty',
|
||||
lastName: 'Carlo',
|
||||
),
|
||||
UserModel(
|
||||
firstName: 'Petey',
|
||||
lastName: 'Cruiser',
|
||||
),
|
||||
UserModel(
|
||||
firstName: 'Barry',
|
||||
lastName: 'Cade',
|
||||
),
|
||||
];
|
||||
int _maxIdValue = 4;
|
||||
|
||||
@@ -14,8 +14,8 @@ class AnimatedPositionedDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AnimatedPositionedDemoState extends State<AnimatedPositionedDemo> {
|
||||
double topPosition;
|
||||
double leftPosition;
|
||||
late double topPosition;
|
||||
late double leftPosition;
|
||||
|
||||
double generateTopPosition(double top) => Random().nextDouble() * top;
|
||||
|
||||
@@ -41,40 +41,38 @@ class _AnimatedPositionedDemoState extends State<AnimatedPositionedDemo> {
|
||||
final appBar = AppBar(title: Text('AnimatedPositioned'));
|
||||
final topPadding = MediaQuery.of(context).padding.top;
|
||||
// AnimatedPositioned animates changes to a widget's position within a Stack
|
||||
return SafeArea(
|
||||
child: Scaffold(
|
||||
appBar: appBar,
|
||||
body: Container(
|
||||
height: size.height,
|
||||
width: size.width,
|
||||
child: Stack(
|
||||
children: [
|
||||
AnimatedPositioned(
|
||||
top: topPosition,
|
||||
left: leftPosition,
|
||||
duration: Duration(seconds: 1),
|
||||
child: InkWell(
|
||||
onTap: () => changePosition(
|
||||
size.height -
|
||||
(appBar.preferredSize.height + topPadding + 50),
|
||||
size.width - 150),
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
width: 150,
|
||||
height: 50,
|
||||
child: Text(
|
||||
'Click Me',
|
||||
style: TextStyle(
|
||||
color:
|
||||
Theme.of(context).buttonTheme.colorScheme.onPrimary,
|
||||
),
|
||||
return Scaffold(
|
||||
appBar: appBar,
|
||||
body: Container(
|
||||
height: size.height,
|
||||
width: size.width,
|
||||
child: Stack(
|
||||
children: [
|
||||
AnimatedPositioned(
|
||||
top: topPosition,
|
||||
left: leftPosition,
|
||||
duration: Duration(seconds: 1),
|
||||
child: InkWell(
|
||||
onTap: () => changePosition(
|
||||
size.height -
|
||||
(appBar.preferredSize.height + topPadding + 50),
|
||||
size.width - 150),
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
width: 150,
|
||||
height: 50,
|
||||
child: Text(
|
||||
'Click Me',
|
||||
style: TextStyle(
|
||||
color:
|
||||
Theme.of(context).buttonTheme.colorScheme!.onPrimary,
|
||||
),
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -30,8 +30,8 @@ class AnimatedSwitcherDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _AnimatedSwitcherDemoState extends State<AnimatedSwitcherDemo> {
|
||||
Widget container;
|
||||
int keyCount;
|
||||
late Widget container;
|
||||
late int keyCount;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -53,7 +53,7 @@ class _AnimatedSwitcherDemoState extends State<AnimatedSwitcherDemo> {
|
||||
child: Text(
|
||||
'Change Widget',
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).buttonTheme.colorScheme.onPrimary),
|
||||
color: Theme.of(context).buttonTheme.colorScheme!.onPrimary),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -15,7 +15,7 @@ class CardSwipeDemo extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _CardSwipeDemoState extends State<CardSwipeDemo> {
|
||||
List<String> fileNames;
|
||||
late List<String> fileNames;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -102,8 +102,8 @@ class SwipeableCard extends StatefulWidget {
|
||||
final VoidCallback onSwiped;
|
||||
|
||||
SwipeableCard({
|
||||
this.onSwiped,
|
||||
this.imageAssetName,
|
||||
required this.onSwiped,
|
||||
required this.imageAssetName,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -112,9 +112,9 @@ class SwipeableCard extends StatefulWidget {
|
||||
|
||||
class _SwipeableCardState extends State<SwipeableCard>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
Animation<Offset> _animation;
|
||||
double _dragStartX;
|
||||
late AnimationController _controller;
|
||||
late Animation<Offset> _animation;
|
||||
late double _dragStartX;
|
||||
bool _isSwipingLeft = false;
|
||||
|
||||
@override
|
||||
@@ -155,18 +155,29 @@ class _SwipeableCardState extends State<SwipeableCard>
|
||||
}
|
||||
|
||||
setState(() {
|
||||
final size = context.size;
|
||||
|
||||
if (size == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate the amount dragged in unit coordinates (between 0 and 1)
|
||||
// using this widgets width.
|
||||
_controller.value =
|
||||
(details.localPosition.dx - _dragStartX).abs() / context.size.width;
|
||||
(details.localPosition.dx - _dragStartX).abs() / size.width;
|
||||
});
|
||||
}
|
||||
|
||||
/// Runs the fling / spring animation using the final velocity of the drag
|
||||
/// gesture.
|
||||
void _dragEnd(DragEndDetails details) {
|
||||
var velocity =
|
||||
(details.velocity.pixelsPerSecond.dx / context.size.width).abs();
|
||||
final size = context.size;
|
||||
|
||||
if (size == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var velocity = (details.velocity.pixelsPerSecond.dx / size.width).abs();
|
||||
_animate(velocity: velocity);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,15 +46,15 @@ typedef OnCurrentItemChangedCallback = void Function(int currentItem);
|
||||
class Carousel extends StatefulWidget {
|
||||
final IndexedWidgetBuilder itemBuilder;
|
||||
|
||||
const Carousel({Key key, @required this.itemBuilder});
|
||||
const Carousel({Key? key, required this.itemBuilder}) : super(key: key);
|
||||
|
||||
@override
|
||||
_CarouselState createState() => _CarouselState();
|
||||
}
|
||||
|
||||
class _CarouselState extends State<Carousel> {
|
||||
PageController _controller;
|
||||
int _currentPage;
|
||||
late final PageController _controller;
|
||||
late int _currentPage;
|
||||
bool _pageHasChanged = false;
|
||||
|
||||
@override
|
||||
@@ -81,11 +81,11 @@ class _CarouselState extends State<Carousel> {
|
||||
itemBuilder: (context, index) => AnimatedBuilder(
|
||||
animation: _controller,
|
||||
builder: (context, child) {
|
||||
var result = _pageHasChanged ? _controller.page : _currentPage * 1.0;
|
||||
var result = _pageHasChanged ? _controller.page! : _currentPage * 1.0;
|
||||
|
||||
// The horizontal position of the page between a 1 and 0
|
||||
var value = result - index;
|
||||
value = (1 - (value.abs() * .5)).clamp(0.0, 1.0) as double;
|
||||
value = (1 - (value.abs() * .5)).clamp(0.0, 1.0);
|
||||
|
||||
return Center(
|
||||
child: SizedBox(
|
||||
|
||||
@@ -15,14 +15,15 @@ class CurvedAnimationDemo extends StatefulWidget {
|
||||
class CurveChoice {
|
||||
final Curve curve;
|
||||
final String name;
|
||||
const CurveChoice({this.curve, this.name});
|
||||
|
||||
const CurveChoice({required this.curve, required this.name});
|
||||
}
|
||||
|
||||
class _CurvedAnimationDemoState extends State<CurvedAnimationDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController controller;
|
||||
Animation<double> animationRotation;
|
||||
Animation<Offset> animationTranslation;
|
||||
late final AnimationController controller;
|
||||
late final Animation<double> animationRotation;
|
||||
late final Animation<Offset> animationTranslation;
|
||||
static const _duration = Duration(seconds: 4);
|
||||
List<CurveChoice> curves = [
|
||||
CurveChoice(curve: Curves.bounceIn, name: 'Bounce In'),
|
||||
@@ -38,8 +39,8 @@ class _CurvedAnimationDemoState extends State<CurvedAnimationDemo>
|
||||
CurveChoice(curve: Curves.easeInCirc, name: 'Ease In Circle'),
|
||||
CurveChoice(curve: Curves.easeOutCirc, name: 'Ease Out Circle'),
|
||||
];
|
||||
CurveChoice selectedForwardCurve, selectedReverseCurve;
|
||||
CurvedAnimation curvedAnimation;
|
||||
late CurveChoice selectedForwardCurve, selectedReverseCurve;
|
||||
late final CurvedAnimation curvedAnimation;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -100,10 +101,12 @@ class _CurvedAnimationDemoState extends State<CurvedAnimationDemo>
|
||||
value: curve, child: Text(curve.name));
|
||||
}).toList(),
|
||||
onChanged: (newCurve) {
|
||||
setState(() {
|
||||
selectedForwardCurve = newCurve;
|
||||
curvedAnimation.curve = selectedForwardCurve.curve;
|
||||
});
|
||||
if (newCurve != null) {
|
||||
setState(() {
|
||||
selectedForwardCurve = newCurve;
|
||||
curvedAnimation.curve = selectedForwardCurve.curve;
|
||||
});
|
||||
}
|
||||
},
|
||||
value: selectedForwardCurve,
|
||||
),
|
||||
@@ -118,10 +121,12 @@ class _CurvedAnimationDemoState extends State<CurvedAnimationDemo>
|
||||
value: curve, child: Text(curve.name));
|
||||
}).toList(),
|
||||
onChanged: (newCurve) {
|
||||
setState(() {
|
||||
selectedReverseCurve = newCurve;
|
||||
curvedAnimation.reverseCurve = selectedReverseCurve.curve;
|
||||
});
|
||||
if (newCurve != null) {
|
||||
setState(() {
|
||||
selectedReverseCurve = newCurve;
|
||||
curvedAnimation.reverseCurve = selectedReverseCurve.curve;
|
||||
});
|
||||
}
|
||||
},
|
||||
value: selectedReverseCurve,
|
||||
),
|
||||
|
||||
@@ -18,7 +18,7 @@ class HeroAnimationDemo extends StatelessWidget {
|
||||
tag: 'hero-page-child',
|
||||
child: _createHeroContainer(
|
||||
size: 50.0,
|
||||
color: Colors.grey[300],
|
||||
color: Colors.grey.shade300,
|
||||
),
|
||||
),
|
||||
onTap: () => Navigator.of(context)
|
||||
@@ -47,7 +47,10 @@ class HeroPage extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
StatelessWidget _createHeroContainer({double size, Color color}) {
|
||||
StatelessWidget _createHeroContainer({
|
||||
required double size,
|
||||
required Color color,
|
||||
}) {
|
||||
return Container(
|
||||
height: size,
|
||||
width: size,
|
||||
|
||||
@@ -27,7 +27,7 @@ class PhysicsCardDragDemo extends StatelessWidget {
|
||||
/// released.
|
||||
class DraggableCard extends StatefulWidget {
|
||||
final Widget child;
|
||||
DraggableCard({this.child});
|
||||
DraggableCard({required this.child});
|
||||
|
||||
@override
|
||||
_DraggableCardState createState() => _DraggableCardState();
|
||||
@@ -35,7 +35,7 @@ class DraggableCard extends StatefulWidget {
|
||||
|
||||
class _DraggableCardState extends State<DraggableCard>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
late final AnimationController _controller;
|
||||
|
||||
/// The alignment of the card as it is dragged or being animated.
|
||||
///
|
||||
@@ -44,7 +44,7 @@ class _DraggableCardState extends State<DraggableCard>
|
||||
/// this value is set to the value of the [_animation].
|
||||
var _dragAlignment = Alignment.center;
|
||||
|
||||
Animation<Alignment> _animation;
|
||||
late Animation<Alignment> _animation;
|
||||
|
||||
final _spring = const SpringDescription(
|
||||
mass: 10,
|
||||
|
||||
@@ -13,8 +13,8 @@ class RepeatingAnimationDemo extends StatefulWidget {
|
||||
|
||||
class RepeatingAnimationDemoState extends State<RepeatingAnimationDemo>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController _controller;
|
||||
Animation<BorderRadius> _borderRadius;
|
||||
late final AnimationController _controller;
|
||||
late final Animation<BorderRadius> _borderRadius;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
||||
Reference in New Issue
Block a user