mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Quarterly maintenance on isolate_example (#142)
This commit is contained in:
@@ -10,7 +10,7 @@ match any new language/SDK features, etc.).
|
|||||||
| chrome-os-best-practices | | |
|
| chrome-os-best-practices | | |
|
||||||
| experimental | | |
|
| experimental | | |
|
||||||
| flutter_maps_firestore | | |
|
| flutter_maps_firestore | | |
|
||||||
| isolate_example | | |
|
| isolate_example | redbrogdon | 9/12/19 |
|
||||||
| jsonexample | | |
|
| jsonexample | | |
|
||||||
| material_studies/shrine | | |
|
| material_studies/shrine | | |
|
||||||
| place_tracker | | |
|
| place_tracker | | |
|
||||||
|
|||||||
10
isolate_example/.gitignore
vendored
10
isolate_example/.gitignore
vendored
@@ -15,8 +15,10 @@
|
|||||||
*.iws
|
*.iws
|
||||||
.idea/
|
.idea/
|
||||||
|
|
||||||
# Visual Studio Code related
|
# The .vscode folder contains launch configuration and tasks you configure in
|
||||||
.vscode/
|
# VS Code which you may wish to be included in version control, so this line
|
||||||
|
# is commented out by default.
|
||||||
|
#.vscode/
|
||||||
|
|
||||||
# Flutter/Dart/Pub related
|
# Flutter/Dart/Pub related
|
||||||
**/doc/api/
|
**/doc/api/
|
||||||
@@ -59,6 +61,7 @@
|
|||||||
**/ios/Flutter/app.flx
|
**/ios/Flutter/app.flx
|
||||||
**/ios/Flutter/app.zip
|
**/ios/Flutter/app.zip
|
||||||
**/ios/Flutter/flutter_assets/
|
**/ios/Flutter/flutter_assets/
|
||||||
|
**/ios/Flutter/flutter_export_environment.sh
|
||||||
**/ios/ServiceDefinitions.json
|
**/ios/ServiceDefinitions.json
|
||||||
**/ios/Runner/GeneratedPluginRegistrant.*
|
**/ios/Runner/GeneratedPluginRegistrant.*
|
||||||
|
|
||||||
@@ -67,5 +70,4 @@
|
|||||||
!**/ios/**/default.mode2v3
|
!**/ios/**/default.mode2v3
|
||||||
!**/ios/**/default.pbxuser
|
!**/ios/**/default.pbxuser
|
||||||
!**/ios/**/default.perspectivev3
|
!**/ios/**/default.perspectivev3
|
||||||
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
|
||||||
bash: parse_git_branch: command not found
|
|
||||||
10
isolate_example/.metadata
Normal file
10
isolate_example/.metadata
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# This file tracks properties of this Flutter project.
|
||||||
|
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
||||||
|
#
|
||||||
|
# This file should be version controlled and should not be manually edited.
|
||||||
|
|
||||||
|
version:
|
||||||
|
revision: 2d2a1ffec95cc70a3218872a2cd3f8de4933c42f
|
||||||
|
channel: stable
|
||||||
|
|
||||||
|
project_type: app
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package dev.flutter.isolate_example
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
|
||||||
|
import io.flutter.app.FlutterActivity
|
||||||
|
import io.flutter.plugins.GeneratedPluginRegistrant
|
||||||
|
|
||||||
|
class MainActivity: FlutterActivity() {
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
GeneratedPluginRegistrant.registerWith(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
13
isolate_example/ios/Runner/AppDelegate.swift
Normal file
13
isolate_example/ios/Runner/AppDelegate.swift
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import UIKit
|
||||||
|
import Flutter
|
||||||
|
|
||||||
|
@UIApplicationMain
|
||||||
|
@objc class AppDelegate: FlutterAppDelegate {
|
||||||
|
override func application(
|
||||||
|
_ application: UIApplication,
|
||||||
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
||||||
|
) -> Bool {
|
||||||
|
GeneratedPluginRegistrant.register(with: self)
|
||||||
|
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||||||
|
}
|
||||||
|
}
|
||||||
1
isolate_example/ios/Runner/Runner-Bridging-Header.h
Normal file
1
isolate_example/ios/Runner/Runner-Bridging-Header.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#import "GeneratedPluginRegistrant.h"
|
||||||
@@ -15,10 +15,16 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
import 'page_one.dart';
|
import 'page_one.dart';
|
||||||
import 'page_two.dart';
|
|
||||||
import 'page_three.dart';
|
import 'page_three.dart';
|
||||||
|
import 'page_two.dart';
|
||||||
|
|
||||||
void main() => runApp(MaterialApp(home: StartApp()));
|
void main() {
|
||||||
|
runApp(
|
||||||
|
MaterialApp(
|
||||||
|
home: StartApp(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
class StartApp extends StatelessWidget {
|
class StartApp extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
@@ -30,9 +36,18 @@ class StartApp extends StatelessWidget {
|
|||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
bottom: TabBar(
|
bottom: TabBar(
|
||||||
tabs: [
|
tabs: [
|
||||||
Tab(icon: Icon(Icons.flash_on), text: 'Performance'),
|
Tab(
|
||||||
Tab(icon: Icon(Icons.sync), text: 'Infinite Process'),
|
icon: Icon(Icons.flash_on),
|
||||||
Tab(icon: Icon(Icons.storage), text: 'Data Transfer'),
|
text: 'Performance',
|
||||||
|
),
|
||||||
|
Tab(
|
||||||
|
icon: Icon(Icons.sync),
|
||||||
|
text: 'Infinite Process',
|
||||||
|
),
|
||||||
|
Tab(
|
||||||
|
icon: Icon(Icons.storage),
|
||||||
|
text: 'Data Transfer',
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
title: Text('Isolate Example'),
|
title: Text('Isolate Example'),
|
||||||
|
|||||||
@@ -15,6 +15,20 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
// Computes the nth number in the Fibonacci sequence.
|
||||||
|
int fib(int n) {
|
||||||
|
int number1 = n - 1;
|
||||||
|
int number2 = n - 2;
|
||||||
|
|
||||||
|
if (n == 1) {
|
||||||
|
return 0;
|
||||||
|
} else if (n == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return (fib(number1) + fib(number2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class PerformancePage extends StatefulWidget {
|
class PerformancePage extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
_PerformancePageState createState() => _PerformancePageState();
|
_PerformancePageState createState() => _PerformancePageState();
|
||||||
@@ -65,7 +79,9 @@ class _PerformancePageState extends State<PerformancePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VoidCallback createMainIsolateCallBack(
|
VoidCallback createMainIsolateCallBack(
|
||||||
BuildContext context, AsyncSnapshot snapshot) {
|
BuildContext context,
|
||||||
|
AsyncSnapshot snapshot,
|
||||||
|
) {
|
||||||
if (snapshot.connectionState == ConnectionState.done) {
|
if (snapshot.connectionState == ConnectionState.done) {
|
||||||
return () {
|
return () {
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -101,27 +117,20 @@ class _PerformancePageState extends State<PerformancePage> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> computeOnMainIsolate() async {
|
Future<void> computeOnMainIsolate() async {
|
||||||
// The isolate will need a little time to disable the buttons before the performance hit.
|
// A delay is added here to give Flutter the chance to redraw the UI at least
|
||||||
await Future.delayed(Duration(milliseconds: 100), () => fib(45));
|
// once before the computation (which, since it's run on the main isolate,
|
||||||
}
|
// will lock up the app) begins executing.
|
||||||
|
await Future.delayed(
|
||||||
|
Duration(milliseconds: 100),
|
||||||
|
() => fib(45),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> computeOnSecondaryIsolate() async {
|
Future<void> computeOnSecondaryIsolate() async {
|
||||||
await compute(fib, 45);
|
// Compute the Fibonacci series on a secondary isolate.
|
||||||
}
|
await compute(fib, 45);
|
||||||
|
|
||||||
int fib(int n) {
|
|
||||||
int number1 = n - 1;
|
|
||||||
int number2 = n - 2;
|
|
||||||
|
|
||||||
if (n == 1) {
|
|
||||||
return 0;
|
|
||||||
} else if (n == 0) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return (fib(number1) + fib(number2));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,44 +142,32 @@ class SmoothAnimationWidget extends StatefulWidget {
|
|||||||
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
||||||
with TickerProviderStateMixin {
|
with TickerProviderStateMixin {
|
||||||
AnimationController _controller;
|
AnimationController _controller;
|
||||||
Animation<BorderRadius> borderRadius;
|
Animation<BorderRadius> _borderAnimation;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
_controller =
|
_controller = AnimationController(
|
||||||
AnimationController(duration: const Duration(seconds: 1), vsync: this)
|
duration: const Duration(seconds: 1),
|
||||||
..addStatusListener(
|
vsync: this,
|
||||||
(status) {
|
|
||||||
if (status == AnimationStatus.completed) {
|
|
||||||
_controller.reverse();
|
|
||||||
} else if (status == AnimationStatus.dismissed) {
|
|
||||||
_controller.forward();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
borderRadius = BorderRadiusTween(
|
|
||||||
begin: BorderRadius.circular(100.0),
|
|
||||||
end: BorderRadius.circular(0.0),
|
|
||||||
).animate(
|
|
||||||
CurvedAnimation(
|
|
||||||
parent: _controller,
|
|
||||||
curve: Curves.linear,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
_controller.forward();
|
_borderAnimation = BorderRadiusTween(
|
||||||
|
begin: BorderRadius.circular(100.0),
|
||||||
|
end: BorderRadius.circular(0.0),
|
||||||
|
).animate(_controller);
|
||||||
|
|
||||||
|
_controller.repeat(reverse: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return AnimatedBuilder(
|
return Center(
|
||||||
animation: borderRadius,
|
child: AnimatedBuilder(
|
||||||
builder: (context, child) {
|
animation: _borderAnimation,
|
||||||
return Center(
|
builder: (context, child) {
|
||||||
child: Container(
|
return Container(
|
||||||
child: FlutterLogo(
|
child: FlutterLogo(
|
||||||
size: 200,
|
size: 200,
|
||||||
),
|
),
|
||||||
@@ -180,13 +177,16 @@ class SmoothAnimationWidgetState extends State<SmoothAnimationWidget>
|
|||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
gradient: LinearGradient(
|
gradient: LinearGradient(
|
||||||
begin: Alignment.topLeft,
|
begin: Alignment.topLeft,
|
||||||
colors: [Colors.blueAccent, Colors.redAccent],
|
colors: [
|
||||||
|
Colors.blueAccent,
|
||||||
|
Colors.redAccent,
|
||||||
|
],
|
||||||
),
|
),
|
||||||
borderRadius: borderRadius.value,
|
borderRadius: _borderAnimation.value,
|
||||||
),
|
),
|
||||||
),
|
);
|
||||||
);
|
},
|
||||||
},
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,11 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
import 'dart:typed_data';
|
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@@ -30,6 +32,8 @@ class DataTransferPageStarter extends StatelessWidget {
|
|||||||
class DataTransferPage extends StatelessWidget {
|
class DataTransferPage extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(context) {
|
Widget build(context) {
|
||||||
|
final controller = Provider.of<DataTransferIsolateController>(context);
|
||||||
|
|
||||||
return SafeArea(
|
return SafeArea(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
@@ -42,17 +46,34 @@ class DataTransferPage extends StatelessWidget {
|
|||||||
padding: EdgeInsets.all(8),
|
padding: EdgeInsets.all(8),
|
||||||
),
|
),
|
||||||
LinearProgressIndicator(
|
LinearProgressIndicator(
|
||||||
value: Provider.of<DataTransferIsolateController>(context)
|
value: controller.progressPercent,
|
||||||
.progressPercent,
|
|
||||||
backgroundColor: Colors.grey[200],
|
backgroundColor: Colors.grey[200],
|
||||||
),
|
),
|
||||||
Expanded(child: RunningList()),
|
Expanded(
|
||||||
|
child: RunningList(),
|
||||||
|
),
|
||||||
Column(
|
Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
children: [
|
children: [
|
||||||
Row(
|
RaisedButton(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
child: const Text('Transfer Data to 2nd Isolate'),
|
||||||
children: [createButtons(context)],
|
color: (controller.runningTest == 1)
|
||||||
|
? Colors.blueAccent
|
||||||
|
: Colors.grey[300],
|
||||||
|
onPressed: () => controller.generateRandomNumbers(false),
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: const Text('Transfer Data with TransferableTypedData'),
|
||||||
|
color: (controller.runningTest == 2)
|
||||||
|
? Colors.blueAccent
|
||||||
|
: Colors.grey[300],
|
||||||
|
onPressed: () => controller.generateRandomNumbers(true),
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: const Text('Generate on 2nd Isolate'),
|
||||||
|
color: (controller.runningTest == 3)
|
||||||
|
? Colors.blueAccent
|
||||||
|
: Colors.grey[300],
|
||||||
|
onPressed: controller.generateOnSecondaryIsolate,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -63,33 +84,39 @@ class DataTransferPage extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class DataTransferIsolateController extends ChangeNotifier {
|
class DataTransferIsolateController extends ChangeNotifier {
|
||||||
Isolate _newIsolate;
|
Isolate _isolate;
|
||||||
ReceivePort _mIceRP;
|
ReceivePort _incomingReceivePort;
|
||||||
SendPort _newIceSP;
|
SendPort _outgoingSendPort;
|
||||||
|
|
||||||
final currentProgress = <String>[];
|
final currentProgress = <String>[];
|
||||||
int runningTest = 0;
|
int runningTest = 0;
|
||||||
Stopwatch _timer = Stopwatch();
|
Stopwatch _timer = Stopwatch();
|
||||||
double progressPercent = 0;
|
double progressPercent = 0;
|
||||||
|
|
||||||
|
Isolate get newIsolate => _isolate;
|
||||||
|
|
||||||
|
bool get running => runningTest != 0;
|
||||||
|
|
||||||
DataTransferIsolateController() {
|
DataTransferIsolateController() {
|
||||||
createIsolate();
|
createIsolate();
|
||||||
listen();
|
listen();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createIsolate() async {
|
Future<void> createIsolate() async {
|
||||||
_mIceRP = ReceivePort();
|
_incomingReceivePort = ReceivePort();
|
||||||
_newIsolate =
|
_isolate = await Isolate.spawn(
|
||||||
await Isolate.spawn(_secondIsolateEntryPoint, _mIceRP.sendPort);
|
_secondIsolateEntryPoint, _incomingReceivePort.sendPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listen() {
|
void listen() {
|
||||||
_mIceRP.listen((dynamic message) {
|
_incomingReceivePort.listen((dynamic message) {
|
||||||
if (message is SendPort) _newIceSP = message;
|
if (message is SendPort) {
|
||||||
|
_outgoingSendPort = message;
|
||||||
|
}
|
||||||
|
|
||||||
if (message is int) {
|
if (message is int) {
|
||||||
currentProgress.insert(
|
currentProgress.insert(
|
||||||
0, '$message% - ${_timer.elapsedMilliseconds / 1000} Sec');
|
0, '$message% - ${_timer.elapsedMilliseconds / 1000} seconds');
|
||||||
progressPercent = message / 100;
|
progressPercent = message / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,7 +136,7 @@ class DataTransferIsolateController extends ChangeNotifier {
|
|||||||
|
|
||||||
_timer = Stopwatch();
|
_timer = Stopwatch();
|
||||||
_timer.start();
|
_timer.start();
|
||||||
_newIceSP.send('start');
|
_outgoingSendPort.send('start');
|
||||||
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
@@ -150,26 +177,22 @@ class DataTransferIsolateController extends ChangeNotifier {
|
|||||||
|
|
||||||
Future<void> sendNumbers(dynamic numList) async {
|
Future<void> sendNumbers(dynamic numList) async {
|
||||||
return Future<void>(() {
|
return Future<void>(() {
|
||||||
_newIceSP.send(numList);
|
_outgoingSendPort.send(numList);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Isolate get newIsolate => _newIsolate;
|
|
||||||
|
|
||||||
bool get running => runningTest != 0;
|
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
_newIsolate?.kill(priority: Isolate.immediate);
|
_isolate?.kill(priority: Isolate.immediate);
|
||||||
_newIsolate = null;
|
_isolate = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RunningList extends StatelessWidget {
|
class RunningList extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final controller = Provider.of<DataTransferIsolateController>(context);
|
final progress =
|
||||||
final progress = controller.currentProgress;
|
Provider.of<DataTransferIsolateController>(context).currentProgress;
|
||||||
|
|
||||||
return DecoratedBox(
|
return DecoratedBox(
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
@@ -234,7 +257,10 @@ Iterable<int> createNums() sync* {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> generateAndSum(
|
Future<void> generateAndSum(
|
||||||
SendPort callerSP, Iterable<int> iter, int length) async {
|
SendPort callerSP,
|
||||||
|
Iterable<int> iter,
|
||||||
|
int length,
|
||||||
|
) async {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int count = 1;
|
int count = 1;
|
||||||
|
|
||||||
@@ -248,41 +274,3 @@ Future<void> generateAndSum(
|
|||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget createButtons(BuildContext context) {
|
|
||||||
final controller =
|
|
||||||
Provider.of<DataTransferIsolateController>(context, listen: false);
|
|
||||||
return ButtonBar(
|
|
||||||
alignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Column(
|
|
||||||
children: [
|
|
||||||
RaisedButton(
|
|
||||||
child: const Text('Transfer Data to 2nd Isolate'),
|
|
||||||
elevation: 8.0,
|
|
||||||
color: (controller.runningTest == 1)
|
|
||||||
? Colors.blueAccent
|
|
||||||
: Colors.grey[300],
|
|
||||||
onPressed: () => controller.generateRandomNumbers(false),
|
|
||||||
),
|
|
||||||
RaisedButton(
|
|
||||||
child: const Text('Transfer Data with TransferableTypedData'),
|
|
||||||
elevation: 8.0,
|
|
||||||
color: (controller.runningTest == 2)
|
|
||||||
? Colors.blueAccent
|
|
||||||
: Colors.grey[300],
|
|
||||||
onPressed: () => controller.generateRandomNumbers(true),
|
|
||||||
),
|
|
||||||
RaisedButton(
|
|
||||||
child: const Text('Generate on 2nd Isolate'),
|
|
||||||
elevation: 8.0,
|
|
||||||
color: (controller.runningTest == 3)
|
|
||||||
? Colors.blueAccent
|
|
||||||
: Colors.grey[300],
|
|
||||||
onPressed: controller.generateOnSecondaryIsolate,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,11 +12,12 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class InfiniteProcessPageStarter extends StatelessWidget {
|
class InfiniteProcessPageStarter extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(context) {
|
Widget build(context) {
|
||||||
@@ -32,28 +33,40 @@ class InfiniteProcessPage extends StatelessWidget {
|
|||||||
Widget build(context) {
|
Widget build(context) {
|
||||||
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
||||||
|
|
||||||
return Column(
|
return SafeArea(
|
||||||
mainAxisSize: MainAxisSize.min,
|
child: Column(
|
||||||
children: [
|
mainAxisSize: MainAxisSize.min,
|
||||||
Padding(
|
children: [
|
||||||
child: Text(
|
Padding(
|
||||||
'Summation Results',
|
padding: const EdgeInsets.all(16.0),
|
||||||
style: Theme.of(context).textTheme.title,
|
child: Text(
|
||||||
|
'Summation Results',
|
||||||
|
style: Theme.of(context).textTheme.title,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
padding: EdgeInsets.all(8),
|
Expanded(
|
||||||
),
|
child: RunningList(),
|
||||||
Expanded(child: RunningList()),
|
),
|
||||||
SafeArea(
|
Column(
|
||||||
child: Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
Row(
|
ButtonBar(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
alignment: MainAxisAlignment.center,
|
||||||
children: [newButtons(context)],
|
children: [
|
||||||
|
RaisedButton(
|
||||||
|
child: const Text('Start'),
|
||||||
|
elevation: 8.0,
|
||||||
|
onPressed: () => controller.start(),
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: const Text('Terminate'),
|
||||||
|
elevation: 8.0,
|
||||||
|
onPressed: () => controller.terminate(),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: [
|
children: [
|
||||||
Switch(
|
Switch(
|
||||||
value: !controller.paused,
|
value: !controller.paused,
|
||||||
@@ -66,11 +79,23 @@ class InfiniteProcessPage extends StatelessWidget {
|
|||||||
Text('Pause/Resume'),
|
Text('Pause/Resume'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
radioButtonWidget(context),
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
for (int i = 1; i <= 3; i++) ...[
|
||||||
|
Radio<int>(
|
||||||
|
value: i,
|
||||||
|
groupValue: controller.currentMultiplier,
|
||||||
|
onChanged: (val) => controller.setMultiplier(val),
|
||||||
|
),
|
||||||
|
Text('${i}x')
|
||||||
|
],
|
||||||
|
],
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -83,9 +108,17 @@ class InfiniteProcessIsolateController extends ChangeNotifier {
|
|||||||
|
|
||||||
int _currentMultiplier = 1;
|
int _currentMultiplier = 1;
|
||||||
List<int> _currentResults = [];
|
List<int> _currentResults = [];
|
||||||
bool _running = false;
|
bool _created = false;
|
||||||
bool _paused = false;
|
bool _paused = false;
|
||||||
|
|
||||||
|
int get currentMultiplier => _currentMultiplier;
|
||||||
|
|
||||||
|
bool get paused => _paused;
|
||||||
|
|
||||||
|
bool get created => _created;
|
||||||
|
|
||||||
|
List<int> get currentResults => _currentResults;
|
||||||
|
|
||||||
Future<void> createIsolate() async {
|
Future<void> createIsolate() async {
|
||||||
mIceRP = ReceivePort();
|
mIceRP = ReceivePort();
|
||||||
newIsolate = await Isolate.spawn(_secondIsolateEntryPoint, mIceRP.sendPort);
|
newIsolate = await Isolate.spawn(_secondIsolateEntryPoint, mIceRP.sendPort);
|
||||||
@@ -103,23 +136,28 @@ class InfiniteProcessIsolateController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> start() async {
|
Future<void> start() async {
|
||||||
if (_running == false && _paused == false) {
|
if (_created == false && _paused == false) {
|
||||||
await createIsolate();
|
await createIsolate();
|
||||||
listen();
|
listen();
|
||||||
_running = true;
|
_created = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminate() {
|
void terminate() {
|
||||||
newIsolate.kill();
|
newIsolate.kill();
|
||||||
_running = false;
|
_created = false;
|
||||||
_currentResults.clear();
|
_currentResults.clear();
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void pausedSwitch() {
|
void pausedSwitch() {
|
||||||
(_paused) ? newIsolate.resume(capability) : capability = newIsolate.pause();
|
if (_paused) {
|
||||||
|
newIsolate.resume(capability);
|
||||||
|
} else {
|
||||||
|
capability = newIsolate.pause();
|
||||||
|
}
|
||||||
|
|
||||||
_paused = !_paused;
|
_paused = !_paused;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
@@ -135,18 +173,10 @@ class InfiniteProcessIsolateController extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
int get multiplier => _currentMultiplier;
|
|
||||||
|
|
||||||
bool get paused => _paused;
|
|
||||||
|
|
||||||
bool get running => _running;
|
|
||||||
|
|
||||||
List<int> get currentResults => _currentResults;
|
|
||||||
|
|
||||||
void dispose() {
|
void dispose() {
|
||||||
super.dispose();
|
|
||||||
newIsolate?.kill(priority: Isolate.immediate);
|
newIsolate?.kill(priority: Isolate.immediate);
|
||||||
newIsolate = null;
|
newIsolate = null;
|
||||||
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,7 +201,7 @@ class RunningList extends StatelessWidget {
|
|||||||
leading: Text('${sums.length - index}.'),
|
leading: Text('${sums.length - index}.'),
|
||||||
title: Text('${sums[index]}.'),
|
title: Text('${sums[index]}.'),
|
||||||
),
|
),
|
||||||
color: (controller.running && !controller.paused)
|
color: (controller.created && !controller.paused)
|
||||||
? Colors.lightGreenAccent
|
? Colors.lightGreenAccent
|
||||||
: Colors.deepOrangeAccent,
|
: Colors.deepOrangeAccent,
|
||||||
),
|
),
|
||||||
@@ -197,77 +227,28 @@ Future<void> _secondIsolateEntryPoint(SendPort callerSP) async {
|
|||||||
if (message is int) multiplyValue = message;
|
if (message is int) multiplyValue = message;
|
||||||
});
|
});
|
||||||
|
|
||||||
int forEnd = 10000;
|
// This runs until the isolate is terminated.
|
||||||
while (true) {
|
while (true) {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
|
|
||||||
for (int i = 0; i < forEnd; i++) {
|
for (int i = 0; i < 10000; i++) {
|
||||||
sum += await brokenUpComputation(1000);
|
sum += await doSomeWork();
|
||||||
}
|
}
|
||||||
|
|
||||||
forEnd += 10;
|
|
||||||
callerSP.send(sum * multiplyValue);
|
callerSP.send(sum * multiplyValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<int> brokenUpComputation(int num) {
|
Future<int> doSomeWork() {
|
||||||
Random rng = Random();
|
Random rng = Random();
|
||||||
|
|
||||||
return Future(() {
|
return Future(() {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
|
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
sum += rng.nextInt(100);
|
sum += rng.nextInt(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget newButtons(BuildContext context) {
|
|
||||||
final controller =
|
|
||||||
Provider.of<InfiniteProcessIsolateController>(context, listen: false);
|
|
||||||
|
|
||||||
return ButtonBar(
|
|
||||||
alignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
RaisedButton(
|
|
||||||
child: const Text('Start'),
|
|
||||||
elevation: 8.0,
|
|
||||||
onPressed: () => controller.start(),
|
|
||||||
),
|
|
||||||
RaisedButton(
|
|
||||||
child: const Text('Terminate'),
|
|
||||||
elevation: 8.0,
|
|
||||||
onPressed: () => controller.terminate(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget radioButtonWidget(BuildContext context) {
|
|
||||||
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
|
||||||
|
|
||||||
return Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Radio(
|
|
||||||
value: 1,
|
|
||||||
groupValue: controller.multiplier,
|
|
||||||
onChanged: (dynamic _) => controller.setMultiplier(1),
|
|
||||||
),
|
|
||||||
Text('1x'),
|
|
||||||
Radio(
|
|
||||||
value: 2,
|
|
||||||
groupValue: controller.multiplier,
|
|
||||||
onChanged: (dynamic _) => controller.setMultiplier(2),
|
|
||||||
),
|
|
||||||
Text('2x'),
|
|
||||||
Radio(
|
|
||||||
value: 3,
|
|
||||||
groupValue: controller.multiplier,
|
|
||||||
onChanged: (dynamic _) => controller.setMultiplier(3),
|
|
||||||
),
|
|
||||||
Text('3x'),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -29,13 +29,6 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.14.11"
|
version: "1.14.11"
|
||||||
cupertino_icons:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: cupertino_icons
|
|
||||||
url: "https://pub.dartlang.org"
|
|
||||||
source: hosted
|
|
||||||
version: "0.1.2"
|
|
||||||
flutter:
|
flutter:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
@@ -80,7 +73,7 @@ packages:
|
|||||||
name: provider
|
name: provider
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.0+1"
|
version: "3.1.0"
|
||||||
quiver:
|
quiver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -8,13 +8,12 @@ environment:
|
|||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
cupertino_icons: ^0.1.2
|
provider: ^3.1.0
|
||||||
provider: ^3.0.0
|
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
pedantic: ^1.7.0
|
pedantic: ^1.8.0+1
|
||||||
|
|
||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
|
|||||||
Reference in New Issue
Block a user