Skip to content

Commit

Permalink
refactor(widgets): stateful to stateless when possible (#360)
Browse files Browse the repository at this point in the history
* perf(library): convert stateful to stateless

* perf(app): convert stateful to stateless

---------

Co-authored-by: Mateo MARTINEZ <[email protected]>
  • Loading branch information
Thelm76 and Mateo MARTINEZ authored Mar 29, 2024
1 parent 07e010c commit f29570a
Show file tree
Hide file tree
Showing 75 changed files with 540 additions and 1,227 deletions.
2 changes: 1 addition & 1 deletion app/lib/ui/about/about_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AboutScreen extends StatefulWidget {
const AboutScreen({Key? key}) : super(key: key);

@override
_AboutScreenState createState() => _AboutScreenState();
State<AboutScreen> createState() => _AboutScreenState();
}

class _AboutScreenState extends State<AboutScreen> {
Expand Down
15 changes: 2 additions & 13 deletions app/lib/ui/components/app_bars/top/top_app_bar_large.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,10 @@ import 'package:ods_flutter_demo/ui/components/app_bars/top/top_app_bars_customi
import 'package:ods_flutter_demo/ui/theme/theme_selector.dart';
import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

class ComponentTopAppBarLarge extends StatefulWidget {
const ComponentTopAppBarLarge({super.key});
@override
State<ComponentTopAppBarLarge> createState() =>
_ComponentTopAppBarLargeState();
}

class _ComponentTopAppBarLargeState extends State<ComponentTopAppBarLarge> {
class ComponentTopAppBarLarge extends StatelessWidget {
ComponentTopAppBarLarge({super.key});
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ComponentTopAppBarsCustomization(
Expand Down
14 changes: 2 additions & 12 deletions app/lib/ui/components/app_bars/top/top_app_bars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,10 @@ import 'package:ods_flutter_demo/ui/components/app_bars/top/top_app_bars_customi
import 'package:ods_flutter_demo/ui/theme/theme_selector.dart';
import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

class ComponentTopAppBars extends StatefulWidget {
const ComponentTopAppBars({super.key});
@override
State<ComponentTopAppBars> createState() => _ComponentTopAppBarsState();
}

class _ComponentTopAppBarsState extends State<ComponentTopAppBars> {
class ComponentTopAppBars extends StatelessWidget {
ComponentTopAppBars({super.key});
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ComponentTopAppBarsCustomization(
Expand Down
45 changes: 17 additions & 28 deletions app/lib/ui/components/buttons/button_contained.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,36 @@ enum ButtonEmphasis {
functional,
}

class ButtonsContained extends StatefulWidget {
final ButtonEmphasis emphasis;

ButtonsContained({Key? key, required this.emphasis}) : super(key: key);
class ButtonsContained extends StatelessWidget {
const ButtonsContained({Key? key, required this.emphasis}) : super(key: key);

@override
_ButtonsContainedState createState() => _ButtonsContainedState();
}

class _ButtonsContainedState extends State<ButtonsContained> {
_ButtonsContainedState();
final ButtonEmphasis emphasis;

@override
Widget build(BuildContext context) {
String getAppBarTitle() {
switch (emphasis) {
case ButtonEmphasis.highEmphasis:
return AppLocalizations.of(context)!.buttonsHighEmphasisVariantTitle;
case ButtonEmphasis.mediumEmphasis:
return AppLocalizations.of(context)!
.buttonsMediumEmphasisVariantTitle;
case ButtonEmphasis.functional:
return AppLocalizations.of(context)!.buttonsFunctionalVariantTitle;
}
}

return ButtonCustomization(
child: Scaffold(
bottomSheet: OdsSheetsBottom(
sheetContent: _CustomizationContent(emphasis: widget.emphasis),
sheetContent: _CustomizationContent(emphasis: emphasis),
title: AppLocalizations.of(context)!.componentCustomizeTitle,
),
appBar: MainAppBar(getAppBarTitle()),
body: SafeArea(child: _Body(emphasis: widget.emphasis)),
body: SafeArea(child: _Body(emphasis: emphasis)),
),
);
}

String getAppBarTitle() {
switch (widget.emphasis) {
case ButtonEmphasis.highEmphasis:
return AppLocalizations.of(context)!.buttonsHighEmphasisVariantTitle;
case ButtonEmphasis.mediumEmphasis:
return AppLocalizations.of(context)!.buttonsMediumEmphasisVariantTitle;
case ButtonEmphasis.functional:
return AppLocalizations.of(context)!.buttonsFunctionalVariantTitle;
}
}
}

class _Body extends StatelessWidget {
Expand Down Expand Up @@ -118,11 +112,6 @@ class _CustomizationContent extends StatefulWidget {
class _CustomizationContentState extends State<_CustomizationContent> {
_CustomizationContentState();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
final ButtonCustomizationState? customizationState =
Expand Down
14 changes: 2 additions & 12 deletions app/lib/ui/components/buttons/button_outlined.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,11 @@ import 'package:ods_flutter/guidelines/spacings.dart';
import 'package:ods_flutter_demo/ui/components/buttons/button_customization.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';

class ButtonsOutlined extends StatefulWidget {
const ButtonsOutlined({super.key});
class ButtonsOutlined extends StatelessWidget {
ButtonsOutlined({super.key});

@override
State<ButtonsOutlined> createState() => _ButtonsOutlinedState();
}

class _ButtonsOutlinedState extends State<ButtonsOutlined> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ButtonCustomization(
Expand Down
19 changes: 2 additions & 17 deletions app/lib/ui/components/buttons/button_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,11 @@ import 'package:ods_flutter_demo/ui/components/buttons/button_customization.dart
import 'package:ods_flutter_demo/ui/components/buttons/button_enum.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';

class ButtonsText extends StatefulWidget {
const ButtonsText({super.key});
class ButtonsText extends StatelessWidget {
ButtonsText({super.key});

@override
State<ButtonsText> createState() => _ButtonsTextState();
}

class _ButtonsTextState extends State<ButtonsText> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ButtonCustomization(
Expand Down Expand Up @@ -95,11 +85,6 @@ class _CustomizationContent extends StatefulWidget {
class _CustomizationContentState extends State<_CustomizationContent> {
_CustomizationContentState();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
final ButtonCustomizationState? customizationState =
Expand Down
19 changes: 2 additions & 17 deletions app/lib/ui/components/buttons/buttons_icon/button_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,11 @@ import 'package:ods_flutter_demo/ui/components/buttons/buttons_icon/button_icon_
import 'package:ods_flutter_demo/ui/components/buttons/buttons_icon/button_icon_enum.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';

class ButtonsIcons extends StatefulWidget {
const ButtonsIcons({super.key});
class ButtonsIcons extends StatelessWidget {
ButtonsIcons({super.key});

@override
State<ButtonsIcons> createState() => _ButtonsIconsState();
}

class _ButtonsIconsState extends State<ButtonsIcons> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return ButtonIconCustomization(
Expand Down Expand Up @@ -126,11 +116,6 @@ class _CustomizationContent extends StatefulWidget {
class _CustomizationContentState extends State<_CustomizationContent> {
_CustomizationContentState();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
final ButtonIconCustomizationState? customizationState =
Expand Down
14 changes: 2 additions & 12 deletions app/lib/ui/components/buttons/segmented/segmented_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,11 @@ import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

enum Foods { ham, milk, figs, eggs, oil }

class SegmentedButtons extends StatefulWidget {
const SegmentedButtons({super.key});
class SegmentedButtons extends StatelessWidget {
SegmentedButtons({super.key});

@override
State<SegmentedButtons> createState() => _SegmentedButtonsState();
}

class _SegmentedButtonsState extends State<SegmentedButtons> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return SegmentedButtonCustomization(
Expand Down
14 changes: 2 additions & 12 deletions app/lib/ui/components/cards/card_horizontal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,11 @@ import 'package:ods_flutter_demo/ui/components/cards/card_enum.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';
import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

class CardHorizontal extends StatefulWidget {
const CardHorizontal({super.key});
class CardHorizontal extends StatelessWidget {
CardHorizontal({super.key});

@override
State<CardHorizontal> createState() => _CardHorizontalState();
}

class _CardHorizontalState extends State<CardHorizontal> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return CardCustomization(
Expand Down
14 changes: 2 additions & 12 deletions app/lib/ui/components/cards/card_small.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@ import 'package:ods_flutter_demo/main.dart';
import 'package:ods_flutter_demo/ui/components/cards/card_customization.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';

class CardSmall extends StatefulWidget {
const CardSmall({super.key});
class CardSmall extends StatelessWidget {
CardSmall({super.key});

@override
State<CardSmall> createState() => _CardSmallState();
}

class _CardSmallState extends State<CardSmall> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return CardCustomization(
Expand Down
31 changes: 6 additions & 25 deletions app/lib/ui/components/cards/card_vertical_header_first.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,11 @@ import 'package:ods_flutter_demo/ui/components/cards/card_customization.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';
import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

class CardVerticalHeaderFirst extends StatefulWidget {
const CardVerticalHeaderFirst({super.key});
class CardVerticalHeaderFirst extends StatelessWidget {
CardVerticalHeaderFirst({super.key});

@override
State<CardVerticalHeaderFirst> createState() =>
_CardVerticalHeaderFirstState();
}

class _CardVerticalHeaderFirstState extends State<CardVerticalHeaderFirst> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return CardCustomization(
Expand Down Expand Up @@ -107,8 +96,8 @@ class _Body extends StatelessWidget {
: null,
title: recipe.title,
subtitle:
customizationState?.hasSubtitle == true ? recipe.subtitle : null,
text: customizationState?.hasText == true ? recipe.description : null,
customizationState.hasSubtitle == true ? recipe.subtitle : null,
text: customizationState.hasText == true ? recipe.description : null,
image: OdsCardImage(
imageProvider: NetworkImage(recipe.url),
contentDescription: '', //Optional
Expand All @@ -117,22 +106,14 @@ class _Body extends StatelessWidget {
),
firstButton: firstButton,
secondButton: secondButton,
onClick: customizationState!.clickable ? () {} : null,
onClick: customizationState.clickable ? () {} : null,
),
),
);
}
}

class _CustomizationContent extends StatefulWidget {
@override
State<_CustomizationContent> createState() => _CustomizationContentState();
}

class _CustomizationContentState extends State<_CustomizationContent> {
int selectedIndex = 0;
bool isFiltered = true;

class _CustomizationContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
final CardCustomizationState? customizationState =
Expand Down
24 changes: 3 additions & 21 deletions app/lib/ui/components/cards/card_vertical_image_first.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,11 @@ import 'package:ods_flutter_demo/ui/components/cards/card_customization.dart';
import 'package:ods_flutter_demo/ui/main_app_bar.dart';
import 'package:ods_flutter_demo/ui/utilities/component_count_row.dart';

class CardVerticalImageFirst extends StatefulWidget {
const CardVerticalImageFirst({super.key});
class CardVerticalImageFirst extends StatelessWidget {
CardVerticalImageFirst({super.key});

@override
State<CardVerticalImageFirst> createState() => _CardVerticalImageFirstState();
}

class _CardVerticalImageFirstState extends State<CardVerticalImageFirst> {
final _scaffoldKey = GlobalKey<ScaffoldState>();

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return CardCustomization(
Expand Down Expand Up @@ -122,15 +112,7 @@ class _Body extends StatelessWidget {
}
}

class _CustomizationContent extends StatefulWidget {
@override
State<_CustomizationContent> createState() => _CustomizationContentState();
}

class _CustomizationContentState extends State<_CustomizationContent> {
int selectedIndex = 0;
bool isFiltered = true;

class _CustomizationContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
final CardCustomizationState? customizationState =
Expand Down
Loading

0 comments on commit f29570a

Please sign in to comment.