|
|
@ -17,15 +17,15 @@ import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
|
|
// Computes the nth number in the Fibonacci sequence.
|
|
|
|
// Computes the nth number in the Fibonacci sequence.
|
|
|
|
int fib(int n) {
|
|
|
|
int fib(int n) {
|
|
|
|
int number1 = n - 1;
|
|
|
|
var a = n - 1;
|
|
|
|
int number2 = n - 2;
|
|
|
|
var b = n - 2;
|
|
|
|
|
|
|
|
|
|
|
|
if (n == 1) {
|
|
|
|
if (n == 1) {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
} else if (n == 0) {
|
|
|
|
} else if (n == 0) {
|
|
|
|
return 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return (fib(number1) + fib(number2));
|
|
|
|
return (fib(a) + fib(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -49,25 +49,29 @@ class _PerformancePageState extends State<PerformancePage> {
|
|
|
|
padding: EdgeInsets.only(top: 150),
|
|
|
|
padding: EdgeInsets.only(top: 150),
|
|
|
|
child: Column(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
children: [
|
|
|
|
FutureBuilder<void>(
|
|
|
|
FutureBuilder(
|
|
|
|
future: computeFuture,
|
|
|
|
future: computeFuture,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return RaisedButton(
|
|
|
|
return RaisedButton(
|
|
|
|
child: const Text('Compute on Main'),
|
|
|
|
child: const Text('Compute on Main'),
|
|
|
|
elevation: 8.0,
|
|
|
|
elevation: 8.0,
|
|
|
|
onPressed: createMainIsolateCallBack(context, snapshot),
|
|
|
|
onPressed:
|
|
|
|
|
|
|
|
snapshot.connectionState == ConnectionState.done
|
|
|
|
|
|
|
|
? () => handleComputeOnMain(context)
|
|
|
|
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
FutureBuilder<void>(
|
|
|
|
FutureBuilder(
|
|
|
|
future: computeFuture,
|
|
|
|
future: computeFuture,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
return RaisedButton(
|
|
|
|
return RaisedButton(
|
|
|
|
child: const Text('Compute on Secondary'),
|
|
|
|
child: const Text('Compute on Secondary'),
|
|
|
|
elevation: 8.0,
|
|
|
|
elevation: 8.0,
|
|
|
|
onPressed:
|
|
|
|
onPressed:
|
|
|
|
createSecondaryIsolateCallBack(context, snapshot),
|
|
|
|
snapshot.connectionState == ConnectionState.done
|
|
|
|
);
|
|
|
|
? () => handleComputeOnSecondary(context)
|
|
|
|
|
|
|
|
: null);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
],
|
|
|
@ -78,54 +82,40 @@ class _PerformancePageState extends State<PerformancePage> {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VoidCallback createMainIsolateCallBack(
|
|
|
|
void handleComputeOnMain(BuildContext context) {
|
|
|
|
BuildContext context,
|
|
|
|
var future = computeOnMainIsolate()
|
|
|
|
AsyncSnapshot snapshot,
|
|
|
|
..then((_) {
|
|
|
|
) {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
|
|
content: Text('Main Isolate Done!'),
|
|
|
|
return () {
|
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
Scaffold.of(context).showSnackBar(snackBar);
|
|
|
|
computeFuture = computeOnMainIsolate()
|
|
|
|
});
|
|
|
|
..then((_) {
|
|
|
|
|
|
|
|
final snackBar = SnackBar(
|
|
|
|
setState(() {
|
|
|
|
content: Text('Main Isolate Done!'),
|
|
|
|
computeFuture = future;
|
|
|
|
);
|
|
|
|
});
|
|
|
|
Scaffold.of(context).showSnackBar(snackBar);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VoidCallback createSecondaryIsolateCallBack(
|
|
|
|
void handleComputeOnSecondary(BuildContext context) {
|
|
|
|
BuildContext context, AsyncSnapshot snapshot) {
|
|
|
|
var future = computeOnSecondaryIsolate()
|
|
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
|
|
..then((_) {
|
|
|
|
return () {
|
|
|
|
var snackBar = SnackBar(
|
|
|
|
setState(() {
|
|
|
|
content: Text('Secondary Isolate Done!'),
|
|
|
|
computeFuture = computeOnSecondaryIsolate()
|
|
|
|
);
|
|
|
|
..then((_) {
|
|
|
|
Scaffold.of(context).showSnackBar(snackBar);
|
|
|
|
final snackBar = SnackBar(
|
|
|
|
});
|
|
|
|
content: Text('Secondary Isolate Done!'),
|
|
|
|
|
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
Scaffold.of(context).showSnackBar(snackBar);
|
|
|
|
computeFuture = future;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> computeOnMainIsolate() async {
|
|
|
|
Future<void> computeOnMainIsolate() async {
|
|
|
|
// A delay is added here to give Flutter the chance to redraw the UI at least
|
|
|
|
// A delay is added here to give Flutter the chance to redraw the UI at
|
|
|
|
// once before the computation (which, since it's run on the main isolate,
|
|
|
|
// least once before the computation (which, since it's run on the main
|
|
|
|
// will lock up the app) begins executing.
|
|
|
|
// isolate, will lock up the app) begins executing.
|
|
|
|
await Future.delayed(
|
|
|
|
await Future<void>.delayed(Duration(milliseconds: 100));
|
|
|
|
Duration(milliseconds: 100),
|
|
|
|
fib(45);
|
|
|
|
() => fib(45),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> computeOnSecondaryIsolate() async {
|
|
|
|
Future<void> computeOnSecondaryIsolate() async {
|
|
|
@ -141,24 +131,22 @@ class SmoothAnimationWidget extends StatefulWidget {
|
|
|
|
|
|
|
|
|
|
|
|
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
|
|
|
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
|
|
|
with TickerProviderStateMixin {
|
|
|
|
with TickerProviderStateMixin {
|
|
|
|
AnimationController _controller;
|
|
|
|
AnimationController _animationController;
|
|
|
|
Animation<BorderRadius> _borderAnimation;
|
|
|
|
Animation<BorderRadius> _borderAnimation;
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
|
|
|
|
_controller = AnimationController(
|
|
|
|
_animationController =
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
AnimationController(duration: const Duration(seconds: 1), vsync: this);
|
|
|
|
vsync: this,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_borderAnimation = BorderRadiusTween(
|
|
|
|
_borderAnimation = BorderRadiusTween(
|
|
|
|
begin: BorderRadius.circular(100.0),
|
|
|
|
begin: BorderRadius.circular(100.0),
|
|
|
|
end: BorderRadius.circular(0.0),
|
|
|
|
end: BorderRadius.circular(0.0))
|
|
|
|
).animate(_controller);
|
|
|
|
.animate(_animationController);
|
|
|
|
|
|
|
|
|
|
|
|
_controller.repeat(reverse: true);
|
|
|
|
_animationController.repeat(reverse: true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
@ -192,7 +180,7 @@ class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
void dispose() {
|
|
|
|
_controller.dispose();
|
|
|
|
_animationController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|