import 'package:flutter/material.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:rxdart/rxdart.dart'; main() => runApp(
MyApp(
storage: TextStorage(
bloc: DataBloc()
)
)
); class MyApp extends StatelessWidget {
MyApp({this.storage}); final TextStorage storage; @override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('ass'),),
body: StreamBuilder(
stream: storage.bloc.dataBloc.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(snapshot.data), RaisedButton(child: Text('add'), onPressed: () {
storage.writeFile('add hello');
},),
RaisedButton(child: Text('check'), onPressed: () {
storage.readFile();
},),
RaisedButton(child: Text('clean'), onPressed: () {
storage.cleanFile();
},),
],
),);
} else {
return Container(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('start'),
onPressed: (){
storage.writeFile('add hello');
}), ],
),
);
}
},
), ),
);
}
} class DataBloc {
PublishSubject dataBloc = PublishSubject(); DataBloc() {
dataBloc.listen(onData);
} void onData(value) {}
} class TextStorage {
TextStorage({this.bloc}); DataBloc bloc; Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
} Future<File> get _localFile async {
final path = await _localPath;
return File('$path/text.txt');
} Future<String> readFile() async {
try {
final file = await _localFile;
String content = await file.readAsString();
bloc.dataBloc.add(content);
return content;
} catch (e) {
return '';
}
} Future<File> writeFile(String text) async {
final file = await _localFile;
bloc.dataBloc.add('this is test');
print('get $text, but write: this is test');
return file.writeAsString('$text\r\n', mode: FileMode.append);
} Future<File> cleanFile() async {
final file = await _localFile;
bloc.dataBloc.add('clean');
return file.writeAsString('');
}
}

  

05-08 08:02