Skip to content

Instantly share code, notes, and snippets.

@Pregum
Created October 2, 2019 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pregum/7fe5ffd50bb1d6f7fea07a2cfed5298e to your computer and use it in GitHub Desktop.
Save Pregum/7fe5ffd50bb1d6f7fea07a2cfed5298e to your computer and use it in GitHub Desktop.
パタパタ時計サンプル
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Clock',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Clock'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
final PublishSubject<DateTime> _subject = PublishSubject<DateTime>();
@override
void initState() {
super.initState();
_subject.sink.add(DateTime.now());
_timer = Timer.periodic(
Duration(milliseconds: 1000),
(time) {
setState(() => _subject.sink.add(DateTime.now()));
print('now: ${DateTime.now()}');
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('ぱたぱた時計', style: TextStyle(fontSize: 32)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation: 0.8,
color: Colors.blueGrey,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: FlipWidget(subject: _subject, func: (dt) => dt.minute ~/ 10),
),
Padding(
padding: const EdgeInsets.all(20),
child: FlipWidget(subject: _subject, func: (dt) => dt.minute % 10),
),
Padding(
padding: const EdgeInsets.only(top: 50.0, left: 15.0),
child: Text(
'分',
style: TextStyle(fontSize: 50),
),
),
],
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
elevation: 0.8,
color: Colors.grey,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: FlipWidget(subject: _subject, func: (dt) => dt.second ~/ 10),
),
Padding(
padding: const EdgeInsets.all(20),
child: FlipWidget(
subject: _subject, func: (dt) => dt.second % 10),
),
Padding(
padding: const EdgeInsets.only(top: 50.0, left: 20.0),
child: Text(
'秒',
style: TextStyle(fontSize: 50),
),
)
],
),
),
),
],
),
);
}
}
typedef TResult Func<T, TResult>(T source);
class FlipWidget extends StatefulWidget {
final Func<DateTime, int> func;
final PublishSubject<DateTime> subject;
FlipWidget({Key key, this.subject, this.func}) : super(key: key);
@override
_FlipWidgetState createState() => _FlipWidgetState();
}
class _FlipWidgetState extends State<FlipWidget>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation _halfTopFlipAnimation;
Animation _halfBottomFlipAnimation;
Stream<DateTime> stream;
Widget _child1;
Widget _backCard;
int _oldTime = 0;
int _newTime = 0;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
);
// 上半分の板を0からpi / 2.0まで回転させるAnimation
_halfTopFlipAnimation = Tween<double>(begin: 0.0, end: pi / 2.0).animate(
CurvedAnimation(
parent: _animationController,
curve: Interval(0.0, 0.5, curve: Curves.linear),
),
);
// 下半分の板を-pi / 2.0から0まで回転させるAnimation
_halfBottomFlipAnimation = Tween<double>(begin: 0.0, end: pi / 2.0).animate(
CurvedAnimation(
parent: _animationController,
curve: Interval(0.5, 1.0, curve: Curves.linear),
),
);
_child1 = _buildFlipCard('$_oldTime');
_backCard = _buildFlipCard('$_newTime');
// streamの設定
if (this.widget?.subject?.stream == null) {
print('stream is null');
return;
}
if (this.widget?.func == null) {
print('func is null');
return;
}
this
.widget
.subject
.stream
.where((dt) => this.widget.func(dt) != _newTime)
.listen(
(time) {
// print('stream is alive.');
setState(() {
_oldTime = _newTime;
_newTime = this.widget.func(time);
_child1 = _buildFlipCard('$_oldTime');
_backCard = _buildFlipCard('$_newTime');
_animationController.reset();
_animationController.forward();
});
},
);
}
Widget _buildFlipCard(String text) {
return Container(
color: Colors.yellow,
width: 90,
height: 130,
child: Center(
child: Text(
text,
style: TextStyle(fontSize: 100, fontWeight: FontWeight.bold),
),
),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animationController,
builder: (BuildContext context, Widget widget) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
ClipRect(
child: Align(
alignment: Alignment.topCenter,
child: _backCard,
heightFactor: 0.5,
),
),
Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.006)
..rotateX(_halfTopFlipAnimation.value),
alignment: Alignment.bottomCenter,
child: ClipRect(
child: Align(
alignment: Alignment.topCenter,
heightFactor: 0.5,
child: _child1,
),
),
),
],
),
Padding(
padding: EdgeInsets.only(top: 2.0),
),
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
ClipRect(
child: Align(
alignment: Alignment.bottomCenter,
heightFactor: 0.5,
child: _child1,
),
),
Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.006)
..rotateX(-pi / 2.0 + _halfBottomFlipAnimation.value),
alignment: Alignment.topCenter,
child: ClipRect(
child: Align(
alignment: Alignment.bottomCenter,
heightFactor: 0.5,
child: _backCard,
),
),
),
],
),
],
);
},
);
}
}
name: animation_patapata_clock
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
rxdart: ^0.22.2
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment