fixed wording, URL sharing
This commit is contained in:
parent
6a2583c6c3
commit
9b65d50e7a
3 changed files with 93 additions and 19 deletions
|
|
@ -1,18 +1,20 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_map/flutter_map.dart';
|
import 'package:flutter_map/flutter_map.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:latlong2/latlong.dart';
|
import 'package:latlong2/latlong.dart';
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'package:map3terms/src/map3terms_scrambler.dart';
|
import 'package:map3terms/src/map3terms_scrambler.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(WhatFreeWordsApp());
|
runApp(Map3TermsApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
class WhatFreeWordsApp extends StatelessWidget {
|
class Map3TermsApp extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp.router(
|
||||||
title: 'WhatFreeWords Map',
|
title: 'Map3Terms Map',
|
||||||
theme: ThemeData.dark().copyWith(
|
theme: ThemeData.dark().copyWith(
|
||||||
primaryColor: Colors.blue,
|
primaryColor: Colors.blue,
|
||||||
scaffoldBackgroundColor: Colors.black,
|
scaffoldBackgroundColor: Colors.black,
|
||||||
|
|
@ -20,17 +22,20 @@ class WhatFreeWordsApp extends StatelessWidget {
|
||||||
bodyMedium: TextStyle(color: Colors.white),
|
bodyMedium: TextStyle(color: Colors.white),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
home: WhatFreeWordsHome(),
|
routerConfig: _router,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class WhatFreeWordsHome extends StatefulWidget {
|
class Map3TermsHome extends StatefulWidget {
|
||||||
|
final String? initialWords;
|
||||||
|
Map3TermsHome({this.initialWords});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_WhatFreeWordsHomeState createState() => _WhatFreeWordsHomeState();
|
_Map3TermsHomeState createState() => _Map3TermsHomeState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _WhatFreeWordsHomeState extends State<WhatFreeWordsHome> {
|
class _Map3TermsHomeState extends State<Map3TermsHome> {
|
||||||
final TextEditingController _inputController = TextEditingController();
|
final TextEditingController _inputController = TextEditingController();
|
||||||
final MapController _mapController = MapController();
|
final MapController _mapController = MapController();
|
||||||
|
|
||||||
|
|
@ -39,10 +44,30 @@ class _WhatFreeWordsHomeState extends State<WhatFreeWordsHome> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.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<void> _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<void> _handleInput() async {
|
Future<void> _handleInput() async {
|
||||||
final String input = _inputController.text.trim();
|
final String input = _inputController.text.trim();
|
||||||
|
|
||||||
|
|
@ -83,31 +108,34 @@ class _WhatFreeWordsHomeState extends State<WhatFreeWordsHome> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Centers the map when words are entered**
|
/// **Centers the map when terms are entered**
|
||||||
Future<void> _updateMapFromWords() async {
|
Future<void> _updateMapFromWords() async {
|
||||||
final words = _inputController.text.trim().replaceAll(" ", ".");
|
final terms = _inputController.text.trim().replaceAll(" ", ".");
|
||||||
if (words.isEmpty) return;
|
if (terms.isEmpty) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final coords = await wordsToCoord(words);
|
final coords = await wordsToCoord(terms);
|
||||||
setState(() {
|
setState(() {
|
||||||
_center = LatLng(coords[0], coords[1]);
|
_center = LatLng(coords[0], coords[1]);
|
||||||
_mapController.move(_center, _mapController.camera.zoom);
|
_mapController.move(_center, _mapController.camera.zoom);
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_showError("Invalid words format.");
|
_showError("Invalid terms format.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// **Updates the words when the map moves**
|
|
||||||
Future<void> _updateWordsFromCenter() async {
|
Future<void> _updateWordsFromCenter() async {
|
||||||
try {
|
try {
|
||||||
final words = await coordToWords([_center.latitude, _center.longitude]);
|
final terms = await coordToWords([_center.latitude, _center.longitude]);
|
||||||
setState(() {
|
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) {
|
} catch (e) {
|
||||||
print("Error converting coordinates to words: $e");
|
print("Error converting coordinates to terms: $e");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -229,6 +257,22 @@ class _WhatFreeWordsHomeState extends State<WhatFreeWordsHome> {
|
||||||
backgroundColor: Colors.white,
|
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
|
@override
|
||||||
bool shouldRepaint(CustomPainter oldDelegate) => false;
|
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);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
16
pubspec.lock
16
pubspec.lock
|
|
@ -160,6 +160,14 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.2.3"
|
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:
|
http:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
@ -240,6 +248,14 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.5.0"
|
version: "2.5.0"
|
||||||
|
logging:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: logging
|
||||||
|
sha256: c8245ada5f1717ed44271ed1c26b8ce85ca3228fd2ffdb75468ab01979309d61
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.0"
|
||||||
map3terms:
|
map3terms:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ dependencies:
|
||||||
map3terms:
|
map3terms:
|
||||||
git: git@github.com:randogoth/map3terms.git
|
git: git@github.com:randogoth/map3terms.git
|
||||||
geolocator: ^13.0.2
|
geolocator: ^13.0.2
|
||||||
|
go_router: ^14.8.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue