Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input field improvement #2

Merged
merged 9 commits into from
Feb 12, 2024
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
19 changes: 17 additions & 2 deletions src/pocket_note/lib/core/pocket_note_app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:pocket_note/ui/screens/resources/strings.dart';

Expand All @@ -11,16 +12,30 @@ class PocketNoteApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
return _buildDynamicColorWidget(lightDynamic, darkDynamic);
});
}

Widget _buildDynamicColorWidget(
ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme defaultColorScheme =
ColorScheme.fromSeed(seedColor: Colors.deepPurple);

ColorScheme lightColors = lightDynamic ?? defaultColorScheme;
ColorScheme darkColors = darkDynamic ?? defaultColorScheme;

return MaterialApp.router(
title: appName,
theme: ThemeData(
colorSchemeSeed: Colors.deepPurple,
colorScheme: lightColors,
useMaterial3: true,
brightness: Brightness.light,
typography: Typography(),
),
darkTheme: ThemeData(
colorSchemeSeed: Colors.deepPurple,
colorScheme: darkColors,
useMaterial3: true,
brightness: Brightness.dark,
typography: Typography(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:pocket_note/core/di/injection.dart';
import 'package:pocket_note/ui/screens/addpowerbill/store/add_power_bill_store.dart';
import 'package:pocket_note/ui/widgets/custom_scaffold.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import '../../widgets/add_power_bill_form.dart';
import '../../widgets/add_power_bill_form_content.dart';
import '../../widgets/loader.dart';
import '../resources/strings.dart';

Expand All @@ -21,6 +21,8 @@ class _AddPowerBillScreenState extends State<AddPowerBillScreen> {

bool _shouldUpdatePreviousScreen = false;

final _formKey = GlobalKey<FormState>();

@override
void initState() {
_store = getIt();
Expand All @@ -41,13 +43,16 @@ class _AddPowerBillScreenState extends State<AddPowerBillScreen> {
builder: (_) => Stack(
children: [
Loader(isLoading: _store.uiState.isLoading),
AddPowerBillForm(
initialMonthName: _store.monthName,
onCurrentReadingChanged: _store.setCurrentReading,
onLastReadingChanged: _store.setLastReading,
onMonthSelected: _store.setMonth,
onNeighborsTotalReadingChanged: _store.setNeighborsTotalReading,
onNeighborsTotalValueChanged: _store.setNeighborsTotalValue,
Form(
key: _formKey,
child: AddPowerBillFormContent(
initialMonthName: _store.monthName,
onCurrentReadingChanged: _store.setCurrentReading,
onLastReadingChanged: _store.setLastReading,
onMonthSelected: _store.setMonth,
onNeighborsTotalReadingChanged: _store.setNeighborsTotalReading,
onNeighborsTotalValueChanged: _store.setNeighborsTotalValue,
),
),
],
),
Expand All @@ -56,29 +61,34 @@ class _AddPowerBillScreenState extends State<AddPowerBillScreen> {
}

_save() {
_store.save().then(
(value) => _processAddPowerBillResult(value),
);
if (_formKey.currentState?.validate() == true) {
_store.save().then(
(value) => _processAddPowerBillResult(value),
);
} else {
_showFeedback(false);
}
}

_processAddPowerBillResult(bool result) {
if (result) {
_shouldUpdatePreviousScreen = true;
}

SnackBar snackBar = _buildSnackBar(result);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
_showFeedback(result);
}

SnackBar _buildSnackBar(bool success) {
_showFeedback(bool success) {
SnackBar snackBar;
if (success) {
return const SnackBar(
snackBar = const SnackBar(
content: Text(addPowerBillSuccessfulMessage),
);
} else {
return const SnackBar(
snackBar = const SnackBar(
content: Text(addPowerBillErrorMessage),
);
}
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
1 change: 0 additions & 1 deletion src/pocket_note/lib/ui/screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class HomeScreen extends StatelessWidget {
StackRouter router = AutoRouter.of(context);
return CustomScaffold(
appBarTitle: appName,
backEvent: () => router.pop(),
body: SizedBox(
width: double.maxFinite,
height: double.maxFinite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class _PowerBillsScreenState extends State<PowerBillScreen> {
Widget build(BuildContext context) {
StackRouter router = AutoRouter.of(context);
return CustomScaffold(
edgeInsets: const EdgeInsets.only(
left: 12.0,
right: 12.0,
top: 0.0,
bottom: 0.0,
),
backEvent: () => router.pop(),
appBarTitle: "Contas de Luz",
body: Observer(
Expand Down
1 change: 1 addition & 0 deletions src/pocket_note/lib/ui/screens/resources/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ const String addPowerBillSuccessfulMessage = "Conta de luz salva com sucesso!";
const String addPowerBillErrorMessage = "Ocorreu um erro. Tente mais tarde!";
const String retry = "Repetir";
const String house = "Casa";
const String invalidInputNumber = "Campo inválido";
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import '../screens/resources/strings.dart';
import 'month_selector.dart';
import 'number_text_input_field.dart';

class AddPowerBillForm extends StatelessWidget {
class AddPowerBillFormContent extends StatelessWidget {
final ValueChanged<String?>? onMonthSelected;
final Function(String?) onCurrentReadingChanged;
final Function(String?) onLastReadingChanged;
final Function(String?) onNeighborsTotalReadingChanged;
final Function(String?) onNeighborsTotalValueChanged;
final String? initialMonthName;

const AddPowerBillForm({
const AddPowerBillFormContent({
super.key,
required this.onMonthSelected,
required this.onCurrentReadingChanged,
Expand Down Expand Up @@ -43,51 +43,42 @@ class AddPowerBillForm extends StatelessWidget {
)
],
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
Text(
house,
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
NumberTextInputField(
labelText: currentReadingInKWm,
onChanged: onCurrentReadingChanged,
autofocus: true,
textInputAction: TextInputAction.next,
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
NumberTextInputField(
labelText: previousReadingInKWm,
onChanged: onLastReadingChanged,
textInputAction: TextInputAction.next,
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
const Divider(),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
Text(
neighborhood,
style: Theme.of(context).textTheme.labelLarge,
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
NumberTextInputField(
labelText: totalConsumptionInKWm,
onChanged: onNeighborsTotalReadingChanged,
textInputAction: TextInputAction.next,
),
const SizedBox(
height: 12,
),
const SizedBox(height: 12),
NumberTextInputField(
labelText: totalCurrencyValue,
onChanged: onNeighborsTotalValueChanged,
textInputAction: TextInputAction.done,
),
],
),
Expand Down
17 changes: 10 additions & 7 deletions src/pocket_note/lib/ui/widgets/custom_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ class CustomScaffold extends StatelessWidget {
final Widget body;
final VoidCallback? backEvent;
final FloatingActionButton? floatingActionButton;
final EdgeInsets edgeInsets;

const CustomScaffold(
{super.key,
required this.appBarTitle,
required this.body,
this.backEvent,
this.floatingActionButton});
const CustomScaffold({
super.key,
required this.appBarTitle,
required this.body,
this.backEvent,
this.floatingActionButton,
this.edgeInsets = const EdgeInsets.symmetric(vertical: 20, horizontal: 16),
});

@override
Widget build(BuildContext context) {
Expand All @@ -27,7 +30,7 @@ class CustomScaffold extends StatelessWidget {
title: Text(appBarTitle),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 16),
padding: edgeInsets,
child: body,
),
floatingActionButton: floatingActionButton,
Expand Down
23 changes: 16 additions & 7 deletions src/pocket_note/lib/ui/widgets/number_text_input_field.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pocket_note/core/extensions/string_ext.dart';
import 'package:pocket_note/ui/screens/resources/strings.dart';

class NumberTextInputField extends StatelessWidget {
final String labelText;
final Function(String?) onChanged;
final TextInputAction? textInputAction;
final bool autofocus;

const NumberTextInputField(
{super.key, required this.labelText, required this.onChanged});
const NumberTextInputField({
super.key,
required this.labelText,
required this.onChanged,
this.textInputAction,
this.autofocus = false,
});

@override
Widget build(BuildContext context) {
return TextFormField(
keyboardType: TextInputType.number,
onChanged: (text) {
if (text.parseToDouble() != null) {
onChanged(text);
}
},
textInputAction: textInputAction,
autofocus: autofocus,
onChanged: onChanged,
inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[0-9.]"))],
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) =>
value?.parseToDouble() == null ? invalidInputNumber : null,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: labelText,
Expand Down
3 changes: 2 additions & 1 deletion src/pocket_note/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.1+3
version: 1.0.1+5

environment:
sdk: '>=3.2.0 <4.0.0'
Expand Down Expand Up @@ -50,6 +50,7 @@ dependencies:
sqflite_common_ffi_web: ^0.4.2+3
path: ^1.8.3
share_plus: ^6.3.4 #latest version is breaking
dynamic_color: ^1.6.9

dev_dependencies:
injectable_generator: ^2.3.2
Expand Down
Loading