检测是否打开页面作为对话框

时间:2019-05-10 11:59:21

标签: flutter

我已经为Flutter应用程序创建了自己的应用程序栏。它位于所有页面扩展的“ MasterPage / DetailPage”中。可以通过常规方式浏览页面(小部件...),并且还可以将某些页面作为对话框打开。一个示例是“人员页面”,它可以显示客户的联系人以及可用资源等。

使用普通的flutter应用程序栏,当以全屏对话框形式打开时,在栏的左侧会显示一个“ x”,如果是作为普通页面打开,则会显示一个菜单或后退图标。

我希望我的MasterPage能够检测它是否以对话框形式打开,以便我可以根据情况呈现页面。

是否有可能实现,还是我需要通过bool来说明页面的导航方式?我认为最后一个选择是多余的,因为明确告知导航器以模态方式打开路线。

我似乎找不到任何文档,但是我敢肯定,自从标准的应用栏显示基于此的应用栏动作以来,有一种方法。

编辑:并不是那么复杂的代码,但希望它能证明为什么我希望我们可以检查上下文是否是对话框而不是传递布尔值。

abstract class MasterPage extends StatelessWidget {
    final bool asDialog;

    MasterPage({this.asDialog = false});

    @override
    Widget build(BuildContext context) {
       Widget content;

       /// What I hope is possible is to do something like so that the 
       /// render only render based on the truth, not what the invoker 
       /// means.
       bool isDialog = context.isDialog;

       bool landscape = MediaQuery...;

       ScreenSize size = ....;

       if(size == size.veryBig) {
           /// Build content for big screens, size is detected elsewhere.
           /// This will most likely bi triggered when the application 
           /// run on FDE etc.. 
           content = _buildContentForVeryBigScreens(context, landscape);
       } else if(size == size.big) {
           content = _buildContentForBigScreens(context, landscape);
       } else if(size == size.small) {
           content = _buildContentForSmallScreens(context, landscape);
       } else {
           content = _buildContentForNormalScreens(context, landscape);
       }

       return Column(
         children: <Widget>[
             SafeArea(child: CustomAppbar(asDialog: asDialog /* Would rather use isDialog field */)),
             /// Some render stuff based on screen size such as a 
             /// minimized menu etc. etc.
             content,
         ],
       );
    }

    Widget _buildContentForSmallScreens(BuildContext context, bool landscape);
    Widget _buildContentForNormalScreens(BuildContext context, bool landscape);
    Widget _buildContentForBigScreens(BuildContext context, bool landscape);
    Widget _buildContentForVeryBigScreens(BuildContext context, bool landscape);
}

class PersonListPage extends MasterPage {
   PersonListPage({bool asDialog}) : super(asDialog: asDialog);

   Widget _buildContentForNormalScreens(BuildContext context, bool landscape) {
      /// What the presentation should be like on a normal screen size
      return Container();
   }
}

class PersonPage extends MasterPage {
   /// WOOPS: forgot to add the asDialog...
   /// So even though this route is invoked as
   /// a dialog we will still get the presentation
   /// of a normal page...
   PersonListPage() : super();

   Widget _buildContentForNormalScreens(BuildContext context, bool landscape) {
      /// What the presentation should be like on a normal screen size
      return Container();
   }

   Widget _buildContentFor.....;
}

0 个答案:

没有答案