dialog_manager 1.0.6

Flutter Android iOS web

a Flutter dialog manager

example/lib/main.dart

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

import './test.dart';

void main () => runApp(DialogManageProvider(child: App()));


class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'dialogManager',
      home: Home(),
      routes: {
        'home': (_) => Home(),
        'test': (_) => Test()
      },
    );
  }
}

class HomeContent extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomeContentState();
  }
}

class HomeContentState extends State<HomeContent> with DialogStateMixin, DialogMixin {
  @override
  initState () {
    super.initState();
//    Future(() {
//      DialogManageProvider.of(context).addListener(this, context);
//    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  onDialogShow () {
    print('Home 子组件 当前页面的弹窗显示');
  }

  @override
  onDialogHide () {
    print('Home 子组件 当前页面的弹窗隐藏');
  }

  @override
  void onDialogHandleComplete() {
    print('当前页面弹窗处理完毕');
  }

  @override
  Widget build(BuildContext context) {
    return Text('home 子组件', style: TextStyle(decoration: TextDecoration.none, fontSize: 20));
  }
}

class Home extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}

class HomeState extends State<Home> with DialogStateMixin {

  bool isShowingDialog;

  @override
  initState () {
    super.initState();
    isShowingDialog = false;
    Future(() {
      DialogManageProvider.of(context).addListener(this, context);
    });
  }

  @override
  dispose () {
    super.dispose();
    DialogManageProvider.of(context).removeListener(this, context);
  }

  @override
  onDialogShow () {
    print('Home当前页面的弹窗显示');
    setState(() {
      isShowingDialog = true;
    });
  }

  @override
  onDialogHide () {
    print('Home当前页面的弹窗隐藏');
    setState(() {
      isShowingDialog = false;
    });
  }

  Future<dynamic> showContent (String content, BuildContext context, { bool showNestBtn = false }) {
    return showDialog(
      context: context,
      builder: (BuildContext innerContext) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

            RaisedButton(
              onPressed: () {
                Navigator.of(innerContext).pop('弹窗关闭返回值');
              },
              child: Text('关闭弹窗', style: TextStyle(decoration: TextDecoration.none, fontSize: 20)),
            ),

            Container(
              width: 100,
              height: 100,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10),
                color: Colors.red
              ),
              child: Text(
                content,
                style: TextStyle(
                  fontSize: 20,
                  decoration: TextDecoration.none,
                  color: Colors.black
                )
              ),
            ),

            showNestBtn ? RaisedButton(
              onPressed: () {
                showOneDialog(context);
              },
              child: Text('再次第二层弹窗'),
            ) : SizedBox.shrink()
          ],
        );
      }
    );
  }

  void showOneDialog (BuildContext context) {
    DialogManageProvider.of(context).requestShowDialog(context, () {
      return showContent('弹窗1', context);
    }).then((result) {
      print('showOneDialog result ${result}');
    }).catchError((error) {
      print('弹窗失败,失败原因为: ${error}');
    });
  }

  void showNestDialog (BuildContext context) {
    DialogManageProvider.of(context).requestShowDialog(context, () {
      return showContent('弹窗1', context, showNestBtn: true);
    }).then((result) {
      print('showNestDialog result ${result}');
    }).catchError((error) {

      print('弹窗失败,失败原因为: ${error}');
    });
  }

  void showConsequentDialog (BuildContext context) {
    DialogManage dialogManage = DialogManageProvider.of(context);
    dialogManage.requestShowDialog(context, () {
      return showContent('弹窗2', context);
    }, priorityLevel: 0).then((result) {
      print('showConsequentDialog result2 ${result}');
    }).catchError((error) {
      print('弹窗失败,失败原因为: ${error}');
    });

    Future.delayed(Duration(milliseconds: 2000), () {
      dialogManage.requestShowDialog(context, () {
        return showContent('弹窗1', context);
      }, priorityLevel: 0).then((result) {
        print('showConsequentDialog result1 ${result}');
      }).catchError((error) {
        print('弹窗失败,失败原因为: ${error}');
      });
    });

    Future.delayed(Duration(milliseconds: 2000), () {
      dialogManage.requestShowDialog(context, () {
        return showContent('弹窗3', context);
      }, priorityLevel: 0).then((result) {
        print('showConsequentDialog result1 ${result}');
      }).catchError((error) {
        print('弹窗失败,失败原因为: ${error}');
      });
    });

//    dialogManage.showFlutterDialog(
//      context: context,
//      builder: (BuildContext context) {
//        return Text('showFlutterDialog');
//      },
//      priorityLevel: 0
//    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      alignment: Alignment.bottomCenter,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          HomeContent(),

          Text('当前页面是否有弹窗在显示: ${isShowingDialog}', style: TextStyle(fontSize: 25, color: Colors.white, decoration: TextDecoration.none),),

          RaisedButton(
            onPressed: () {
              showOneDialog(context);
            },
            child: Text('显示弹窗'),
          ),

          RaisedButton(
            onPressed: () {
              showConsequentDialog(context);
            },
            child: Text('连续显示两个弹窗', style: TextStyle(decoration: TextDecoration.none)),
          ),

          RaisedButton(
            onPressed: () {
              showNestDialog(context);
            },
            child: Text('嵌套显示弹窗', style: TextStyle(decoration: TextDecoration.none)),
          ),

          RaisedButton(
            onPressed: () {
              Navigator.of(context).pushNamed('test');
            },
            child: Text('跳转到Test页面', style: TextStyle(decoration: TextDecoration.none)),
          ),

          RaisedButton(
            onPressed: () {
              DialogManageProvider.of(context).showFlutterDialog(
                context: context,
                builder: (BuildContext context) {
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Container(
                        width: 100,
                        height: 100,
                        color: Colors.red
                      )
                    ],
                  );
                },
                priorityLevel: 1
              ).then((result) {
                print('${result}');
              });

              DialogManageProvider.of(context).showFlutterDialog(
                context: context,
                builder: (BuildContext context) {
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Container(
                        width: 100,
                        height: 100,
                        color: Colors.blue
                      )
                    ],
                  );
                },
                priorityLevel: 0
              ).then((result) {
                print('${result}');
              });
            },
            child: Text('显示flutter dialog弹窗', style: TextStyle(decoration: TextDecoration.none)),
          ),

          RaisedButton(
            onPressed: () {
              bool isShowing = DialogManageProvider.of(context).getIsShowingState(context);
              print('home页面的弹窗显示状态:${isShowing}');
            },
            child: Text('获取home页面的弹窗显示状态', style: TextStyle(decoration: TextDecoration.none)),
          )
        ],
      )
    );
  }
}
1
likes
75
pub points
51%
popularity

a Flutter dialog manager

Homepage

Documentation

API reference

Uploader

xiaojielade@gmail.com

License

BSD (LICENSE)

Dependencies

flutter

More

Packages that depend on dialog_manager