dart - 如何在 flutter 中制作全屏对话框?

标签 dart flutter

我想制作一个全屏对话框。对话框背景必须是不透明的。 这是一个例子: enter image description here

如何在 Flutter 中做出这样的事情?

最佳答案

您可以使用 Navigator 推送半透明 ModalRoute :

import 'package:flutter/material.dart';

class TutorialOverlay extends ModalRoute<void> {
  @override
  Duration get transitionDuration => Duration(milliseconds: 500);

  @override
  bool get opaque => false;

  @override
  bool get barrierDismissible => false;

  @override
  Color get barrierColor => Colors.black.withOpacity(0.5);

  @override
  String get barrierLabel => null;

  @override
  bool get maintainState => true;

  @override
  Widget buildPage(
      BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      ) {
    // This makes sure that text and other content follows the material style
    return Material(
      type: MaterialType.transparency,
      // make sure that the overlay content is not cut off
      child: SafeArea(
        child: _buildOverlayContent(context),
      ),
    );
  }

  Widget _buildOverlayContent(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            'This is a nice overlay',
            style: TextStyle(color: Colors.white, fontSize: 30.0),
          ),
          RaisedButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Dismiss'),
          )
        ],
      ),
    );
  }

  @override
  Widget buildTransitions(
      BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    // You can add your own animations for the overlay content
    return FadeTransition(
      opacity: animation,
      child: ScaleTransition(
        scale: animation,
        child: child,
      ),
    );
  }
}


// Example application:
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Playground',
      home: TestPage(),
    );
  }
}

class TestPage extends StatelessWidget {
  void _showOverlay(BuildContext context) {
    Navigator.of(context).push(TutorialOverlay());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Test')),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Center(
          child: RaisedButton(
            onPressed: () => _showOverlay(context),
            child: Text('Show Overlay'),
          ),
        ),
      ),
    );
  }
}

关于dart - 如何在 flutter 中制作全屏对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51908187/

相关文章:

firebase - Flutter需要更多时间从Firestore加载视频

dart - Flutter:右溢出 200 像素

android-studio - 在构建过程中调用setState()或markNeedsBuild()

flutter - 在 ABI 的 NDK 工具链文件夹中找不到工具链,前缀为 : arm-linux-androideabi

dart - ListView 中的行未显示

flutter - 在 dispose 方法中获取提供者方法不起作用

templates - Dart错误: '_Template'类没有带有匹配参数的实例方法 'render'

在应用栏上更新时,Flutter 脚手架更新了整个页面

dart - 如何测试 google dart 进行网络开发?

charts - 有没有办法在 TimeSeries Flutter 图表上设置笔划宽度?