prefs_config 0.8.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 79

prefs_config #

This dart package works alongside the shared preferences plugin and can be used to automatically generate a settings or preferences screen for a Flutter app.

Usage #

This package contains three principle objects for use.

Pref for defining preferences for display. Prefs which can be used to hold a collection of these preferences and manage them. PreferenceContainer which is a StatefulWidget that will contruct a screen to so a user may view and edit them.

Pref Class #

This object allows the description of a preference, both in terms of data and also behaviour in the preference screen, these are as follows:

  • prefKey. String. This is the key that identifies the preference. The same key can be used to get or set, either through the Prefs object or the shared preferences plugin, any preference, other than the special preference types.
  • type. Int. The type of preference. Note that these are principally display, rather than data types, the first six of which handle bool, String, int, and DateTime data types and the last two special types are not true preferences, but UI elements. These are:
    • Bool. Bool preference type. The format property may be used so it can be displayed as either a switch or checkbox input. Default is as a switch input.
    • Text. String preference type. The format property may be used so it can input as plain text, URI, phone number or an email. Default is as a plain text.
    • Int. Int preference type. The format property may be used so it can input as text input or using a slider (max and min properties are required for the latter). Default is as a text input.
    • Date. DateTime preference type. The format property may be used so it can displayed in dd/mm/yyyy, mm/dd/yyyy, yyyy/mm/dd format. Default is dd/mm/yyyy.
    • Time. DateTime preference type.
    • List. Int preference type. The format property may be used so it can be displayed as either a dropdown or as a seperate_dialog_ input. Default is as a dropdown input.
    • Header. Special Preference Type. A list header sub-label. The defVal property may be used to assign a background color
    • Function. Special Preference Type. A list item that triggers a call to a function.
  • defVal. Dynamic. The default value of the preference, when first created.
  • visible. Bool. Denotes if a preference will appear on the generated preference screen or not. True by default.
  • enabled. Bool. Denotes if a preference will be enabled on the generated preference screen or not. True by default.
  • dependancy. String. A prefernece may have its enabled property overridden by this property. If the value is set to the key of a Bool type preference, to the current value of that preference. Note, that this does not change the value of the preference, simply whether it is enabled on the generated preference screen.
  • function. Function. The function to call when a Function type preference is clicked in the list.
  • listItems. Map<int,String>. Only used with List type preferences. The Map object what the option value and option label values are. The ultimate value of the List type preference will be the integer.
  • label. String. The title shown on the preference tile on the generated preference screen, it also serves as the text to any Header type preference.
  • description. String. A short description that is shown below the preference tile title on the generated preference screen.
  • format. Int. Some preference types may be formatted to take in particular input. Please refer above to the type property for more information.
  • max. Int. The maximum size allowed for Int type preferences, day in the future for Date type preferences and length allowed for Text type preferences.
  • min. Int. The minimum size allowed for Int type preferences, day in the past for Date type preferences and length allowed for Text type preferences.

Prefs Class #

To initiate this class a list of pref items, in order in which they will be displayed, must be submitted to the constructor.

Prefs p = Prefs (
  preferences: <Pref> [
    Pref(
      prefKey: "txt_plain",
      type: Pref.TYPE_TEXT,
      defVal: "foobar",
      min: 1, // Minimum length of text preference - 1 means cannot be empty.
      label: "Text Pref",
      description: "This is a Text Setting"
    ),
    Pref(
      prefKey: "bol_switch",
      type: Pref.TYPE_BOOL,
      defVal: true,
      label: "Bool Switch Pref",
      format: Pref.FORMAT_BOOL_SWITCH,
      description: "This is a Bool Switch Setting"
    ),
  ]
);

The class contains getters and setters for the principle preference data types - String, int, bool and DateTime. The are interchangeable with the corresponding shared preferences methods (and in fact use them), but also manage the corresponding Pref values and thus the generated preferences screen display.

Additionally, the class comes with a number of helper methods to manage the Pref items and finally a method to launch the generated preferences screen.

p.buildPreferencesScreen(context, 'My Preferences Screen');

PreferenceContainer Class #

This class is a StatefulWidget that invokes the preferences screen. It may be called via the buildPreferencesScreen method of the Prefs class, as shown above, or directly with the following constructor:

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => PreferenceContainer(
      preferences: this.preferences,
      title: title
    )
  )
);

[0.8.1] - 21.09.2019

  • Added dropdown format for list preferences.
  • Preference item layout made more flexible so as to accommodate long descriptions.
  • Added constructor constraints.
  • Code clean up and updated to conform with new updated Dart.

[0.8.0] - 16.03.2019

  • Initial release.

example/README.md

Example #

The following is an example of how prefs_config may be used.

import 'package:flutter/material.dart';
import 'package:prefs_config/prefs_config.dart';

void main() => runApp(new PrefsExample());

class PrefsExample extends StatefulWidget {
  @override
  _PrefsExampleState createState() => _PrefsExampleState();
}

class _PrefsExampleState extends State<PrefsExample> {
  Prefs p;
  String prefScreenTitle;

  @override
  void initState() {
    // Title used by the generated preference screen.
    this.prefScreenTitle = "Example Settings";

    // List of options used in the example list preference
    Map<int, String> myList = Map();
    for (int i = 0; i < 20; i++)
      myList[i] = 'Option ' + (i + 1).toString();

    /// The Pref Object uses the following properties:
    ///
    /// prefKey: Unique identifier key used to identify the preference.
    /// type: Integer-based category denoting preference type.
    /// defVal: Dynamic property denoting the default value of the preference.
    /// label: The title of the preference used in the prefs screen.
    /// description: Description text for the preference used in the prefs screen.
    /// visible: Whether a preference is editable via the prefs screen.
    /// enabled: Whether a preference is enabled (does not affect headers).
    /// format: An optional value that affects the behaviour/appearance of the preference when edited.
    ///
    /// Additional notes to properties are below.
    List<Pref> prefs = [
      Pref(
          prefKey: "txt_plain",
          type: Pref.TYPE_TEXT,
          defVal: "foobar",
          min: 1, // Minimum length of text preference - 1 means cannot be empty.
          max: 10, // Maximum length of text preference.
          label: "Text Pref",
          description: "This is a Text Setting"
      ),
      Pref(
          prefKey: "txt_hidden",
          type: Pref.TYPE_TEXT,
          defVal: "foobar",
          visible: false
      ),
      Pref(
          prefKey: "bol_switch",
          type: Pref.TYPE_BOOL,
          defVal: false,
          label: "Bool Switch Pref",
          format: Pref.FORMAT_BOOL_SWITCH,
          description: "This is a Bool Switch Setting"
      ),
      Pref(
          prefKey: "bol_switch_dis",
          type: Pref.TYPE_BOOL,
          defVal: false,
          label: "Bool Switch Pref (disabled)",
          enabled: false,
          format: Pref.FORMAT_BOOL_SWITCH,
          description: "This is a Bool Switch Setting"
      ),
      Pref(
          prefKey: "lst_options1",
          type: Pref.TYPE_LIST,
          defVal: 6,
          label: "List Pref",
          format: Pref.FORMAT_LIST_DIALOG,
          listItems: myList,
          description: "This is a Dialog List Setting"
      ),
      Pref(
          prefKey: "lst_options2",
          type: Pref.TYPE_LIST,
          defVal: 6,
          label: "List Pref",
          listItems: myList,
          description: "This is a Dropdown List Setting"
      ),
      Pref(
          prefKey: "bol_check",
          type: Pref.TYPE_BOOL,
          defVal: false,
          label: "Bool CheckboxPref",
          format: Pref.FORMAT_BOOL_CHECKBOX,
          description: "This is a Bool Checkbox Setting"
      ),
      Pref(
          prefKey: "txt_number",
          type: Pref.TYPE_INT,
          defVal: 45,
          label: "Number Pref",
          description: "This is a Number Textbox Setting"
      ),
      Pref(
          prefKey: "sdr_number",
          type: Pref.TYPE_INT,
          defVal: 45,
          min: 0, // Minimum value of integer on slider
          max: 100, // Maximum value of integer on slider
          format: Pref.FORMAT_INT_SLIDER,
          label: "Number Pref (Slider)",
          description: "This is a Number Slider Setting"
      ),
      Pref(
          prefKey: "dt_day",
          type: Pref.TYPE_DATE,
          defVal: 20180124,
          dependancy: 'bol_switch',
          label: "Date Pref",
          format: Pref.FORMAT_DATE_YMD,
          description: "This is a Date Setting"
      ),
      Pref(
          prefKey: "dt_time",
          type: Pref.TYPE_TIME,
          defVal: "20180124T132700",
          label: "Time Pref",
          description: "This is a Time Setting"
      ),
      Pref(
        prefKey: "hdr",
        type: Pref.TYPE_HEADER,
        label: "Setting Header",
        defVal: Colors.white30, // Background color of header
      ),
      Pref(
          prefKey: "func_test",
          type: Pref.TYPE_FUNCTION,
          function: testFunction, // Name of function that preference item will call when clicked
          label: "Function Pref",
          description: "This is a Function Setting"
      ),
      Pref(
          prefKey: "txt_email",
          type: Pref.TYPE_TEXT,
          defVal: "test@test.com",
          format: Pref.FORMAT_TEXT_EMAIL,
          label: "Email Pref",
          description: "This is an Email String Setting"
      ),
      Pref(
          prefKey: "txt_phone",
          type: Pref.TYPE_TEXT,
          defVal: "+41798521111",
          format: Pref.FORMAT_TEXT_PHONE,
          label: "Phone Pref",
          description: "This is an Phone String Setting"
      ),
      Pref(
          prefKey: "txt_uri",
          type: Pref.TYPE_TEXT,
          defVal: "http://www.google.com",
          format: Pref.FORMAT_TEXT_URI,
          label: "URL Pref",
          description: "This is an URL Setting"
      ),
    ];

    this.p = Prefs(preferences: prefs);

    super.initState();
  }

  // This is the function called by func_test above
  void testFunction() {
    showAboutDialog(context: context);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
          child: RaisedButton(
            onPressed: () => this.p.buildPreferencesScreen(context, this.prefScreenTitle),
            child: Text("Launch"),
          )
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  prefs_config: ^0.8.1

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:prefs_config/prefs_config.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
62
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Nov 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/generated/i18n.dart.

Run flutter format to format lib/generated/i18n.dart.

Format lib/prefitems/pref_date.dart.

Run flutter format to format lib/prefitems/pref_date.dart.

Format lib/prefitems/pref_int.dart.

Run flutter format to format lib/prefitems/pref_int.dart.

Format lib/prefs_config.dart.

Run flutter format to format lib/prefs_config.dart.

Format lib/shared/preflistedit.dart.

Run flutter format to format lib/shared/preflistedit.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (shared_preferences).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
shared_preferences ^0.4.3 0.4.3 0.5.4+5
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test