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

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

如何在 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'),
          ),
        ),
      ),
    );
  }
}

https://stackoverflow.com/questions/51908187/

相关文章:

Flutter - 如何将用户数据传递给所有 View

flutter - 如何更改文本字段 flutter 中的值?

dart - Flutter 切换到 Tab 重新加载小部件并运行 FutureBuilder

flutter - 弹出时强制 Flutter 导航器重新加载状态

dart - Dart VM 还在使用吗?

dart - 如何在另一个 flutter 应用程序中使用本地 flutter 包?

flutter - 如何将 BottomNavigationBar 与 Navigator 一起使用

android-studio - Dart 的 SDK 在/flutter 文件夹中的什么位置?

dart - 如何去除滚动发光?

dart - 使用不包含 MediaQuery 的上下文调用的 Flutter 错误 : Media