From 2776162281f503e594031339716a729e08a6c58c Mon Sep 17 00:00:00 2001 From: HauTranCong Date: Sun, 26 Jan 2025 12:11:16 +0700 Subject: [PATCH] Practice for working with other files type --- .../screens/assetsmedia/OtherFilesType.dart | 73 +++++++++++++++++++ .../assetsmedia/assets_media_page.dart | 4 + pubspec.lock | 24 ++++++ pubspec.yaml | 1 + 4 files changed, 102 insertions(+) create mode 100644 lib/src/screens/assetsmedia/OtherFilesType.dart diff --git a/lib/src/screens/assetsmedia/OtherFilesType.dart b/lib/src/screens/assetsmedia/OtherFilesType.dart new file mode 100644 index 0000000..551009f --- /dev/null +++ b/lib/src/screens/assetsmedia/OtherFilesType.dart @@ -0,0 +1,73 @@ +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; +import 'dart:io'; + +class OtherFilesType extends StatefulWidget { + @override + _OtherFilesTypeState createState() => _OtherFilesTypeState(); +} + +class _OtherFilesTypeState extends State { + final TextEditingController _controller = TextEditingController(); + String _responseText = ''; + + Future fetchData(String url) async { + try { + final response = await http.get(Uri.parse(url)); // GET method + if (response.statusCode == 200) { + setState(() { + _responseText = response.body; + }); + } else { + setState(() { + _responseText = 'Error: ${response.statusCode}'; + }); + } + } on SocketException { + setState(() { + _responseText = 'Network error: Failed to fetch data'; + }); + } on HttpException { + setState(() { + _responseText = 'HTTP error: Failed to fetch data'; + }); + } on FormatException { + setState(() { + _responseText = 'Format error: Bad response format'; + }); + } catch (e) { + setState(() { + _responseText = 'Unexpected error: $e'; + }); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Other Files Type'), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + TextField( + controller: _controller, + decoration: InputDecoration( + labelText: 'Enter URL', + ), + ), + SizedBox(height: 20), + ElevatedButton( + onPressed: () => fetchData(_controller.text), + child: Text('Fetch Data'), + ), + SizedBox(height: 20), + Text(_responseText), + ], + ), + ), + ); + } +} diff --git a/lib/src/screens/assetsmedia/assets_media_page.dart b/lib/src/screens/assetsmedia/assets_media_page.dart index ce34b53..78aad16 100644 --- a/lib/src/screens/assetsmedia/assets_media_page.dart +++ b/lib/src/screens/assetsmedia/assets_media_page.dart @@ -1,21 +1,25 @@ import 'package:flutter/material.dart'; import 'Images.dart'; import 'Fonts.dart'; +import 'OtherFilesType.dart'; class AssetsMediaPage extends StatelessWidget { final List pages = const [ 'Images', 'Fronts', + 'Other Files Type', ]; final List subtitles = const [ 'Badges show notifications, counts, or status information on navigation items and icons', 'Text fonts', + 'Fetch data from the internet', ]; final List pageWidgets = [ ImagesPage(), FontsPage(), + OtherFilesType(), ]; @override diff --git a/pubspec.lock b/pubspec.lock index 28b3039..ed6cd96 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -80,6 +80,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2" + url: "https://pub.dev" + source: hosted + version: "0.13.6" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" leak_tracker: dependency: transitive description: @@ -205,6 +221,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.3" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" url_launcher: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 1ac4e0a..0eb12a4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 url_launcher: ^6.0.20 + http: ^0.13.3 dev_dependencies: flutter_test: