dialog_context 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

Pub Version PRs Welcome

Show Dialog, BottomSheet, ModalBottomSheet and SnackBar with no need BuildContext. #

To be discontinued, use the OneContext package: one_context

https://pub.dev/packages/one_context #

// example snackBar
DialogContext().showSnackBar(
    snackBar: SnackBar(content: Text('My awesome snackBar!'))
);
// example dialog
DialogContext().showDialog(
    builder: (context) => AlertDialog( 
        title: new Text("The Title"),
        content: new Text("The Body"),
    )
);
// example bottomSheet
DialogContext().showBottomSheet(
    builder: (context) => Container(
    alignment: Alignment.topCenter,
    height: 200,
    child: IconButton(
        icon: Icon(Icons.arrow_drop_down),
        iconSize: 50,
        color: Colors.white,
        onPressed: () => Navigator.pop(context)),
    ),
);

Important: Use the DialogContext().builder in MaterialApp #

MaterialApp(
  builder: DialogContext().builder, /// important: Use [DialogContext().builder] builder here!
  home: MyHomePage(title: 'Dialogs without BuildContext'),
);

Add dependency

  dependencies:
    dialog_context: ^0.1.1

[0.1.2] - Update readme #

[0.1.1] - Refactors #

[0.1.0] - Initial release #

  • add showDialog
  • add showModalBottomSheet
  • add showBottomSheet
  • add showSnackBar
  • add dismissSnackBar

example/lib/main.dart

import 'package:dialog_context/dialog_context.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: DialogContext().builder,

      /// important: Use [DialogContext().builder] builder
      title: 'Dialogs without BuildContext',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Dialogs without BuildContext'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Show SnackBar'),
              onPressed: () {
                DialogContext().dismissSnackBar(); // Dismiss snackbar
                DialogContext().showSnackBar(
                    snackBar: SnackBar(
                  content: Text(
                    'My awesome snackBar!',
                    textAlign: TextAlign.center,
                    style: Theme.of(context)
                        .textTheme
                        .title
                        .copyWith(color: Colors.white),
                  ),
                  action: SnackBarAction(label: 'DISMISS', onPressed: () {}),
                ));
              },
            ),
            RaisedButton(
              child: Text('Show Dialog'),
              onPressed: () async {
                var result = await DialogContext().showDialog<String>(
                    // Don't need context to show alertDialog
                    builder: (context) => AlertDialog(
                          title: new Text("The Title"),
                          content: new Text("The Body"),
                          actions: <Widget>[
                            new FlatButton(
                                child: new Text("OK"),
                                onPressed: () =>
                                    Navigator.of(context).pop('ok')),
                            new FlatButton(
                                child: new Text("CANCEL"),
                                onPressed: () =>
                                    Navigator.of(context).pop('cancel')),
                          ],
                        ));
                print(result);
              },
            ),
            RaisedButton(
              child: Text('Show modalBottomSheet'),
              onPressed: () async {
                var result = await DialogContext().showModalBottomSheet<String>(
                    builder: (context) => Container(
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              ListTile(
                                  leading: Icon(Icons.music_note),
                                  title: Text('Music'),
                                  onTap: () =>
                                      Navigator.of(context).pop('Music')),
                              ListTile(
                                leading: Icon(Icons.videocam),
                                title: Text('Video'),
                                onTap: () => Navigator.of(context).pop('Video'),
                              ),
                              SizedBox(height: 45)
                            ],
                          ),
                        ));
                print(result);
              },
            ),
            RaisedButton(
              child: Text('Show bottomSheet'),
              onPressed: () {
                DialogContext().showBottomSheet(
                  builder: (context) => Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                      color: Colors.red,
                    ),
                    margin: EdgeInsets.all(16),
                    alignment: Alignment.topCenter,
                    height: 200,
                    child: IconButton(
                        icon: Icon(Icons.arrow_drop_down),
                        iconSize: 50,
                        color: Colors.white,
                        onPressed: () => Navigator.pop(context)),
                  ),
                );
              },
            ),
            RaisedButton(
                child: Text('Show and await input'),
                onPressed: () async {
                  switch (await DialogContext().showDialog<int>(
                      barrierDismissible: false,
                      builder: (BuildContext context) {
                        return SimpleDialog(
                          shape: RoundedRectangleBorder(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          title: const Text('Select assignment'),
                          children: <Widget>[
                            SimpleDialogOption(
                              onPressed: () {
                                Navigator.pop(context, 1);
                              },
                              child: const Text('Number 1'),
                            ),
                            SimpleDialogOption(
                              onPressed: () {
                                Navigator.pop(context, 2);
                              },
                              child: const Text('Number 2'),
                            ),
                          ],
                        );
                      })) {
                    case 1:
                      print('number one');
                      break;
                    case 2:
                      print('number two');
                      break;
                  }
                }),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  dialog_context: ^0.1.2

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

We analyzed this package on Jul 8, 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:

  • dialog_context that is a package requiring null.

Dependencies

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