Add : Web file gethering

This commit is contained in:
rl544
2025-05-09 16:56:41 +09:00
parent 2922726916
commit caa2502a1e
4 changed files with 283 additions and 124 deletions
-4
View File
@@ -19,7 +19,3 @@ subprojects {
tasks.register<Delete>("clean") { tasks.register<Delete>("clean") {
delete(rootProject.layout.buildDirectory) delete(rootProject.layout.buildDirectory)
} }
android {
ndkVersion = "27.0.12077973"
...
}
+257 -119
View File
@@ -4,35 +4,54 @@ import 'package:flutter/services.dart' show rootBundle;
import 'package:window_size/window_size.dart'; import 'package:window_size/window_size.dart';
import 'package:desktop_window/desktop_window.dart'; import 'package:desktop_window/desktop_window.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart'; import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:http/http.dart' as http;
import 'package:csv/csv.dart';
void main() { void main() {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized(); WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
setWindowTitle('하나랜덤식당'); setWindowTitle('하나랜덤식당');
DesktopWindow.setWindowSize(Size(480,740)); DesktopWindow.setWindowSize(Size(480, 740));
//FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding); //FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
FlutterNativeSplash.remove(); FlutterNativeSplash.remove();
runApp(RandomSelectorApp()); runApp(RandomSelectorApp());
} }
Future<String> fetchCsvData(String url) async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return response.body;
} else {
print('Failed to fetch CSV: ${response.statusCode}');
return ''; // Or throw an error
}
} catch (e) {
print('Error fetching CSV: $e');
return ''; // Or throw an error
}
}
class RandomSelectorApp extends StatelessWidget { class RandomSelectorApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(title: '명동 식당고르기', home: RandomSelectorScreen());
title: '명동 식당고르기',
home: RandomSelectorScreen(),
);
} }
} }
class RandomSelectorScreen extends StatefulWidget { class RandomSelectorScreen extends StatefulWidget {
final String csvUrl =
'https://raw.githubusercontent.com/rl544/HanaRandomFood/refs/heads/main/assets/list.csv'; // Replace with the actual URL
@override @override
_RandomSelectorScreenState createState() => _RandomSelectorScreenState(); _RandomSelectorScreenState createState() => _RandomSelectorScreenState();
} }
class _RandomSelectorScreenState extends State<RandomSelectorScreen> with SingleTickerProviderStateMixin { class _RandomSelectorScreenState extends State<RandomSelectorScreen>
with SingleTickerProviderStateMixin {
List<String> items = []; List<String> items = [];
List<String> _csvData = []; // List<List<dynamic>> _csvData = [];
List<Map<String, dynamic>> _csvDataWithHeader = [];
// List<dynamic>? selectedItem;
String? selectedItem; String? selectedItem;
int _key = 0; int _key = 0;
Color _color1 = const Color.fromARGB(255, 242, 240, 245); Color _color1 = const Color.fromARGB(255, 242, 240, 245);
@@ -40,6 +59,7 @@ class _RandomSelectorScreenState extends State<RandomSelectorScreen> with Single
late AnimationController _controller; late AnimationController _controller;
late Animation<double> _rotationAnimation; late Animation<double> _rotationAnimation;
bool _loading = true; bool _loading = true;
String _errorMessage = '';
@override @override
void initState() { void initState() {
@@ -48,24 +68,114 @@ class _RandomSelectorScreenState extends State<RandomSelectorScreen> with Single
duration: Duration(milliseconds: 600), duration: Duration(milliseconds: 600),
vsync: this, vsync: this,
); );
_rotationAnimation = Tween<double>(begin: 0, end: 2 * pi).animate( _rotationAnimation = Tween<double>(
CurvedAnimation(parent: _controller, curve: Curves.elasticOut), begin: 0,
); end: 2 * pi,
_loadCSV(); ).animate(CurvedAnimation(parent: _controller, curve: Curves.elasticOut));
_loadCsvData(); // from Web
// _loadCSV(); // from internal
} }
Future<void> _loadCsvData() async {
setState(() {
_loading = true;
_errorMessage = '';
});
final csvString = await fetchCsvData(widget.csvUrl);
if (csvString.isNotEmpty) {
setState(() {
// _csvData = parseCsv(csvString);
_csvData = parseLCsv(csvString);
_csvDataWithHeader = parseCsvWithHeader(csvString);
_loading = false;
});
} else {
setState(() {
_loading = false;
_errorMessage = 'Failed to load CSV data.';
});
}
}
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(title: Text('CSV Data from Web')),
// body:
// _loading
// ? Center(child: CircularProgressIndicator())
// : _errorMessage.isNotEmpty
// ? Center(child: Text(_errorMessage))
// : SingleChildScrollView(
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: [
// Text(
// 'Raw CSV Data:',
// style: TextStyle(fontWeight: FontWeight.bold),
// ),
// for (var row in _csvData) Text(row.join(', ')),
// SizedBox(height: 20),
// Text(
// 'CSV Data with Header:',
// style: TextStyle(fontWeight: FontWeight.bold),
// ),
// if (_csvDataWithHeader.isNotEmpty)
// for (var row in _csvDataWithHeader) Text(row.toString()),
// if (_csvDataWithHeader.isEmpty && _csvData.isNotEmpty)
// Text('No header row detected (using basic parsing).'),
// if (_csvData.isEmpty && _errorMessage.isEmpty)
// Text('No CSV data loaded yet.'),
// ],
// ),
// ),
// );
// }
Future<void> _loadCSV() async { Future<void> _loadCSV() async {
final csvString = await rootBundle.loadString('assets/list.csv'); final csvString = await rootBundle.loadString('assets/list.csv');
setState(() { setState(() {
items = csvString items = parseLCsv(csvString);
.split('\n')
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.toList();
_loading = false; _loading = false;
}); });
} }
List<String> parseLCsv(String csvString) {
final List<String> ans =
csvString
.split('\n')
.map((e) => e.trim())
.where((e) => e.isNotEmpty)
.toList();
return ans;
}
List<List<dynamic>> parseCsv(String csvString) {
final List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter()
.convert(csvString);
return rowsAsListOfValues;
}
List<Map<String, dynamic>> parseCsvWithHeader(String csvString) {
final List<List<dynamic>> rows = const CsvToListConverter().convert(
csvString,
);
if (rows.isEmpty) {
return [];
}
final List<String> header =
rows.first.map((item) => item.toString()).toList();
final List<Map<String, dynamic>> data = [];
for (int i = 1; i < rows.length; i++) {
final Map<String, dynamic> rowData = {};
for (int j = 0; j < header.length; j++) {
rowData[header[j]] = rows[i][j];
}
data.add(rowData);
}
return data;
}
@override @override
void dispose() { void dispose() {
_controller.dispose(); _controller.dispose();
@@ -73,13 +183,17 @@ class _RandomSelectorScreenState extends State<RandomSelectorScreen> with Single
} }
void selectRandomItem() { void selectRandomItem() {
if (items.isEmpty) return; if (_csvData.isEmpty) return; // _csvData items
final random = Random(); final random = Random();
setState(() { setState(() {
selectedItem = items[random.nextInt(items.length)]; selectedItem = _csvData[random.nextInt(_csvData.length)];
_key++; _key++;
_color1 = Color((random.nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.2); _color1 = Color(
_color2 = Color((random.nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.2); (random.nextDouble() * 0xFFFFFF).toInt(),
).withOpacity(0.2);
_color2 = Color(
(random.nextDouble() * 0xFFFFFF).toInt(),
).withOpacity(0.2);
}); });
_controller.forward(from: 0); _controller.forward(from: 0);
} }
@@ -87,7 +201,6 @@ class _RandomSelectorScreenState extends State<RandomSelectorScreen> with Single
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: AnimatedContainer( body: AnimatedContainer(
duration: Duration(milliseconds: 1200), duration: Duration(milliseconds: 1200),
decoration: BoxDecoration( decoration: BoxDecoration(
@@ -98,124 +211,149 @@ class _RandomSelectorScreenState extends State<RandomSelectorScreen> with Single
), ),
), ),
child: Center( child: Center(
child: _loading child:
? CircularProgressIndicator() _loading
: Column( ? CircularProgressIndicator()
mainAxisAlignment: MainAxisAlignment.center, : Column(
children: [ mainAxisAlignment: MainAxisAlignment.center,
ElevatedButton( children: [
onPressed: null, ElevatedButton(
style: ElevatedButton.styleFrom( onPressed: null,
backgroundColor: Colors.white, style: ElevatedButton.styleFrom(
foregroundColor: Colors.white, backgroundColor: Colors.white,
elevation: 6, foregroundColor: Colors.white,
shape: RoundedRectangleBorder( elevation: 6,
borderRadius: BorderRadius.circular(10), shape: RoundedRectangleBorder(
), borderRadius: BorderRadius.circular(10),
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 16), ),
textStyle: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), padding: EdgeInsets.symmetric(
), horizontal: 32,
child: Text('하나은행 IT시스템부 단말인프라팀', style: TextStyle(color: Colors.white, vertical: 16,
shadows: [ ),
Shadow( textStyle: TextStyle(
blurRadius: 18.0, fontSize: 24,
color: Colors.black.withOpacity(0.6),
offset: Offset(0, 0),
),
Shadow(
blurRadius: 8.0,
color: Colors.black.withOpacity(0.5),
offset: Offset(2, 2),
),
],
),
),
),
SizedBox(height: 150),
Padding(
padding: const EdgeInsets.only(bottom: 32.0),
child: Image.asset(
'assets/images/ask.png',
width: 100,
height: 100,
),
),
AnimatedBuilder(
animation: _rotationAnimation,
builder: (context, child) {
return Transform.rotate(
angle: _rotationAnimation.value,
child: child,
);
},
child: AnimatedSwitcher(
duration: Duration(milliseconds: 500),
transitionBuilder: (Widget child, Animation<double> animation) {
return ScaleTransition(
scale: animation,
child: FadeTransition(
opacity: animation,
child: child,
),
);
},
child: Text(
selectedItem ?? '오늘의 식당을 골라주세요!',
key: ValueKey<int>(_key),
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
),
),
child: Text(
'하나은행 IT시스템부 단말인프라팀',
style: TextStyle(
color: Colors.white, color: Colors.white,
shadows: [ shadows: [
Shadow( Shadow(
blurRadius: 18.0, blurRadius: 18.0,
color: Colors.black.withOpacity(0.7), color: Colors.black.withOpacity(0.6),
offset: Offset(0, 0), offset: Offset(0, 0),
), ),
Shadow( Shadow(
blurRadius: 8.0, blurRadius: 8.0,
color: _color1.withOpacity(0.7), color: Colors.black.withOpacity(0.5),
offset: Offset(2, 2), offset: Offset(2, 2),
), ),
], ],
), ),
textAlign: TextAlign.center,
), ),
), ),
), SizedBox(height: 150),
SizedBox(height: 50), Padding(
ElevatedButton( padding: const EdgeInsets.only(bottom: 32.0),
style: ElevatedButton.styleFrom( child: Image.asset(
backgroundColor: const Color.fromARGB(255, 101, 177, 130), 'assets/images/ask.png',
foregroundColor: _color1, width: 100,
elevation: 10, height: 100,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
), ),
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 16),
textStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
), ),
onPressed: selectRandomItem, AnimatedBuilder(
child: Text('어디로 갈까요?', animation: _rotationAnimation,
style: TextStyle( builder: (context, child) {
color: Colors.white, return Transform.rotate(
shadows: [ angle: _rotationAnimation.value,
Shadow( child: child,
blurRadius: 18.0, );
color: Colors.black.withOpacity(0.4), },
offset: Offset(0, 0), child: AnimatedSwitcher(
duration: Duration(milliseconds: 500),
transitionBuilder: (
Widget child,
Animation<double> animation,
) {
return ScaleTransition(
scale: animation,
child: FadeTransition(
opacity: animation,
child: child,
),
);
},
child: Text(
selectedItem ?? '오늘의 식당을 골라주세요!',
key: ValueKey<int>(_key),
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 18.0,
color: Colors.black.withOpacity(0.7),
offset: Offset(0, 0),
),
Shadow(
blurRadius: 8.0,
color: _color1.withOpacity(0.7),
offset: Offset(2, 2),
),
],
), ),
Shadow( textAlign: TextAlign.center,
blurRadius: 8.0, ),
color: _color1.withOpacity(0.5),
offset: Offset(2, 2),
),
],
), ),
), ),
), SizedBox(height: 50),
], ElevatedButton(
), style: ElevatedButton.styleFrom(
backgroundColor: const Color.fromARGB(
255,
101,
177,
130,
),
foregroundColor: _color1,
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 16,
),
textStyle: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
onPressed: selectRandomItem,
child: Text(
'어디로 갈까요?',
style: TextStyle(
color: Colors.white,
shadows: [
Shadow(
blurRadius: 18.0,
color: Colors.black.withOpacity(0.4),
offset: Offset(0, 0),
),
Shadow(
blurRadius: 8.0,
color: _color1.withOpacity(0.5),
offset: Offset(2, 2),
),
],
),
),
),
],
),
), ),
), ),
); );
+24
View File
@@ -184,6 +184,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.15.6" version: "0.15.6"
http:
dependency: "direct main"
description:
name: http
sha256: "2c11f3f94c687ee9bad77c171151672986360b2b001d109814ee7140b2cf261b"
url: "https://pub.dev"
source: hosted
version: "1.4.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
url: "https://pub.dev"
source: hosted
version: "4.1.2"
image: image:
dependency: transitive dependency: transitive
description: description:
@@ -365,6 +381,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "14.3.1" version: "14.3.1"
web:
dependency: transitive
description:
name: web
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
url: "https://pub.dev"
source: hosted
version: "1.1.1"
window_size: window_size:
dependency: "direct main" dependency: "direct main"
description: description:
+1
View File
@@ -34,6 +34,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8 cupertino_icons: ^1.0.8
http: ^1.2.1
csv: ^6.0.0 csv: ^6.0.0
flutter_launcher_icons: ^0.14.3 flutter_launcher_icons: ^0.14.3
flutter_native_splash: ^2.4.6 flutter_native_splash: ^2.4.6