ifonly 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new53

IfOnly #

Flutter conditional (if-else / switch-case) helpers to make a more readable and simpler conditional statement code.

🏃‍♂️ Getting Started #

In your flutter project add the dependency:

dependencies:
  ...
  ifonly: ^0.1.3

🧞‍♂️ Usage #

Importing package

import 'package:ifonly/ifonly.dart';

🐋 Why using this? #

Have you ever seen a repetitive code like this?

bool isValid = (username != null && username.isNotEmpty && password != null && password.isNotEmpty && email != null && email.isNotEmpty && phoneNumber != null && phoneNumber.isNotEmpty);

or returning widget like this

return (isValid) ? RaisedButton(child: Text('Click Me!'), onPressed: () => print('Hi!');) : RaisedButton(child: Text('Please fill the empty field first!'), onPressed: null;

🧙‍♂️ What IfOnly do #

With this package, we are trying to make a commonly used code easier to read.

Like making the isValid to this:

bool isValid = [username, password, email, phoneNumber].isNotNullOrEmpties();

And returning widget to this:

return IfOnly(
    condition: isValid,
    validBuilder: (context) => RaisedButton(child: Text('Click Me!'), onPressed: () => print('Hi!');),
    invalidBuilder: (context) => RaisedButton(child: Text('Please fill the empty field first!'),
),

Or like this:

return IfCaseOnly<bool>(
    value: isValid,
    caseBuilder: {
        true: (BuildContext context) => RaisedButton(child: Text('Click Me!'), onPressed: () => print('Hi!');),
        false: (BuildContext context) => RaisedButton(child: Text('Please fill the empty field first!'),
    },
    defaultBuilder: (context) => Text("Expression's builder is undefined. please input it to caseBuilder."),
),

Or even like this:

return IfCaseOnly<IfCases>(
    value: IfCases(
        cases: [
            IfCaseItem(isValid.isTrue(), (context) => YourValidButton()),
            IfCaseItem(isValid.isFalse(), (context) => YourInvalidButton()),
        ],
    ),
    defaultBuilder: (context) => Text("Expression's builder is undefined. please input it to caseBuilder."),
),

👨‍🎨 Custom conditional function #

If you need a self defined function like when define isValid, Use method isCondition or ifCondition for single data, isConditions or ifConditions for list:

bool isValid = [username, password, email, phoneNumber].isConditions((item) => item.isNotNullOrEmpty());

Execute code after conditions are meet #

Change the is on function to if

Widget nextButton;
[username, password, email, phoneNumber].ifConditions(
    (item) => item.isNotNullOrEmpty(),
    onTrue: (context) => nextButton = RaisedButton(child: Text('Click Me!'), onPressed: () => print('Hi!');),
    onFalse: (context) => nextButton = RaisedButton(child: Text('Please fill the empty field first!'),
);

More detailed explanation #

We have inputting comment to all function code, Go check Repository for more.

🙏🏻 Contributions #

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue. If you fixed a bug or implemented a feature, please send a pull request.

[0.1.3] - 05/05/2020. #

  • Update readme description.

[0.1.2] - 05/05/2020. #

  • Update readme description.

[0.1.1] - 05/05/2020. #

  • Update pubspec.yaml description.

[0.1.0] - 05/05/2020. #

  • Initial Release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:ifonly_example/widget/invalid_button.dart';
import 'package:ifonly_example/widget/valid_button.dart';
import 'package:regexpattern/regexpattern.dart';
import 'package:ifonly/ifonly.dart';

part 'section/multiple_textfield_section.dart';
part 'section/single_textfield_section.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'IfOnly Demo',
      theme: ThemeData.dark().copyWith(
        primaryColor: Colors.blueGrey,
        primaryColorDark: Colors.white,
      ),
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Builder(
        builder: (BuildContext scContext) => SafeArea(
          child: SingleChildScrollView(
            child: Container(
              margin:
                  EdgeInsets.symmetric(horizontal: 20.0).copyWith(top: 20.0),
              child: Column(
                children: <Widget>[
                  SingleTextfieldSection(scaffoldContext: scContext),
                  Container(
                    child: Divider(),
                    margin: EdgeInsets.symmetric(vertical: 20.0),
                  ),
                  MultipleTextfieldSection(scaffoldContext: scContext),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ifonly: ^0.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ifonly/ifonly.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
5
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
53
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test