반응형
- 코드 작성
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
class Notepad extends StatefulWidget {
@override
_NotepadState createState() => _NotepadState();
}
class _NotepadState extends State<Notepad> {
TextEditingController _textController = TextEditingController();
String _filePath = '';
@override
void initState() {
super.initState();
_loadFile();
}
Future<void> _loadFile() async {
final directory = await getApplicationDocumentsDirectory();
_filePath = '${directory.path}/notepad.txt';
final file = File(_filePath);
if (await file.exists()) {
final contents = await file.readAsString();
setState(() {
_textController.text = contents;
});
}
}
Future<void> _saveFile() async {
final file = File(_filePath);
await file.writeAsString(_textController.text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notepad'),
actions: [
IconButton(
icon: Icon(Icons.save),
onPressed: () async {
await _saveFile();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Note saved')));
},
),
],
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: TextField(
controller: _textController,
maxLines: null,
decoration: InputDecoration(
hintText: 'Start typing here...',
border: InputBorder.none,
),
),
),
);
}
}
반응형