如何在flutter中创建这种类型的对话框。

问题描述 投票:0回答:1

我正在创建一个故事应用程序,其中两个用户讲述的故事像下面的图片,所以在这里我想创建一个对话框,像下面的图片。

H

flutter dart flutter-layout
1个回答
1
投票

您应该采用以下方式实现

class IntroPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _IntroPageState();
}

class _IntroPageState extends State<IntroPage>
    with SingleTickerProviderStateMixin {
  AnimationController animationController;
  bool _menuShown = false;

  @override
  void initState() {
    animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Animation opacityAnimation =
        Tween(begin: 0.0, end: 1.0).animate(animationController);
    if (_menuShown)
      animationController.forward();
    else
      animationController.reverse();
    return Scaffold(
      backgroundColor: Colors.amberAccent,
      body: Stack(
        overflow: Overflow.visible,
        children: <Widget>[
          Positioned(
            right: 0,
            top:90,
            child: InkWell(
              onTap: () {
                setState(() {
                  _menuShown = !_menuShown;
                });
              },
              child: Image.asset(
                'assets/images/girls.png',
                height: 250,
              ),
            ),
          ),
          Positioned(
            child: FadeTransition(
              opacity: opacityAnimation,
              child: _DialogUI(),
            ),
            right: 40.0,
            top: 300.0,
          ),
        ],
      ),
    );
  }
}

class _DialogUI extends StatelessWidget {
  _DialogUI();

  final double padding = 8.0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Material(
          clipBehavior: Clip.antiAlias,
          shape: _DialogShapeBorder(
              borderRadius: BorderRadius.all(Radius.circular(padding)),
              padding: padding),
          elevation: 4.0,
          child: Container(
            margin: const EdgeInsets.all(10),
            padding: EdgeInsets.all(padding).copyWith(bottom: padding * 2),
            child: Center(
              child: Text(
                  'Filler text is text that shares \nsome characteristics of a real written text, \n but is random or otherwise generated.\n It may be used to display a sample of fonts,\n generate text for testing, or to spoof an e-mail spam filter.'),
            ),
          )),
    );
  }
}

class _DialogShapeBorder extends RoundedRectangleBorder {
  _DialogShapeBorder({
    @required this.padding,
    side = BorderSide.none,
    borderRadius = BorderRadius.zero,
  }) : super(side: side, borderRadius: borderRadius);
  final double padding;

  @override
  Path getOuterPath(Rect rect, {TextDirection textDirection}) {

    return Path()
      ..moveTo(rect.width - 18.0, rect.top)
      ..lineTo(rect.width - 20.0, rect.top - 36.0)
      ..lineTo(rect.width - 52.0, rect.top)
      ..addRRect(borderRadius.resolve(textDirection).toRRect(Rect.fromLTWH(
          rect.left, rect.top, rect.width, rect.height - padding)));
  }
}

产量

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.