有没有办法改变对话框的形状?

时间:2018-10-03 16:04:39

标签: flutter

我正在查看不同对话框类的属性,但没有发现任何会改变其形状的东西。有没有办法改变对话框的形状?

3 个答案:

答案 0 :(得分:2)

您可以使用Container结合现有的裁剪小部件(Painting and Effect Widgets)或扩展CustomClipper来创建各种形状的对话框。下面将为您提供菱形对话框。有ClipOval之类的现有小部件可以直接使用,无需任何自定义(请参见下面的屏幕截图)。如果您想试用ClipOval,只需将ClipPath替换为ClipOval并注释掉clipper:。查看painting.dart类,以了解有关创建自定义路径的信息。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Shaped Dialog Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        dialogBackgroundColor: Colors.transparent,
      ),
      home: MyHomePage(title: 'Flutter Shaped Dialog Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            _showShapedDialog();
          }),
    );
  }

  _showShapedDialog() {
    showDialog(
      context: context,
      builder: (context) {
        return Padding(
          padding: const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
          child: ClipPath(
            child: Material(
              color: Colors.white,
              child: Center(
                child: Container(
                  alignment: FractionalOffset.center,
                  height: MediaQuery.of(context).size.width / 2.0,
                  width: MediaQuery.of(context).size.width / 2.0,
                  decoration: BoxDecoration(
                    border: Border.all(),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(bottom: 20.0),
                        child: Text(
                          'Clipping to a path is expensive. Certain shapes have more optimized widgets.',
                          textAlign: TextAlign.center,
                        ),
                      ),
                      FlatButton(
                        child: Text(
                          'OK',
                          style: TextStyle(color: Colors.blue),
                        ),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ),
            clipper: _MyClipper(), // Comment this out if you want to replace ClipPath with ClipOval
          ),
        );
      },
    );
  }
}

class _MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    final path = Path();
    path.lineTo(size.width / 2.0, 0.0);
    path.lineTo(0.0, size.height / 2.0);
    path.lineTo(size.width / 2.0, size.height);
    path.lineTo(size.width, size.height / 2.0);
    path.lineTo(size.width / 2.0, 0.0);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

Diamond-shaped dialog using ClipPath

Dialog using ClipOval

答案 1 :(得分:1)

喜欢

Dialog(
  shape: RoundedRectangleBorder(
    borderRadius: BorderRadius.circular(8.0),
  ),
  child: Text('Dialog'),
);

答案 2 :(得分:1)

AlertDialog(
   shape: RoundedRectangleBorder(borderRadius: 
    BorderRadius.all(Radius.circular(15))),
    title: Text('Your title!'),
    content: Container(),
);