Skip to content

Commit

Permalink
Practice for working with other files type
Browse files Browse the repository at this point in the history
  • Loading branch information
HauTranCong committed Jan 26, 2025
1 parent abf6896 commit 2776162
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
73 changes: 73 additions & 0 deletions lib/src/screens/assetsmedia/OtherFilesType.dart
Original file line number Diff line number Diff line change
@@ -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<OtherFilesType> {
final TextEditingController _controller = TextEditingController();
String _responseText = '';

Future<void> 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),
],
),
),
);
}
}
4 changes: 4 additions & 0 deletions lib/src/screens/assetsmedia/assets_media_page.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import 'package:flutter/material.dart';
import 'Images.dart';
import 'Fonts.dart';
import 'OtherFilesType.dart';

class AssetsMediaPage extends StatelessWidget {
final List<String> pages = const [
'Images',
'Fronts',
'Other Files Type',
];

final List<String> subtitles = const [
'Badges show notifications, counts, or status information on navigation items and icons',
'Text fonts',
'Fetch data from the internet',
];

final List<Widget> pageWidgets = [
ImagesPage(),
FontsPage(),
OtherFilesType(),
];

@override
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 2776162

Please sign in to comment.