Giter Club home page Giter Club logo

flutter-focusable-control-builder's Introduction

focusable_control_builder

Using FocusableControlBuilder you can quickly create a new control that will work properly on all platforms and input devices.

Working "properly" entails support for:

  • tab key traversal
  • focus / rollover states
  • modified mouse cursor
  • support for keyboard shortcuts (Enter/Space by default)

Under the hood, this builder wraps the built in FocusableActionDetector and you can read there for more details about how it all works.

For more information on why you'd use these widgets, see this blog post: https://blog.gskinner.com/archives/2021/06/flutter-building-custom-components-with-focusableactiondetector.html

๐Ÿ”จ Installation

$ flutter pub add focusable_control_builder

โš™ Import

import 'package:focusable_control_builder/focusable_control_builder.dart';

๐Ÿ•น๏ธ Usage

To create a custom button just implement the FocusableControlBuilder.builder method and assign an onPressed handler:

class MyCustomButton extends StatelessWidget {
  const MyCustomButton(this.label, {Key? key, required this.onPressed}) : super(key: key);
  final String label;
  final VoidCallback onPressed;
  @override
  Widget build(BuildContext context) {
    return FocusableControlBuilder(
        onPressed: onPressed,
        builder: (_, FocusableControlState control) {
          // Decide which colors to use, based on .isFocused and .isHovered
          Color outlineColor = control.isFocused ? Colors.black : Colors.transparent;
          Color bgColor = control.isHovered ? Colors.blue.shade100 : Colors.grey.shade200;
          // Return custom button contents
          return Container(
            padding: const EdgeInsets.all(8),
            child: Text(label),
            decoration: BoxDecoration(
              color: bgColor,
              border: Border.all(color: outlineColor, width: 1),
            ),
          );
        });
  }
}

Custom Mouse Cursor

FocusableControlBuilder will assume you want a "hand" cursor for your control, which is typically the case. If you'd like a different cursor, just assign it:

return FocusableControlBuilder(
    cursor: SystemMouseCursors.resizeDown,
    ...
);

Request Focus On Press

FocusableControlBuilder will assume you want your control to request focus when it is pressed. To disable, just set this to false:

return FocusableControlBuilder(
    requestFocusOnPress: false,
    ...
);

Custom Shortcuts

FocusableControlBuilder will create a default activation action which handles [Submit] and [Enter] keys. To create custom keyboard shortcuts you can set the .shortcuts and .actions parameters. This is a bit beyond the scope of this doc, but if you'd like to learn how, checkout the FocusableActionDetector docs which contain an example.

๐Ÿž Bugs/Requests

If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

๐Ÿ“ƒ License

MIT License

flutter-focusable-control-builder's People

Contributors

domesticmouse avatar esdotdev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.