반응형
- 코드 작성
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,
),
),
),
);
}
}
반응형
'개발 > Flutter' 카테고리의 다른 글
플러터 Flutter 뒤로가기 2번 앱 종료 간단 구현 (0) | 2023.03.28 |
---|---|
Flutter 플러터 Progress bar 진행바 간단 구현 LinearProgressIndicator (0) | 2023.03.09 |
플러터 Flutter For문 Switch문 간단 구현 (0) | 2023.02.18 |
플러터 Flutter if else문 간단 구현법 (0) | 2023.02.18 |
플러터 Flutter ToDo List 구현하기 (0) | 2023.01.29 |
Flutter 플러터 타이머 간단 구현 Timer (0) | 2023.01.29 |
Flutter 플러터 URL 주소 열기 간단 구현 url launcher (0) | 2022.12.25 |
Flutter 플러터 화면 전환 애니메이션 삭제 간단 사용법 Navigator Animation (0) | 2022.12.25 |
댓글