Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/event_placeholder_img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lib/core/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class AnyStepApp extends ConsumerWidget {
highContrastDarkTheme: AnyStepTheme.highContrastDarkTheme,
themeMode: themeMode.hasValue ? themeMode.value : ThemeMode.system,
locale: localeAsync.hasValue ? localeAsync.value : null,
builder:
(context, child) => AppStartupWidget(onLoaded: (context) => child ?? const SizedBox()),
builder: (context, child) =>
AppStartupWidget(onLoaded: (context) => child ?? const SizedBox()),
);
}
}
8 changes: 4 additions & 4 deletions lib/core/common/widgets/dropdown_section.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:anystep/core/common/constants/spacing.dart';
import 'package:flutter/material.dart';

class DropdownSection extends StatefulWidget {
const DropdownSection({
class DropdownText extends StatefulWidget {
const DropdownText({
super.key,
required this.title,
required this.content,
Expand All @@ -16,10 +16,10 @@ class DropdownSection extends StatefulWidget {
final EdgeInsetsGeometry padding;

@override
State<DropdownSection> createState() => _DropdownSectionState();
State<DropdownText> createState() => _DropdownTextState();
}

class _DropdownSectionState extends State<DropdownSection> {
class _DropdownTextState extends State<DropdownText> {
bool _expanded = false;

@override
Expand Down
191 changes: 191 additions & 0 deletions lib/core/common/widgets/inputs/address_autocomplete_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
import 'dart:async';

import 'package:anystep/core/common/constants/spacing.dart';
import 'package:anystep/core/common/utils/log_utils.dart';
import 'package:anystep/core/common/widgets/inputs/any_step_text_field.dart';
import 'package:anystep/core/features/location/data/places_api_client.dart';
import 'package:anystep/core/features/location/domain/places_models.dart';
import 'package:anystep/core/features/location/utils/place_to_address.dart';
import 'package:anystep/l10n/generated/app_localizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class AddressAutocompleteField extends ConsumerStatefulWidget {
const AddressAutocompleteField({
super.key,
required this.formKey,
this.countryCode = 'US',
this.enabled = true,
});

final GlobalKey<FormBuilderState> formKey;
final String countryCode;
final bool enabled;

@override
ConsumerState<AddressAutocompleteField> createState() => _AddressAutocompleteFieldState();
}

class _AddressAutocompleteFieldState extends ConsumerState<AddressAutocompleteField> {
final TextEditingController _controller = TextEditingController();
final FocusNode _focusNode = FocusNode();
Timer? _debounce;
List<PlacesPrediction> _predictions = [];
bool _isLoading = false;
String? _error;

@override
void initState() {
super.initState();
_focusNode.addListener(_handleFocusChange);
}

@override
void dispose() {
_debounce?.cancel();
_controller.dispose();
_focusNode.removeListener(_handleFocusChange);
_focusNode.dispose();
super.dispose();
}

void _handleFocusChange() {
if (!_focusNode.hasFocus) {
setState(() {
_predictions = [];
_error = null;
});
}
}

void _onChanged(String? value) {
_debounce?.cancel();
final query = value?.trim() ?? '';
if (query.isEmpty) {
setState(() {
_predictions = [];
_error = null;
});
return;
}
_debounce = Timer(const Duration(milliseconds: 300), () async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final results = await ref
.read(placesApiClientProvider)
.autocomplete(query, countryCode: widget.countryCode);
if (!mounted) return;
setState(() {
_predictions = results;
_isLoading = false;
});
} catch (e) {
if (!mounted) return;
setState(() {
_predictions = [];
_isLoading = false;
_error = e.toString();
});
}
});
}

Future<void> _selectPrediction(PlacesPrediction prediction) async {
setState(() {
_isLoading = true;
_error = null;
});
try {
final details = await ref.read(placesApiClientProvider).placeDetails(prediction.placeId);
final parsed = placeDetailsToAddress(details);
final form = widget.formKey.currentState;
if (form == null) return;
form.fields['street']?.didChange(parsed.street);
form.fields['streetSecondary']?.didChange(parsed.streetSecondary);
form.fields['city']?.didChange(parsed.city);
form.fields['state']?.didChange(parsed.state);
form.fields['postalCode']?.didChange(parsed.postalCode);
form.fields['zipCode']?.didChange(parsed.postalCode);
form.fields['placeId']?.didChange(parsed.placeId);
form.fields['latitude']?.didChange(parsed.latitude);
form.fields['longitude']?.didChange(parsed.longitude);
form.fields['placeName']?.didChange(parsed.name ?? prediction.description);
_controller.text = prediction.description;
_controller.selection =
TextSelection.fromPosition(TextPosition(offset: _controller.text.length));
_focusNode.unfocus();
setState(() {
_predictions = [];
_isLoading = false;
});
} catch (e, stackTrace) {
Log.e('Places selection error', e, stackTrace);
if (!mounted) return;
setState(() {
_isLoading = false;
_error = e.toString();
});
}
}

@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AnyStepTextField(
name: 'addressSearch',
labelText: loc.searchAddress,
hintText: loc.startTypingAddress,
controller: _controller,
focusNode: _focusNode,
enabled: widget.enabled,
onChanged: _onChanged,
textInputAction: TextInputAction.search,
),
if (_isLoading)
Padding(
padding: const EdgeInsets.only(bottom: AnyStepSpacing.sm4),
child: LinearProgressIndicator(minHeight: 2),
),
if (_error != null)
Padding(
padding: const EdgeInsets.only(bottom: AnyStepSpacing.sm4),
child: Text(
_error!,
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
),
if (_predictions.isEmpty && !_isLoading && _controller.text.trim().isNotEmpty)
Padding(
padding: const EdgeInsets.only(bottom: AnyStepSpacing.sm4),
child: Text(loc.noMatchesFound),
),
if (_predictions.isNotEmpty)
Card(
margin: const EdgeInsets.only(bottom: AnyStepSpacing.sm8),
child: ListView.separated(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: _predictions.length,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (context, index) {
final prediction = _predictions[index];
return ListTile(
title: Text(prediction.mainText ?? prediction.description),
subtitle:
prediction.secondaryText != null ? Text(prediction.secondaryText!) : null,
onTap: () => _selectPrediction(prediction),
);
},
),
),
],
);
}
}
Loading