下面是一个例子

enter image description here

你可以看到障碍物的 colored颜色 是渐变的,而在Fightle中,showDialog个小部件只有一个名为barrierColor的参数.

有什么方法可以设置barrier colored颜色 渐变吗?

这是给出的图片的showDialog代码

showDialog(
                barrierColor: AppColor.pink.withAlpha(200),
                context: context,
                builder: (BuildContext context) {
                  return Dialog(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(18.0),
                    ),
                    child: Container(
                      height: 300,
                      padding: const EdgeInsets.all(20.0),
                      child: Column(
                        children: [
                          Row(
                            children: [
                              Text(
                                option,
                                style: const TextStyle(
                                    fontSize: 18,
                                    color: Colors.black,
                                    fontWeight: FontWeight.w500),
                              ),
                            ],
                          ),
                         const SizedBox(height: 8),
                          const TextField(
                            cursorColor: Colors.black,
                            textInputAction: TextInputAction.newline,
                             maxLines: null,
                            decoration: InputDecoration(
                              hintText: 'Tell us...',
                              border: InputBorder.none,
                            ),
                          ),
                          const Spacer(),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: const Text(
                                  'Cancel',
                                  style: TextStyle(
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                              TextButton(
                                onPressed: () {
                                  Navigator.of(context).pop();
                                },
                                child: const Text(
                                  'Save',
                                  style: TextStyle(
                                    color: Colors.black,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  );
                },
              );

这是代码,我想把障碍色实现为渐变色,

推荐答案

Container小部件的decoration属性中有渐变参数.因此您可以用Container个小部件包装AlertDialog小部件并使用渐变属性.

NOTE : - Whichever color you give, add opacity to it or else the background won't be visible because by default it has solid color.

示例代码:-

Container(
decoration: BoxDecoration(
  gradient: LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [
    const Color.fromARGB(255, 255, 36, 226).withOpacity(0.2),
    const Color.fromARGB(255, 39, 205, 255).withOpacity(0.2)
              ]
            )
        )
    )
 ),

完整代码:-

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('AlertDialog Sample')),
        body: const Center(
          child: DialogExample(),
        ),
      ),
    );
  }
}

class DialogExample extends StatelessWidget {
  const DialogExample({super.key});

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () => showDialog<String>(
        context: context,
        builder: (BuildContext context) => Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                const Color.fromARGB(255, 255, 36, 226).withOpacity(0.2),
                const Color.fromARGB(255, 39, 205, 255).withOpacity(0.2)
              ])),
          child: AlertDialog(
            title: const Text('AlertDialog Title'),
            content: const Text('AlertDialog description'),
            actions: <Widget>[
              TextButton(
                onPressed: () => Navigator.pop(context, 'Cancel'),
                child: const Text('Cancel'),
              ),
              TextButton(
                onPressed: () => Navigator.pop(context, 'OK'),
                child: const Text('OK'),
              ),
            ],
          ),
        ),
      ),
      child: const Text('Show Dialog'),
    );
  }
}

发布帖子:--

Flutter相关问答推荐

无效字符(位于字符68处)flutter—update—... europe-west1.firebasedatabase.app/

为什么Flutter 翼扩展卡有上下边框?

BoxConstraints强制使用无限宽度(列中的Listview)

如何将请求字段正确添加到DART多部分请求

如何更改Flutter 中文本的混合模式?

找不到任何与 com.google.firebase:firebase-sessions:[15.0.0, 16.0.0) 匹配的版本

即使小部件比Flutter 中的屏幕更宽,也始终显示滚动条

如何使Flutter 边框宽度在外面而不是在里面?

以编程方式在 flutter 中安装 apk 时解析错误

Riverpod 注册多少个 providers

不使用 GlobalKey 仍然收到 GlobalKey 被多次使用的错误

Flutter Flame OS 错误:使用音频文件 .wav 时出现该进程无法访问该文件,因为它正被另一个进程使用

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

在 flutter 中获取最喜欢的文档

Flutter:为什么我的 listTile colored颜色 被容器 colored颜色 覆盖了?

如何在向下滚动时隐藏顶部卡片并在向上滚动时出现?

如何在手势检测器中获得涟漪效应

Flutter 中父容器顶部的 Gridview 间距额外区域

Firebase Cloud Messaging (Flutter):订阅主题时出现错误消息

Flutter Desktop,如何获取windows用户配置文件名称?