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.
41 lines
1.1 KiB
41 lines
1.1 KiB
// Copyright 2023 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.
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
/// TickingBuilder is for ambient animation to be run
|
|
/// on each frame.
|
|
class TickingBuilder extends StatefulWidget {
|
|
const TickingBuilder({super.key, required this.builder});
|
|
final Widget Function(BuildContext context, double time) builder;
|
|
@override
|
|
State<TickingBuilder> createState() => _TickingBuilderState();
|
|
}
|
|
|
|
class _TickingBuilderState extends State<TickingBuilder>
|
|
with SingleTickerProviderStateMixin {
|
|
late final Ticker _ticker;
|
|
double _time = 0.0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_ticker = createTicker(_handleTick)..start();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_ticker.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _handleTick(Duration elapsed) {
|
|
setState(() => _time = elapsed.inMilliseconds.toDouble() / 1000.0);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.builder.call(context, _time);
|
|
}
|