Skip to content

Instantly share code, notes, and snippets.

@backviet
Forked from jebright/main.dart
Last active June 25, 2024 16:35
Show Gist options
  • Save backviet/63c689856acbe47912b4eeef33c7b46a to your computer and use it in GitHub Desktop.
Save backviet/63c689856acbe47912b4eeef33c7b46a to your computer and use it in GitHub Desktop.

Revisions

  1. backviet renamed this gist Jun 25, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jebright jebright created this gist Apr 16, 2019.
    96 changes: 96 additions & 0 deletions main.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'dart:isolate';

    void main() => runApp(new MyApp());

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return new MaterialApp(
    title: 'Flutter Isolate Demo',
    theme: new ThemeData(
    primarySwatch: Colors.blue,
    ),
    home: new MyHomePage(title: 'Flutter Isolates'),
    );
    }
    }

    class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;

    @override
    _MyHomePageState createState() => new _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
    Isolate _isolate;
    bool _running = false;
    static int _counter = 0;
    String notification = "";
    ReceivePort _receivePort;

    void _start() async {
    _running = true;
    _receivePort = ReceivePort();
    _isolate = await Isolate.spawn(_checkTimer, _receivePort.sendPort);
    _receivePort.listen(_handleMessage, onDone:() {
    print("done!");
    });
    }

    static void _checkTimer(SendPort sendPort) async {
    Timer.periodic(new Duration(seconds: 1), (Timer t) {
    _counter++;
    String msg = 'notification ' + _counter.toString();
    print('SEND: ' + msg);
    sendPort.send(msg);
    });
    }

    void _handleMessage(dynamic data) {
    print('RECEIVED: ' + data);
    setState(() {
    notification = data;
    });
    }

    void _stop() {
    if (_isolate != null) {
    setState(() {
    _running = false;
    notification = '';
    });
    _receivePort.close();
    _isolate.kill(priority: Isolate.immediate);
    _isolate = null;
    }
    }


    @override
    Widget build(BuildContext context) {
    return new Scaffold(
    appBar: new AppBar(
    title: new Text(widget.title),
    ),
    body: new Center(
    child: new Column(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
    new Text(
    notification,
    ),
    ],
    ),
    ),
    floatingActionButton: new FloatingActionButton(
    onPressed: _running ? _stop : _start,
    tooltip: _running ? 'Timer stop' : 'Timer start',
    child: _running ? new Icon(Icons.stop) : new Icon(Icons.play_arrow),
    ),
    );
    }
    }