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