-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add notes about keys for testing * Add AddressScreen * Tweaks main.dart * Hardcode Android SDK version * Rename VSCode configuration name * Cleanup pubspec.yaml * Cleanup address.dart * Document AddressScreen
- Loading branch information
Showing
13 changed files
with
189 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
ecommerce_app/lib/src/features/address/address_screen.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:ecommerce_app/src/common_widgets/primary_button.dart'; | ||
import 'package:ecommerce_app/src/common_widgets/responsive_scrollable_card.dart'; | ||
import 'package:ecommerce_app/src/constants/app_sizes.dart'; | ||
|
||
/// A page where the user can enter and submit all the address details. | ||
/// * This page is not yet in use. We will add it to the checkout flows later. | ||
class AddressScreen extends StatefulWidget { | ||
const AddressScreen({Key? key, this.onSubmit}) : super(key: key); | ||
final VoidCallback? onSubmit; | ||
|
||
// * Keys for testing using find.byKey() | ||
static const addressKey = Key('address'); | ||
static const townCityKey = Key('townCity'); | ||
static const stateKey = Key('state'); | ||
static const postalCodeKey = Key('postalCode'); | ||
static const countryKey = Key('country'); | ||
static const scrollableKey = Key('addressPageScrollable'); | ||
|
||
@override | ||
_AddressPageState createState() => _AddressPageState(); | ||
} | ||
|
||
class _AddressPageState extends State<AddressScreen> { | ||
final _formKey = GlobalKey<FormState>(); | ||
|
||
final _addressController = TextEditingController(); | ||
final _cityController = TextEditingController(); | ||
final _stateController = TextEditingController(); | ||
final _postalCodeController = TextEditingController(); | ||
final _countryController = TextEditingController(); | ||
|
||
final _isLoading = false; | ||
|
||
Future<void> _submit() async { | ||
if (_formKey.currentState!.validate()) { | ||
FocusScope.of(context).unfocus(); | ||
// TODO: uncomment and submit the address | ||
// final address = Address( | ||
// address: _addressController.value.text, | ||
// city: _cityController.value.text, | ||
// state: _stateController.value.text, | ||
// postalCode: _postalCodeController.value.text, | ||
// country: _countryController.value.text, | ||
// ); | ||
// TODO: Only fire callback if submission is successful | ||
widget.onSubmit?.call(); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ResponsiveScrollableCard( | ||
key: AddressScreen.scrollableKey, | ||
child: Form( | ||
key: _formKey, | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.stretch, | ||
children: [ | ||
AddressFormField( | ||
formFieldKey: AddressScreen.addressKey, | ||
controller: _addressController, | ||
labelText: 'Address'.hardcoded, | ||
keyboardType: TextInputType.streetAddress, | ||
enabled: !_isLoading, | ||
), | ||
gapH8, | ||
AddressFormField( | ||
formFieldKey: AddressScreen.townCityKey, | ||
controller: _cityController, | ||
labelText: 'Town/City'.hardcoded, | ||
keyboardType: TextInputType.streetAddress, | ||
enabled: !_isLoading, | ||
), | ||
gapH8, | ||
AddressFormField( | ||
formFieldKey: AddressScreen.stateKey, | ||
controller: _stateController, | ||
labelText: 'State'.hardcoded, | ||
keyboardType: TextInputType.streetAddress, | ||
enabled: !_isLoading, | ||
), | ||
gapH8, | ||
AddressFormField( | ||
formFieldKey: AddressScreen.postalCodeKey, | ||
controller: _postalCodeController, | ||
labelText: 'Postal Code'.hardcoded, | ||
keyboardType: TextInputType.streetAddress, | ||
enabled: !_isLoading, | ||
), | ||
gapH8, | ||
AddressFormField( | ||
formFieldKey: AddressScreen.countryKey, | ||
controller: _countryController, | ||
labelText: 'Country'.hardcoded, | ||
keyboardType: TextInputType.streetAddress, | ||
enabled: !_isLoading, | ||
), | ||
gapH8, | ||
PrimaryButton( | ||
text: 'Submit'.hardcoded, | ||
onPressed: _submit, | ||
isLoading: _isLoading, | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
// Reusable address form field | ||
class AddressFormField extends StatelessWidget { | ||
const AddressFormField({ | ||
Key? key, | ||
required this.controller, | ||
required this.labelText, | ||
this.keyboardType, | ||
this.enabled = true, | ||
this.formFieldKey, | ||
}) : super(key: key); | ||
|
||
/// Controller used to read out the value in the parent widget | ||
final TextEditingController controller; | ||
final String labelText; | ||
final TextInputType? keyboardType; | ||
|
||
/// Whether the text field is enabled or not | ||
final bool enabled; | ||
|
||
/// Key used in the widget tests | ||
final Key? formFieldKey; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TextFormField( | ||
key: formFieldKey, | ||
controller: controller, | ||
decoration: InputDecoration( | ||
labelText: labelText, | ||
enabled: enabled, | ||
), | ||
autocorrect: true, | ||
textInputAction: TextInputAction.next, | ||
keyboardType: keyboardType, | ||
keyboardAppearance: Brightness.light, | ||
validator: (value) => | ||
value?.isNotEmpty == true ? null : 'Can\'t be empty'.hardcoded, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// Address information used for shipping. | ||
class Address { | ||
const Address({ | ||
required this.address, | ||
required this.city, | ||
required this.state, | ||
required this.postalCode, | ||
required this.country, | ||
}); | ||
|
||
final String address; | ||
final String city; | ||
final String state; | ||
final String postalCode; | ||
final String country; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters