Advertisement
sourav8256

Untitled

Aug 11th, 2023 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'dart:io';
  4.  
  5. void main() {
  6. runApp(MyApp());
  7. }
  8.  
  9. class MyApp extends StatelessWidget {
  10. @override
  11. Widget build(BuildContext context) {
  12. return MaterialApp(
  13. title: 'String Input and Save',
  14. theme: ThemeData(
  15. primarySwatch: Colors.blue,
  16. ),
  17. home: MyHomePage(),
  18. );
  19. }
  20. }
  21.  
  22. class MyHomePage extends StatefulWidget {
  23. @override
  24. _MyHomePageState createState() => _MyHomePageState();
  25. }
  26.  
  27. class _MyHomePageState extends State<MyHomePage> {
  28. TextEditingController _textEditingController = TextEditingController();
  29.  
  30. void _saveToFile() async {
  31. String text = _textEditingController.text;
  32. if (text.isNotEmpty) {
  33. Directory directory = await getApplicationDocumentsDirectory();
  34. File file = File('${directory.path}/my_text_file.txt');
  35. await file.writeAsString(text);
  36. ScaffoldMessenger.of(context).showSnackBar(
  37. SnackBar(
  38. content: Text('Text saved to file'),
  39. ),
  40. );
  41. }
  42. }
  43.  
  44. @override
  45. Widget build(BuildContext context) {
  46. return Scaffold(
  47. appBar: AppBar(
  48. title: Text('String Input and Save'),
  49. ),
  50. body: Padding(
  51. padding: EdgeInsets.all(16.0),
  52. child: Column(
  53. mainAxisAlignment: MainAxisAlignment.center,
  54. crossAxisAlignment: CrossAxisAlignment.stretch,
  55. children: [
  56. TextField(
  57. controller: _textEditingController,
  58. decoration: InputDecoration(labelText: 'Enter your text'),
  59. ),
  60. SizedBox(height: 16.0),
  61. ElevatedButton(
  62. onPressed: _saveToFile,
  63. child: Text('Save to File'),
  64. ),
  65. ],
  66. ),
  67. ),
  68. );
  69. }
  70. }
  71.  
  72.  
  73.  
  74.  
  75. dependencies:
  76. flutter:
  77. sdk: flutter
  78. path_provider: ^2.0.2
  79.  
  80.  
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement