build_context 2.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

Logo

BuildContext #

Access most used properties in your BuildContext instance. This package relies on Dart's extension to provide easy access for the most used properties and functions that depends on the BuildContext instance.

Available Extensions #

I update this package frequently to add more extensions, bellow you can see the currently available extensions in the latest version.

From the MediaQuery. Access properties right in the context instance. Available extensions:

  • context.mediaQuerySize
  • context.orientation
  • context.mediaQueryPadding
  • context.alwaysUse24HourFormat
  • context.devicePixelRatio
  • context.platformBrightness
  • context.textScaleFactor
  • context.isLandscape
  • context.isPortrait
  • context.mediaQueryViewPadding
  • context.mediaQueryViewInsets
  • context.mediaQueryShortestSide
  • context.isPhone
  • context.isTablet
  • context.isSmallTablet
  • context.isLargeTablet

From the Navigator class. Navigate with ease. Available extensions:

  • context.push()
  • context.pop()
  • context.canPop()
  • context.pushNamed()
  • context.popUntil()

From the Theme class. Access your themes right in the context instance. Available extensions:

  • context.textTheme
  • context.primaryTextTheme
  • context.accentTextTheme
  • context.bottomAppBarTheme
  • context.bottomSheetTheme
  • context.appBarTheme
  • context.backgroundColor
  • context.primaryColor
  • context.buttonColor
  • context.scaffoldBackgroundColor

From Scaffold class. Handle your scaffold in their context.

Note: those must be called in the context of a Scaffold widget otherwise you might have errors.

  • context.openDrawer()
  • context.openEndDrawer()
  • context.showSnackBar()
  • context.hideCurrentSnackBar()
  • context.removeCurrentSnackBar()
  • context.showBottomSheet()

From Form.of(context) class.

Note: those must be called in the context of a Scaffold widget otherwise you might have errors.

  • context.form.validate()
  • context.form.reset()
  • context.form.save()

From FocusScope.of(context) class.

  • context.focusScope.hasFocus

  • context.focusScope.isFirstFocus

  • context.focusScope.canRequestFocus

  • context.focusScope.hasPrimaryFocus

  • context.focusScope.unfocus()

  • context.focusScope.nextFocus()

  • context.focusScope.requestFocus()

  • context.focusScope.previousFocus()

  • context.focusScope.setFirstFocus()

  • context.focusScope.consumeKeyboardToken()

Install #

Add it in your pubspec.yaml:

dependencies:
  build_context: ^2.0.1

Import it where you want to use it e.g, in your widget's file.

import "package:build_context/build_context.dart";

Here is a usage example:

import 'package:flutter/material.dart';
import "package:build_context/build_context.dart";

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: context.scaffoldBackgroundColor, // There is no Theme.of(context)
      body: Center(
        child: GestureDetector(
          onTap: () => context.pushNamed('/detailsPage'), // we use only context not Navigator.of(context)
          child: Text(
            'Press Me',
            style: context.primaryTextTheme.title, // we use only context not Theme.of(context)
          ),
        ),
      ),
    );
  }
}

Support #

You liked this package? then give it a star. If you want to help then:

  • Start this repository
  • Send a Pull Request with new features
  • Share this package
  • Create issues if you find a Bug or want to suggest something

[2.0.1] - 23/04/2020. #

  • context.pop() param value is now optional

[2.0.0] - 23/04/2020. #

This is a breaking change for users of the bellow functions:

  • context.pop() now returns void as the default function (Flutter 1.17.0)
  • context.unfocus() now receive [UnfocusDisposition] as optional argument (Flutter 1.17.0)

[1.1.1] - 23/04/2020. #

Reasons to removeCurrentSnackBar and hideCurrentSnackBar are now optional as in the default implementation

[1.1.0] - 23/04/2020. #

  • Added support for:

  • context.focusScope.hasFocus

  • context.focusScope.isFirstFocus

  • context.focusScope.canRequestFocus

  • context.focusScope.hasPrimaryFocus

  • context.focusScope.unfocus()
  • context.focusScope.nextFocus()
  • context.focusScope.requestFocus()
  • context.focusScope.previousFocus()
  • context.focusScope.setFirstFocus()
  • context.focusScope.consumeKeyboardToken()

[1.0.0] - 21/04/2020. #

Added support for:

  • context.mediaQueryShortestSide
  • context.isPhone
  • context.isTablet
  • context.isSmallTablet
  • context.isLargeTablet

[0.0.5] - 07/04/2020. #

  • Code format

[0.0.4] - 04/04/2020. #

Added support for:

  • context.form.validate()
  • context.form.reset()
  • context.form.save()

[0.0.3] - 03/04/2020. #

Added support for:

  • context.isLandscape
  • context.isPortrait
  • context.mediaQueryViewInsets
  • context.mediaQueryViewPadding

[0.0.2] - 03/04/2020. #

  • Fixed README.md format issue

[0.0.1] - 02/04/2020. #

Added support for the following classes:

  • MediaQuery
  • Theme
  • Navigator
  • Scaffold

example/lib/main.dart

import 'package:example/pages/focus_scope.dart';
import 'package:example/pages/form.dart';
import 'package:flutter/material.dart';

import 'package:build_context/build_context.dart';

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

class Routes {
  static const focusScope = '/focusScopePage';
  static const form = '/formPage';
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        scaffoldBackgroundColor: Colors.white24,
        primaryTextTheme: TextTheme(
          title: TextStyle(color: Colors.blue),
        ),
      ),
      home: HomePage(),
      routes: {
        Routes.focusScope: (_) => FocusScopePage(),
        Routes.form: (_) => FormPage(),
      },
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: context.scaffoldBackgroundColor,
      appBar: AppBar(),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              MaterialButton(
                onPressed: () => context.pushNamed(Routes.form),
                child: Text(
                  'Form Example',
                  style: context.primaryTextTheme.title,
                ),
              ),
              MaterialButton(
                onPressed: () => context.pushNamed(Routes.focusScope),
                child: Text(
                  'Focus Scope Example',
                  style: context.primaryTextTheme.title,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


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

We analyzed this package on May 23, 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