Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blaws56 committed Oct 2, 2021
0 parents commit c25b956
Show file tree
Hide file tree
Showing 13 changed files with 528 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
@@ -0,0 +1,75 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/ephemeral
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
10 changes: 10 additions & 0 deletions .metadata
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: de080169131aa1d98f397388bdff299e37e9583a
channel: master

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,3 @@
## 0.0.1

* TODO: Describe initial release.
8 changes: 8 additions & 0 deletions LICENSE
@@ -0,0 +1,8 @@
Copyright (c) 2021 Brett Laws. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 changes: 62 additions & 0 deletions README.md
@@ -0,0 +1,62 @@
A simple library for applying the Humble Object Pattern in order to create cleaner, more testable widgets.



## Features



This package might be helpful to you if:
- a high level of test coverage is required
- you apply test driven development to app development
- you find it difficult to separate UI logic from appearance
- the widgets you create depend on too many details


## Usage

Using the library will feel very familiar. Let's start by creating a simple button as a <b>HumbleStatelessWidget</b>.

1) Write a test! (skip ahead if you still haven't been converted to TDD 😜). Be sure to include tests for all logic that is important to you. This may include strings, colors, padding -- anything that is necessary to make the widget "right".
```dart
bool testSimpleButtonLogic(){
bool tapped = false;
final logic = SimpleButtonLogic(text: 'test', onTap: () {
tapped = true;
});
if (logic.text != 'test') return false;
logic.onTap();
if (tapped == false) return false;
return true;
}
```
2) Create a plain old Dart object to represent the UI element's logic.
```dart
class SimpleButtonLogic {
SimpleButtonLogic({required this.text, required this.onTap});
final String text;
final Function() onTap;
}
```
3) Once your class passes it's tests, you will use it to create your widget. Use the <b>logic</b> getter to access your widget's logic.
```dart
class SimpleButton extends HumbleStatelessWidget<SimpleButtonLogic> {
SimpleButton(SimpleButtonLogic logic) : super(logic, key: Key('SimpleButton'));
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: Text(logic.text),
onPressed: () => logic.onTap(),
);
}
}
```
That's it! You now have a testable (albeit simple) widget! Take a look at the <b>example</b> folder for creating a <b>HumbleStatefulWidget</b>.

## Additional information

The amount of source code in the library is minuscule, and it doesn't constitute anything new or bleeding-edge. However, use of this library will help the average developer apply high level and highly-esteemed software engineering principles to their code.

Learn more about the Humble Object Pattern here: https://martinfowler.com/bliki/HumbleObject.html.
4 changes: 4 additions & 0 deletions analysis_options.yaml
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
52 changes: 52 additions & 0 deletions example/blinker.dart
@@ -0,0 +1,52 @@
import 'dart:async';
import 'package:flutter/material.dart';

// Good luck testing logic used by the widget...

class Blinker extends StatefulWidget {
const Blinker(
{required this.text,
required this.primary,
required this.secondary,
required this.period,
required this.onPress})
: super(key: const Key('Blinker'));

final String text;
final Color primary;
final Color secondary;
final Duration period;
final Function() onPress;

@override
State<StatefulWidget> createState() => _BlinkerState();
}

class _BlinkerState extends State<Blinker> {
bool _blink = false;

@override
void initState() {
Timer.periodic(widget.period, (timer) {
if (mounted) {
setState(() => _blink = !_blink);
} else {
timer.cancel();
}
});
super.initState();
}

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
widget.onPress();
},
style: ElevatedButton.styleFrom(
primary: _blink ? widget.primary : widget.secondary,
),
child: Text(widget.text),
);
}
}
72 changes: 72 additions & 0 deletions example/humble_blinker.dart
@@ -0,0 +1,72 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_hop/flutter_hop.dart';

bool testBlinkerLogic() {
bool pressed = false;
final logic = BlinkerLogic(
text: 'some-text',
primary: Colors.red,
secondary: Colors.grey,
period: const Duration(seconds: 1),
onPress: () {
pressed = true;
});
if (logic.text != 'some-text') return false;
if (logic.primary != Colors.red) return false;
if (logic.secondary != Colors.grey) return false;
if (logic.period != const Duration(seconds: 1)) return false;
if (pressed == false) return false;
return true;
}

class BlinkerLogic {
BlinkerLogic(
{required this.text,
required this.primary,
required this.secondary,
required this.period,
required this.onPress});

final String text;
final Color primary;
final Color secondary;
final Duration period;
final Function() onPress;
}

class Blinker extends HumbleStatefulWidget<BlinkerLogic> {
const Blinker(BlinkerLogic logic) : super(logic, const Key('Blinker'));

@override
State<StatefulWidget> createState() => _BlinkerState();
}

class _BlinkerState extends HumbleState<BlinkerLogic> {
bool _blink = false;

@override
void initState() {
Timer.periodic(logic.period, (timer) {
if (mounted) {
setState(() => _blink = !_blink);
} else {
timer.cancel();
}
});
super.initState();
}

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
logic.onPress();
},
style: ElevatedButton.styleFrom(
primary: _blink ? logic.primary : logic.secondary,
),
child: Text(logic.text),
);
}
}
3 changes: 3 additions & 0 deletions lib/flutter_hop.dart
@@ -0,0 +1,3 @@
library flutter_hop;

export 'humble_widget.dart';
20 changes: 20 additions & 0 deletions lib/humble_widget.dart
@@ -0,0 +1,20 @@
import 'package:flutter/widgets.dart';

abstract class HumbleWidget<T> extends StatelessWidget {
const HumbleWidget(this.logic, [Key? key]) : super(key: key);

final T logic;
}

abstract class HumbleStatefulWidget<T> extends StatefulWidget {
const HumbleStatefulWidget(this.logic, [Key? key]) : super(key: key);
final T logic;
}

abstract class HumbleState<T> extends State<HumbleStatefulWidget<T>> {
T get logic => widget.logic;
}

abstract class HumbleTester {
bool test();
}

0 comments on commit c25b956

Please sign in to comment.