Skip to content

Commit 8835ae6

Browse files
Cleanup and better README for Teddy
1 parent 6c4a2db commit 8835ae6

File tree

4 files changed

+44
-60
lines changed

4 files changed

+44
-60
lines changed

example/teddy/README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
1-
# teddy
1+
# Teddy
22

3-
A new Flutter project.
3+
<img align="right" src="https://i.imgur.com/hJU9Obt.gif" height="250">
44

5-
## Getting Started
5+
An example built using [JCToon's](https://www.2dimensions.com/a/JuanCarlos/files/flare/teddy/preview) Flare File as a custom UI component. <br/>
6+
Teddy will follow the cursor as you type or move it around.
67

7-
This project is a starting point for a Flutter application.
8+
## Overview
89

9-
A few resources to get you started if this is your first Flutter project:
10+
The basic idea is to use the `ctrl_face` node in JCToon's file to change the direction of Teddy's gaze, as it's shown here in the gif to the right.
1011

11-
- [Lab: Write your first Flutter app](https://flutter.io/docs/get-started/codelab)
12-
- [Cookbook: Useful Flutter samples](https://flutter.io/docs/cookbook)
12+
This is done by using [custom `FlareControls`](lib/teddy_controller.dart), available in `/lib/teddy_controller.dart`.
13+
14+
`FlareControls` is a custom implementation of the `FlareController` interface. <br/>The interface and can be found in [flare_actor.dart](../../lib/flare_actor.dart#L13-L17) and it has three methods:
15+
16+
```
17+
abstract class FlareController {
18+
void initialize(FlutterActorArtboard artboard);
19+
void setViewTransform(Mat2D viewTransform);
20+
bool advance(FlutterActorArtboard artboard, double elapsed);
21+
}
22+
```
23+
24+
<img align="right" src="https://i.imgur.com/WdjurVo.gif" width="300" />
25+
26+
An instance of `TeddyController` is passed to the `FlareActor` in [`/lib/main.dart`](lib/main.dart#L77). This ties the controller to this widget, and allows it to use the three overrides to perform custom actions:
27+
28+
```
29+
FlareActor(
30+
"assets/Teddy.flr",
31+
controller: _teddyController,
32+
[...]
33+
)
34+
```
35+
36+
In this example, `initialize()` will grab the reference to the `ctrl_face` node through the library call `artboard.getNode("ctrl_face")`.
37+
38+
Moreover, by [extending `FlareControls`](../../lib/flare_actor.dart#L462), `TeddyController` can take advantage of a concrete implementation of this interface:
39+
- `play(String animationName)`
40+
- `advance(double elapsed)` - a base implementation which advances and mixes multiple animations
1341

14-
For help getting started with Flutter, view our
15-
[online documentation](https://flutter.io/docs), which offers tutorials,
16-
samples, guidance on mobile development, and a full API reference.

example/teddy/lib/main.dart

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@ import 'package:teddy/tracking_text_input.dart';
88
void main() => runApp(MyApp());
99

1010
class MyApp extends StatelessWidget {
11-
// This widget is the root of your application.
1211
@override
1312
Widget build(BuildContext context) {
1413
return MaterialApp(
1514
title: 'Flutter Demo',
1615
theme: ThemeData(
17-
// This is the theme of your application.
18-
//
19-
// Try running your application with "flutter run". You'll see the
20-
// application has a blue toolbar. Then, without quitting the app, try
21-
// changing the primarySwatch below to Colors.green and then invoke
22-
// "hot reload" (press "r" in the console where you ran "flutter run",
23-
// or simply save your changes to "hot reload" in a Flutter IDE).
24-
// Notice that the counter didn't reset back to zero; the application
25-
// is not restarted.
2616
primarySwatch: Colors.blue,
2717
),
2818
home: MyHomePage(title: 'Flutter Demo Home Page'),
@@ -33,15 +23,6 @@ class MyApp extends StatelessWidget {
3323
class MyHomePage extends StatefulWidget {
3424
MyHomePage({Key key, this.title}) : super(key: key);
3525

36-
// This widget is the home page of your application. It is stateful, meaning
37-
// that it has a State object (defined below) that contains fields that affect
38-
// how it looks.
39-
40-
// This class is the configuration for the state. It holds the values (in this
41-
// case the title) provided by the parent (in this case the App widget) and
42-
// used by the build method of the State. Fields in a Widget subclass are
43-
// always marked "final".
44-
4526
final String title;
4627

4728
@override
@@ -58,12 +39,6 @@ class _MyHomePageState extends State<MyHomePage> {
5839

5940
@override
6041
Widget build(BuildContext context) {
61-
// This method is rerun every time setState is called, for instance as done
62-
// by the _incrementCounter method above.
63-
//
64-
// The Flutter framework has been optimized to make rerunning build methods
65-
// fast, so that you can just rebuild anything that needs updating rather
66-
// than having to individually change instances of widgets.
6742
EdgeInsets devicePadding = MediaQuery.of(context).padding;
6843

6944
return Scaffold(
@@ -101,11 +76,12 @@ class _MyHomePageState extends State<MyHomePage> {
10176
height: 200,
10277
child: FlareActor(
10378
"assets/Teddy.flr",
79+
controller: _teddyController,
10480
shouldClip: false,
10581
alignment: Alignment.center,
106-
fit: BoxFit.contain,
107-
controller: _teddyController,
108-
)),
82+
fit: BoxFit.contain
83+
)
84+
),
10985
Container(
11086
decoration: BoxDecoration(
11187
color: Colors.white,
@@ -118,12 +94,6 @@ class _MyHomePageState extends State<MyHomePage> {
11894
mainAxisAlignment: MainAxisAlignment.center,
11995
crossAxisAlignment: CrossAxisAlignment.center,
12096
children: <Widget>[
121-
// Row(
122-
// mainAxisAlignment: MainAxisAlignment.center,
123-
// children: <Widget>[
124-
// Lurker(this._fieldPosition, TextSpan(text: this._textValue))
125-
// ],
126-
// ),
12797
TrackingTextInput(
12898
label: "Email",
12999
hint: "What's your email address?",

example/teddy/lib/teddy_controller.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ class TeddyController extends FlareControls {
6969

7070
_faceControl.translation = frameTranslation;
7171

72-
//_originalPosition = Vec2D.clone(_faceControl.translation);
7372
return true;
7473
}
7574

75+
// Fetch references for the `ctrl_face` node and store a copy of its original translation.
7676
@override
7777
void initialize(FlutterActorArtboard artboard) {
7878
super.initialize(artboard);
@@ -88,11 +88,15 @@ class TeddyController extends FlareControls {
8888
play("idle");
8989
}
9090

91+
// Called every frame by the wrapping [FlareActor].
92+
// Keep updating the matrix that transforms Global-Flutter-coordinates into Flare-World-coordinates.
9193
@override
9294
void setViewTransform(Mat2D viewTransform) {
9395
Mat2D.invert(_globalToFlareWorld, viewTransform);
9496
}
9597

98+
// Transform the [Offset] into a [Vec2D].
99+
// If no caret is provided, lower the [_hasFocus] flag.
96100
void lookAt(Offset caret) {
97101
if (caret == null) {
98102
_hasFocus = false;

example/teddy/pubspec.yaml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
name: teddy
22
description: A new Flutter project.
33

4-
# The following defines the version and build number for your application.
5-
# A version number is three numbers separated by dots, like 1.2.43
6-
# followed by an optional build number separated by a +.
7-
# Both the version and the builder number may be overridden in flutter
8-
# build by specifying --build-name and --build-number, respectively.
9-
# Read more about versioning at semver.org.
104
version: 1.0.0+1
115

126
environment:
@@ -18,24 +12,15 @@ dependencies:
1812
flare_flutter:
1913
path: ../../
2014

21-
# The following adds the Cupertino Icons font to your application.
22-
# Use with the CupertinoIcons class for iOS style icons.
2315
cupertino_icons: ^0.1.2
2416

2517
dev_dependencies:
2618
flutter_test:
2719
sdk: flutter
2820

2921

30-
# For information on the generic Dart part of this file, see the
31-
# following page: https://www.dartlang.org/tools/pub/pubspec
32-
33-
# The following section is specific to Flutter.
3422
flutter:
3523

36-
# The following line ensures that the Material Icons font is
37-
# included with your application, so that you can use the icons in
38-
# the material Icons class.
3924
uses-material-design: true
4025
fonts:
4126
- family: RobotoMedium

0 commit comments

Comments
 (0)