标签:navig body 通信 pause .data state initial tle creat
main.dart
import ‘package:flutter/material.dart‘; import ‘package:flutter_isolate/flutter_isolate.dart‘; import ‘isolates.dart‘; import ‘dbhelper.dart‘; import ‘package:rxdart/rxdart.dart‘; import ‘dart:isolate‘; void main() { runApp(MaterialApp( title: ‘Flutter Demo‘, initialRoute: ‘/‘, routes: { ‘/‘:(context)=>MyApp(), ‘/second‘:(context)=>NextPage(), }, )); } class MyApp extends StatefulWidget{ @override State<StatefulWidget> createState() { return MyAppState(); } } class MyAppState extends State<MyApp> { FlutterIsolate isoltex; DataBloc bloc = DataBloc(); ReceivePort toChild; DB db = DB(); addData()async{ var a = await db.addData(‘type‘, {‘name‘:‘11maintest‘}); print(a); } checkData()async{ var b = await db.queryData(‘SELECT * FROM type‘); print(b); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(‘sss‘),), body: Container(child: Column( children: <Widget>[ StreamBuilder( stream: bloc.dataBloc.stream, builder: (context, snapshot){ if(snapshot.hasData){ return Text(‘${snapshot.data}‘); }else{ return Text(‘pending..‘); } }, ), RaisedButton(child: Text(‘addData‘),onPressed: ()async{ addData(); },), RaisedButton(child: Text(‘check data‘),onPressed: ()async{ checkData(); },), RaisedButton(child: Text(‘start‘),onPressed: ()async{ isoltex = await createIsolate(bloc,‘abc‘); },), RaisedButton(child: Text(‘pause‘),onPressed: (){ isoltex.pause(); },), RaisedButton(child: Text(‘resume‘),onPressed: (){ isoltex.resume(); },), RaisedButton(child: Text(‘kill‘),onPressed: (){ isoltex.kill(); },), RaisedButton(child: Text(‘go to next‘),onPressed: (){ Navigator.of(context).pushNamed(‘/second‘); },) ]), ), ); } } class NextPage extends StatelessWidget{ @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(‘page2‘),), body: Container( child: RaisedButton(child:Text(‘btn‘), onPressed: (){ print(‘hello‘); }), ), ); } } class DataBloc { ReplaySubject dataBloc = ReplaySubject(); }
isolates.dart
import ‘package:flutter_isolate/flutter_isolate.dart‘; import ‘dart:async‘; import ‘dart:isolate‘; import ‘main.dart‘; Future<FlutterIsolate> createIsolate(DataBloc bloc, String type) async { ReceivePort receivePort = ReceivePort(); ReceivePort fromChild = ReceivePort(); FlutterIsolate isolate = await FlutterIsolate.spawn(isolateEntry, receivePort.sendPort); SendPort t = await receivePort.first; t.send({‘sender‘:fromChild.sendPort,‘msg‘:type}); fromChild.listen((value){ bloc.dataBloc.add(value); }); return isolate; } isolateEntry(SendPort sendPort)async{ ReceivePort port = ReceivePort(); sendPort.send(port.sendPort); port.listen((data)async{ print(‘son $data‘); await doWork(data[‘msg‘], data[‘sender‘]); }); } Future doWork(data, SendPort s)async{ print(‘start working‘); int i = 0; while(i<10){ s.send(‘$data : $i‘); await Future.delayed(Duration(seconds: 1)); i++; } }
标签:navig body 通信 pause .data state initial tle creat
原文地址:https://www.cnblogs.com/pythonClub/p/10822365.html