mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.6 KiB
79 lines
2.6 KiB
// Copyright 2019 The Flutter team. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
class AnimatedBuilderDemo extends StatefulWidget {
|
|
const AnimatedBuilderDemo({super.key});
|
|
static const String routeName = 'basics/animated_builder';
|
|
|
|
@override
|
|
State<AnimatedBuilderDemo> createState() => _AnimatedBuilderDemoState();
|
|
}
|
|
|
|
class _AnimatedBuilderDemoState extends State<AnimatedBuilderDemo>
|
|
with SingleTickerProviderStateMixin {
|
|
static const Color beginColor = Colors.deepPurple;
|
|
static const Color endColor = Colors.deepOrange;
|
|
Duration duration = const Duration(milliseconds: 800);
|
|
late AnimationController controller;
|
|
late Animation<Color?> animation;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller = AnimationController(vsync: this, duration: duration);
|
|
animation = ColorTween(
|
|
begin: beginColor,
|
|
end: endColor,
|
|
).animate(controller);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('AnimatedBuilder')),
|
|
body: Center(
|
|
// AnimatedBuilder handles listening to a given animation and calling the builder
|
|
// whenever the value of the animation change. This can be useful when a Widget
|
|
// tree contains some animated and non-animated elements, as only the subtree
|
|
// created by the builder needs to be re-built when the animation changes.
|
|
child: AnimatedBuilder(
|
|
animation: animation,
|
|
builder: (context, child) {
|
|
return ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: animation.value,
|
|
),
|
|
child: child,
|
|
onPressed: () {
|
|
switch (controller.status) {
|
|
case AnimationStatus.completed:
|
|
controller.reverse();
|
|
default:
|
|
controller.forward();
|
|
}
|
|
},
|
|
);
|
|
},
|
|
// AnimatedBuilder can also accept a pre-built child Widget which is useful
|
|
// if there is a non-animated Widget contained within the animated widget.
|
|
// This can improve performance since this widget doesn't need to be rebuilt
|
|
// when the animation changes.
|
|
child: const Text(
|
|
'Change Color',
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|