textfield_state 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 94

textfield_state #

pub

A helper class for maintaining a TextEditingController and/or a FocusNode for a State and its Widgets.

example

[0.2.1] - 2020-06-11 #

  • Fix controller not created if textChanged not defined.
  • add .text getter.

[0.2.0+2] - 2020-04-24 #

  • Add text-only support and example
  • Add screenshot and link to pub

[0.1.0] - 2020-04-24 #

  • initial release.

example/lib/main.dart

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

/// Demo 1 - state only
class Demo1 extends StatefulWidget {
  @override
  _Demo1State createState() => _Demo1State();
}

class _Demo1State extends State<Demo1> {
  TextFieldState state;

  @override
  void initState() {
    state = TextFieldState(
      textChanged: _rebuild,
      focusChanged: _rebuild,
      primaryFocusChanged: _rebuild,
      text: 'Demo 1 - state only',
    );
    super.initState();
  }

  void _rebuild(_) => setState(() {});

  @override
  void dispose() {
    state.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => DemoTextField(state);
}

/// Demo 2 - widget defined
class Demo2 extends StatefulWidget {
  const Demo2(this.controller, this.focusNode);

  final TextEditingController controller;
  final FocusNode focusNode;

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

class _Demo2State extends State<Demo2> {
  TextFieldState state;

  @override
  void initState() {
    state = TextFieldState(
      textChanged: _rebuild,
      focusChanged: _rebuild,
      primaryFocusChanged: _rebuild,
      controller: widget.controller,
      focusNode: widget.focusNode,
    );
    super.initState();
  }

  void _rebuild(_) => setState(() {});

  @override
  void didUpdateWidget(Demo2 oldWidget) {
    state.update(controller: widget.controller, focusNode: widget.focusNode);
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    state.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => DemoTextField(state);
}

/// Demo 3 - text only
class Demo3 extends StatefulWidget {
  const Demo3(this.text);
  final String text;
  @override
  _Demo3State createState() => _Demo3State();
}

class _Demo3State extends State<Demo3> {
  TextFieldState state;

  @override
  void initState() {
    state = TextFieldState(
      textChanged: _rebuild,
      focusChanged: _rebuild,
      primaryFocusChanged: _rebuild,
      text: widget.text,
    );
    super.initState();
  }

  void _rebuild(_) => setState(() {});

  @override
  void didUpdateWidget(Demo3 oldWidget) {
    state.update(text: widget.text);
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    state.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => DemoTextField(state);
}

/// Helper for dumping info, shows connection to TextField.
class DemoTextField extends StatelessWidget {
  const DemoTextField(this.state);
  final TextFieldState state;
  @override
  Widget build(BuildContext context) => TextField(
        controller: state.controller,
        focusNode: state.focusNode,
        decoration: InputDecoration(
          helperText: 'focused: ${state.focusNode.hasFocus}, '
              'primary: ${state.focusNode.hasPrimaryFocus}\n'
              'text: ${state.controller.text}\n'
              'controller instance: ${state.controller.hashCode}\n'
              'focusNode instance: ${state.focusNode.hashCode}',
          helperMaxLines: 5,
        ),
      );
}

// ---

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  /// There's a Flutter issue when creating a FocusNode on every build.
  /// See https://stackoverflow.com/a/57586327/884522.
  final focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'TextFieldState Demo',
        theme: ThemeData(
          inputDecorationTheme:
              InputDecorationTheme(border: OutlineInputBorder()),
        ),
        home: Scaffold(
          appBar: AppBar(title: Text('TextFieldState Demo')),
          body: SingleChildScrollView(
            child: Container(
              padding: EdgeInsets.all(32),
              child: Column(
                children: <Widget>[
                  Demo1(),
                  SizedBox(height: 24),
                  Demo2(TextEditingController(text: 'Demo 2 - widget defined'), focusNode),
                  SizedBox(height: 24),
                  Demo3('Demo 3 - text only'),
                  SizedBox(height: 24),
                  RaisedButton(
                    child: Text('Rebuild'),
                    onPressed: () => setState(() {}),
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  textfield_state: ^0.2.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:textfield_state/textfield_state.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
87
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]
94
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • textfield_state that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test