mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Secondpage (#97)
This commit is contained in:
committed by
Andrew Brogdon
parent
b3ef6466fc
commit
2ec8cc3684
@@ -19,9 +19,9 @@ import 'dart:math';
|
||||
|
||||
class InfiniteProcessPageStarter extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(context) {
|
||||
return ChangeNotifierProvider(
|
||||
builder: (context) => IsolateController(),
|
||||
builder: (context) => InfiniteProcessIsolateController(),
|
||||
child: InfiniteProcessPage(),
|
||||
);
|
||||
}
|
||||
@@ -29,7 +29,9 @@ class InfiniteProcessPageStarter extends StatelessWidget {
|
||||
|
||||
class InfiniteProcessPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(context) {
|
||||
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
@@ -40,21 +42,15 @@ class InfiniteProcessPage extends StatelessWidget {
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
newButtons(context),
|
||||
],
|
||||
children: [newButtons(context)],
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
children: [
|
||||
Switch(
|
||||
value:
|
||||
!(Provider.of<IsolateController>(context, listen: true)
|
||||
.paused),
|
||||
onChanged: (_) =>
|
||||
Provider.of<IsolateController>(context, listen: false)
|
||||
.pausedSwitch(),
|
||||
value: !controller.paused,
|
||||
onChanged: (_) => controller.pausedSwitch(),
|
||||
activeTrackColor: Colors.lightGreenAccent,
|
||||
activeColor: Colors.black,
|
||||
inactiveTrackColor: Colors.deepOrangeAccent,
|
||||
@@ -72,7 +68,7 @@ class InfiniteProcessPage extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class IsolateController extends ChangeNotifier {
|
||||
class InfiniteProcessIsolateController extends ChangeNotifier {
|
||||
Isolate newIsolate;
|
||||
ReceivePort mIceRP;
|
||||
SendPort newIceSP;
|
||||
@@ -83,30 +79,30 @@ class IsolateController extends ChangeNotifier {
|
||||
bool _running = false;
|
||||
bool _paused = false;
|
||||
|
||||
IsolateController() {
|
||||
InfiniteProcessIsolateController() {
|
||||
start();
|
||||
_running = true;
|
||||
}
|
||||
|
||||
void createIsolate() async {
|
||||
Future<void> createIsolate() async {
|
||||
mIceRP = ReceivePort();
|
||||
newIsolate = await Isolate.spawn(_secondIsolateEntryPoint, mIceRP.sendPort);
|
||||
}
|
||||
|
||||
void listen() async {
|
||||
void listen() {
|
||||
mIceRP.listen((message) {
|
||||
if (message is SendPort) {
|
||||
newIceSP = message;
|
||||
newIceSP.send(_currentMultiplier.toString());
|
||||
newIceSP.send(_currentMultiplier);
|
||||
} else if (message is int) {
|
||||
setCurrentResults(message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future start() async {
|
||||
Future<void> start() async {
|
||||
if (_running == false && _paused == false) {
|
||||
createIsolate();
|
||||
await createIsolate();
|
||||
listen();
|
||||
_running = true;
|
||||
notifyListeners();
|
||||
@@ -144,20 +140,24 @@ class IsolateController extends ChangeNotifier {
|
||||
bool get running => _running;
|
||||
|
||||
List<int> get currentResults => _currentResults;
|
||||
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
newIsolate?.kill(priority: Isolate.immediate);
|
||||
newIsolate = null;
|
||||
}
|
||||
}
|
||||
|
||||
class RunningList extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
List<int> sums =
|
||||
Provider.of<IsolateController>(context, listen: true).currentResults;
|
||||
Widget build(context) {
|
||||
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
||||
|
||||
List<int> sums = controller.currentResults;
|
||||
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: (Provider.of<IsolateController>(context, listen: true).running ==
|
||||
true &&
|
||||
Provider.of<IsolateController>(context, listen: true).paused ==
|
||||
false)
|
||||
color: (controller.running == true && controller.paused == false)
|
||||
? Colors.lightGreenAccent
|
||||
: Colors.deepOrangeAccent,
|
||||
),
|
||||
@@ -212,51 +212,48 @@ Future<int> brokenUpComputation(int num) {
|
||||
}
|
||||
|
||||
Widget newButtons(context) {
|
||||
final controller =
|
||||
Provider.of<InfiniteProcessIsolateController>(context, listen: false);
|
||||
|
||||
return ButtonBar(
|
||||
alignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
RaisedButton(
|
||||
child: const Text('Start'),
|
||||
elevation: 8.0,
|
||||
onPressed: () =>
|
||||
Provider.of<IsolateController>(context, listen: false).start(),
|
||||
onPressed: () => controller.start(),
|
||||
),
|
||||
RaisedButton(
|
||||
child: const Text('Terminate'),
|
||||
elevation: 8.0,
|
||||
onPressed: () =>
|
||||
Provider.of<IsolateController>(context, listen: false).terminate(),
|
||||
onPressed: () => controller.terminate(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget radioButtonWidget(context) {
|
||||
final controller = Provider.of<InfiniteProcessIsolateController>(context);
|
||||
|
||||
return new Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
new Radio(
|
||||
value: 1,
|
||||
groupValue:
|
||||
Provider.of<IsolateController>(context, listen: true).multiplier,
|
||||
onChanged: (_) => Provider.of<IsolateController>(context, listen: false)
|
||||
.setMultiplier(1),
|
||||
groupValue: controller.multiplier,
|
||||
onChanged: (_) => controller.setMultiplier(1),
|
||||
),
|
||||
new Text('1x'),
|
||||
new Radio(
|
||||
value: 2,
|
||||
groupValue:
|
||||
Provider.of<IsolateController>(context, listen: true).multiplier,
|
||||
onChanged: (_) => Provider.of<IsolateController>(context, listen: false)
|
||||
.setMultiplier(2),
|
||||
groupValue: controller.multiplier,
|
||||
onChanged: (_) => controller.setMultiplier(2),
|
||||
),
|
||||
new Text('2x'),
|
||||
new Radio(
|
||||
value: 3,
|
||||
groupValue:
|
||||
Provider.of<IsolateController>(context, listen: true).multiplier,
|
||||
onChanged: (_) => Provider.of<IsolateController>(context, listen: false)
|
||||
.setMultiplier(3),
|
||||
groupValue: controller.multiplier,
|
||||
onChanged: (_) => controller.setMultiplier(3),
|
||||
),
|
||||
new Text('3x'),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user