Skip to content

Commit 0f3721f

Browse files
authored
Quarterly maintenance on isolate_example (#142)
1 parent 7a63156 commit 0f3721f

File tree

12 files changed

+243
-228
lines changed

12 files changed

+243
-228
lines changed

Diff for: MAINTENANCE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ match any new language/SDK features, etc.).
1010
| chrome-os-best-practices | | |
1111
| experimental | | |
1212
| flutter_maps_firestore | | |
13-
| isolate_example | | |
13+
| isolate_example | redbrogdon | 9/12/19 |
1414
| jsonexample | | |
1515
| material_studies/shrine | | |
1616
| place_tracker | | |

Diff for: isolate_example/.gitignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*.iws
1616
.idea/
1717

18-
# Visual Studio Code related
19-
.vscode/
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
2022

2123
# Flutter/Dart/Pub related
2224
**/doc/api/
@@ -59,6 +61,7 @@
5961
**/ios/Flutter/app.flx
6062
**/ios/Flutter/app.zip
6163
**/ios/Flutter/flutter_assets/
64+
**/ios/Flutter/flutter_export_environment.sh
6265
**/ios/ServiceDefinitions.json
6366
**/ios/Runner/GeneratedPluginRegistrant.*
6467

@@ -67,5 +70,4 @@
6770
!**/ios/**/default.mode2v3
6871
!**/ios/**/default.pbxuser
6972
!**/ios/**/default.perspectivev3
70-
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
71-
bash: parse_git_branch: command not found
73+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

Diff for: isolate_example/.metadata

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 2d2a1ffec95cc70a3218872a2cd3f8de4933c42f
8+
channel: stable
9+
10+
project_type: app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package dev.flutter.isolate_example
2+
3+
import android.os.Bundle
4+
5+
import io.flutter.app.FlutterActivity
6+
import io.flutter.plugins.GeneratedPluginRegistrant
7+
8+
class MainActivity: FlutterActivity() {
9+
override fun onCreate(savedInstanceState: Bundle?) {
10+
super.onCreate(savedInstanceState)
11+
GeneratedPluginRegistrant.registerWith(this)
12+
}
13+
}

Diff for: isolate_example/ios/Runner/AppDelegate.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import UIKit
2+
import Flutter
3+
4+
@UIApplicationMain
5+
@objc class AppDelegate: FlutterAppDelegate {
6+
override func application(
7+
_ application: UIApplication,
8+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
9+
) -> Bool {
10+
GeneratedPluginRegistrant.register(with: self)
11+
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
12+
}
13+
}

Diff for: isolate_example/ios/Runner/Runner-Bridging-Header.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#import "GeneratedPluginRegistrant.h"

Diff for: isolate_example/lib/main.dart

+20-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515
import 'package:flutter/material.dart';
1616

1717
import 'page_one.dart';
18-
import 'page_two.dart';
1918
import 'page_three.dart';
19+
import 'page_two.dart';
2020

21-
void main() => runApp(MaterialApp(home: StartApp()));
21+
void main() {
22+
runApp(
23+
MaterialApp(
24+
home: StartApp(),
25+
),
26+
);
27+
}
2228

2329
class StartApp extends StatelessWidget {
2430
@override
@@ -30,9 +36,18 @@ class StartApp extends StatelessWidget {
3036
appBar: AppBar(
3137
bottom: TabBar(
3238
tabs: [
33-
Tab(icon: Icon(Icons.flash_on), text: 'Performance'),
34-
Tab(icon: Icon(Icons.sync), text: 'Infinite Process'),
35-
Tab(icon: Icon(Icons.storage), text: 'Data Transfer'),
39+
Tab(
40+
icon: Icon(Icons.flash_on),
41+
text: 'Performance',
42+
),
43+
Tab(
44+
icon: Icon(Icons.sync),
45+
text: 'Infinite Process',
46+
),
47+
Tab(
48+
icon: Icon(Icons.storage),
49+
text: 'Data Transfer',
50+
),
3651
],
3752
),
3853
title: Text('Isolate Example'),

Diff for: isolate_example/lib/page_one.dart

+50-50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
import 'package:flutter/foundation.dart';
1616
import 'package:flutter/material.dart';
1717

18+
// Computes the nth number in the Fibonacci sequence.
19+
int fib(int n) {
20+
int number1 = n - 1;
21+
int number2 = n - 2;
22+
23+
if (n == 1) {
24+
return 0;
25+
} else if (n == 0) {
26+
return 1;
27+
} else {
28+
return (fib(number1) + fib(number2));
29+
}
30+
}
31+
1832
class PerformancePage extends StatefulWidget {
1933
@override
2034
_PerformancePageState createState() => _PerformancePageState();
@@ -65,7 +79,9 @@ class _PerformancePageState extends State<PerformancePage> {
6579
}
6680

6781
VoidCallback createMainIsolateCallBack(
68-
BuildContext context, AsyncSnapshot snapshot) {
82+
BuildContext context,
83+
AsyncSnapshot snapshot,
84+
) {
6985
if (snapshot.connectionState == ConnectionState.done) {
7086
return () {
7187
setState(() {
@@ -101,27 +117,20 @@ class _PerformancePageState extends State<PerformancePage> {
101117
return null;
102118
}
103119
}
104-
}
105-
106-
Future<void> computeOnMainIsolate() async {
107-
// The isolate will need a little time to disable the buttons before the performance hit.
108-
await Future.delayed(Duration(milliseconds: 100), () => fib(45));
109-
}
110-
111-
Future<void> computeOnSecondaryIsolate() async {
112-
await compute(fib, 45);
113-
}
114120

115-
int fib(int n) {
116-
int number1 = n - 1;
117-
int number2 = n - 2;
121+
Future<void> computeOnMainIsolate() async {
122+
// A delay is added here to give Flutter the chance to redraw the UI at least
123+
// once before the computation (which, since it's run on the main isolate,
124+
// will lock up the app) begins executing.
125+
await Future.delayed(
126+
Duration(milliseconds: 100),
127+
() => fib(45),
128+
);
129+
}
118130

119-
if (n == 1) {
120-
return 0;
121-
} else if (n == 0) {
122-
return 1;
123-
} else {
124-
return (fib(number1) + fib(number2));
131+
Future<void> computeOnSecondaryIsolate() async {
132+
// Compute the Fibonacci series on a secondary isolate.
133+
await compute(fib, 45);
125134
}
126135
}
127136

@@ -133,44 +142,32 @@ class SmoothAnimationWidget extends StatefulWidget {
133142
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
134143
with TickerProviderStateMixin {
135144
AnimationController _controller;
136-
Animation<BorderRadius> borderRadius;
145+
Animation<BorderRadius> _borderAnimation;
137146

138147
@override
139148
void initState() {
140149
super.initState();
141150

142-
_controller =
143-
AnimationController(duration: const Duration(seconds: 1), vsync: this)
144-
..addStatusListener(
145-
(status) {
146-
if (status == AnimationStatus.completed) {
147-
_controller.reverse();
148-
} else if (status == AnimationStatus.dismissed) {
149-
_controller.forward();
150-
}
151-
},
152-
);
151+
_controller = AnimationController(
152+
duration: const Duration(seconds: 1),
153+
vsync: this,
154+
);
153155

154-
borderRadius = BorderRadiusTween(
156+
_borderAnimation = BorderRadiusTween(
155157
begin: BorderRadius.circular(100.0),
156158
end: BorderRadius.circular(0.0),
157-
).animate(
158-
CurvedAnimation(
159-
parent: _controller,
160-
curve: Curves.linear,
161-
),
162-
);
159+
).animate(_controller);
163160

164-
_controller.forward();
161+
_controller.repeat(reverse: true);
165162
}
166163

167164
@override
168165
Widget build(BuildContext context) {
169-
return AnimatedBuilder(
170-
animation: borderRadius,
171-
builder: (context, child) {
172-
return Center(
173-
child: Container(
166+
return Center(
167+
child: AnimatedBuilder(
168+
animation: _borderAnimation,
169+
builder: (context, child) {
170+
return Container(
174171
child: FlutterLogo(
175172
size: 200,
176173
),
@@ -180,13 +177,16 @@ class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
180177
decoration: BoxDecoration(
181178
gradient: LinearGradient(
182179
begin: Alignment.topLeft,
183-
colors: [Colors.blueAccent, Colors.redAccent],
180+
colors: [
181+
Colors.blueAccent,
182+
Colors.redAccent,
183+
],
184184
),
185-
borderRadius: borderRadius.value,
185+
borderRadius: _borderAnimation.value,
186186
),
187-
),
188-
);
189-
},
187+
);
188+
},
189+
),
190190
);
191191
}
192192

0 commit comments

Comments
 (0)