轻按障碍时小部件不会弹出

时间:2019-07-12 20:36:49

标签: flutter

我正在尝试创建一个浮动的Widget,其背后具有模式障碍。如果用户点击障碍,则应弹出窗口小部件。 我将barrierDismissable设置为true。 我还不得不看Flutter ModalBottomSheets和没有运气的对话框。

我几乎被困在这里,不知道我在做什么错。

import 'package:flutter/material.dart';

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

  @override
  // TODO: implement barrierColor
  Color get barrierColor => const Color(0x80000000);

  @override
  // TODO: implement barrierDismissible
  bool get barrierDismissible => true;

  @override
  // TODO: implement barrierLabel
  String get barrierLabel => null;

  @override
  Widget buildPage(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
  ) {
    return Material(
      type: MaterialType.transparency,
      child: SafeArea(
        child: _buildOverlayContent(context),
      ),
    );
  }

  Widget _buildOverlayContent(BuildContext context) {
    return GestureDetector(
      onTap: () => print('t'),
      child: Align(
        alignment: Alignment.center,
        child: Container(
          color: Colors.red,
          height: 200,
          width: 200,
        ),
      ),
    );
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return FadeTransition(
      opacity: animation,
      child: ScaleTransition(
        scale: animation,
        child: child,
      ),
    );
  }
}

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

1 个答案:

答案 0 :(得分:0)

您不能在Material方法中使用buildPage(),因为Material是不透明的,并且将在障碍上显示,因此您将无法对其进行点击。 / p>