搜索插件、代码仓与 APP 分类

插件详情v3.0.0

build_context

一个简化在 BuildContext 中访问最常用属性/函数的包。

近 30 天下载
169
喜欢数
145
Pub 分数
60

安装插件

flutter pub add build_context

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

build_context 包通过向 BuildContext 类添加常用属性和函数来增强 Flutter 开发,减少样板代码。它提供了对主题、媒体查询、导航和其他重要工具的直接访问,无需进行显式的上下文查找。这简化了组件创建并提高了代码可读性。该包轻量且设计用于在各种 Flutter 项目中轻松使用。

适合场景

  • 简化小部件中的主题访问
  • 快速导航与上下文
  • 访问媒体查询数据
  • 减少重复的上下文查找
  • 提高控件可读性

主要特点

  • 将 BuildContext 扩展为包含有用方法
  • 减少样板代码
  • 提供主题和媒体查询快捷方式
  • 包含导航辅助功能
  • 轻量且易于集成

适合对象

  • 寻求更简洁代码的开发者
  • 频繁使用上下文的项目
  • 注重代码可维护性的团队
  • Flutter 初学者学习上下文模式
  • 具有复杂UI布局的应用程序

使用建议

  • 可能会略微增加包大小
  • 仅限基本实用工具扩展
  • 不适合高级自定义上下文逻辑
  • 需要理解 BuildContext 的基本概念
  • 最好与适当的状态管理配合使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Languages: English | Brazilian Portuguse

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.theme
  • context.textTheme
  • context.primaryTextTheme
  • context.accentTextTheme
  • context.bottomAppBarTheme
  • context.bottomSheetTheme
  • context.appBarTheme
  • context.backgroundColor
  • context.primaryColor
  • context.buttonColor
  • context.scaffoldBackgroundColor
  • context.platform
  • context.isAndroid
  • context.isIOS
  • context.isWindows
  • context.isMacOS
  • context.isLinux
  • context.isFuchsia
  • context.headline1
  • context.headline2
  • context.headline3
  • context.headline4
  • context.headline5
  • context.headline6
  • context.subtitle1
  • context.bodyText1
  • context.bodyText2
  • context.caption
  • context.button
  • context.subtitle2
  • context.overline
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()

  • context.closeKeyboard()

From ModalRoute.of(context) class.
  • context.modalRoute
  • context.routeSettings

Install

Add it in your pubspec.yaml:

dependencies:
  build_context: ^3.0.0

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

所属分类

源码仓库

使用此插件的项目