From 9b65d50e7ab3186e6b43ffed0e53520ce03874b3 Mon Sep 17 00:00:00 2001 From: randogoth Date: Tue, 18 Feb 2025 21:45:30 +0200 Subject: [PATCH] fixed wording, URL sharing --- lib/main.dart | 95 ++++++++++++++++++++++++++++++++++++++++----------- pubspec.lock | 16 +++++++++ pubspec.yaml | 1 + 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index d5823c4..36b16f2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,18 +1,20 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_map/flutter_map.dart'; +import 'package:go_router/go_router.dart'; import 'package:latlong2/latlong.dart'; import 'package:geolocator/geolocator.dart'; import 'package:map3terms/src/map3terms_scrambler.dart'; void main() { - runApp(WhatFreeWordsApp()); + runApp(Map3TermsApp()); } -class WhatFreeWordsApp extends StatelessWidget { +class Map3TermsApp extends StatelessWidget { @override Widget build(BuildContext context) { - return MaterialApp( - title: 'WhatFreeWords Map', + return MaterialApp.router( + title: 'Map3Terms Map', theme: ThemeData.dark().copyWith( primaryColor: Colors.blue, scaffoldBackgroundColor: Colors.black, @@ -20,17 +22,20 @@ class WhatFreeWordsApp extends StatelessWidget { bodyMedium: TextStyle(color: Colors.white), ), ), - home: WhatFreeWordsHome(), + routerConfig: _router, ); } } -class WhatFreeWordsHome extends StatefulWidget { +class Map3TermsHome extends StatefulWidget { + final String? initialWords; + Map3TermsHome({this.initialWords}); + @override - _WhatFreeWordsHomeState createState() => _WhatFreeWordsHomeState(); + _Map3TermsHomeState createState() => _Map3TermsHomeState(); } -class _WhatFreeWordsHomeState extends State { +class _Map3TermsHomeState extends State { final TextEditingController _inputController = TextEditingController(); final MapController _mapController = MapController(); @@ -39,10 +44,30 @@ class _WhatFreeWordsHomeState extends State { @override void initState() { super.initState(); - _updateWordsFromCenter(); + WidgetsBinding.instance.addPostFrameCallback((_) async { + if (widget.initialWords != null && widget.initialWords!.isNotEmpty) { + await _initializeFromURL(widget.initialWords!); + } else { + await _updateWordsFromCenter(); + } + }); } - /// **Handles input change: Detects if input is coordinates or words** + Future _initializeFromURL(String terms) async { + terms = terms.replaceAll("-", " "); // Convert URL-friendly format + try { + final coords = await wordsToCoord(terms); + setState(() { + _center = LatLng(coords[0], coords[1]); + _mapController.move(_center, 19.0); + _inputController.text = terms.replaceAll(".", " "); // Show spaces instead of dots + }); + } catch (e) { + print("Error processing terms from URL: $e"); + } + } + + /// **Handles input change: Detects if input is coordinates or terms** Future _handleInput() async { final String input = _inputController.text.trim(); @@ -83,31 +108,34 @@ class _WhatFreeWordsHomeState extends State { } } - /// **Centers the map when words are entered** + /// **Centers the map when terms are entered** Future _updateMapFromWords() async { - final words = _inputController.text.trim().replaceAll(" ", "."); - if (words.isEmpty) return; + final terms = _inputController.text.trim().replaceAll(" ", "."); + if (terms.isEmpty) return; try { - final coords = await wordsToCoord(words); + final coords = await wordsToCoord(terms); setState(() { _center = LatLng(coords[0], coords[1]); _mapController.move(_center, _mapController.camera.zoom); }); } catch (e) { - _showError("Invalid words format."); + _showError("Invalid terms format."); } } - /// **Updates the words when the map moves** Future _updateWordsFromCenter() async { try { - final words = await coordToWords([_center.latitude, _center.longitude]); + final terms = await coordToWords([_center.latitude, _center.longitude]); setState(() { - _inputController.text = words.replaceAll(".", " "); + _inputController.text = terms.replaceAll(".", " "); }); + + // 🌍 Update the browser URL dynamically + final String urlWords = terms.replaceAll(".", "-"); + GoRouter.of(context).go("/?terms=$urlWords"); } catch (e) { - print("Error converting coordinates to words: $e"); + print("Error converting coordinates to terms: $e"); } } @@ -229,6 +257,22 @@ class _WhatFreeWordsHomeState extends State { backgroundColor: Colors.white, ), ), + Positioned( + bottom: 80, + right: 20, + child: FloatingActionButton( + onPressed: () { + final String terms = _inputController.text.replaceAll(" ", "-"); // Convert to URL format + final String shareableUrl = "https://yourapp.com/?terms=$terms"; + Clipboard.setData(ClipboardData(text: shareableUrl)); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Copied to clipboard: $shareableUrl")), + ); + }, + child: Icon(Icons.share, color: Colors.black), + backgroundColor: Colors.white, + ), + ), ], ), ); @@ -272,3 +316,16 @@ class CrossOverlayPainter extends CustomPainter { @override bool shouldRepaint(CustomPainter oldDelegate) => false; } + +final GoRouter _router = GoRouter( + debugLogDiagnostics: true, // Helps debug routing issues + routes: [ + GoRoute( + path: '/', + builder: (context, state) { + final String? terms = state.uri.queryParameters['terms']?.replaceAll("-", "."); + return Map3TermsHome(initialWords: terms); + }, + ), + ], +); \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 919a907..d07a1a9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -160,6 +160,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.2.3" + go_router: + dependency: "direct main" + description: + name: go_router + sha256: "04539267a740931c6d4479a10d466717ca5901c6fdfd3fcda09391bbb8ebd651" + url: "https://pub.dev" + source: hosted + version: "14.8.0" http: dependency: transitive description: @@ -240,6 +248,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.5.0" + logging: + dependency: transitive + description: + name: logging + sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61 + url: "https://pub.dev" + source: hosted + version: "1.3.0" map3terms: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 796c2eb..1ec633f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,6 +39,7 @@ dependencies: map3terms: git: git@github.com:randogoth/map3terms.git geolocator: ^13.0.2 + go_router: ^14.8.0 dev_dependencies: flutter_test: