如何在Flutter的showModalBottomSheet上添加按钮(或其他小部件)?

3

左边是设计,右边是我想构建这个设计的Flutter应用程序:

enter image description here

无论我尝试什么,都没有将按钮放在线条上面。

我尝试过的最后一件事是使用Stacked小部件:

showModalBottomSheet(
  context: context,
  shape: RoundedRectangleBorder(
    ...
  ),
  builder: (BuildContext context) {
    return Stack(
      alignment: AlignmentDirectional.topCenter,
      children: <Widget>[
        Container(
          width: 50,
          height: 50,
          child: Image.asset('assets/images/add.png'),
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: <Color>[
                CustomColors.PurpleLight,
                CustomColors.PurpleDark,
              ],
            ),
            borderRadius: BorderRadius.all(
              Radius.circular(50.0),
            ),
            boxShadow: [
              BoxShadow(
                color: CustomColors.PurpleShadow,
                blurRadius: 10.0,
                spreadRadius: 5.0,
                offset: Offset(0.0, 0.0),
              ),
            ],
          ),
        ),
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            SizedBox(height: 50),
            Text('Add new task'),
            RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              textColor: Colors.white,
              padding: const EdgeInsets.all(0.0),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8.0),
              ),
              child: Container(),
            ),
            SizedBox(height: 20),
          ],
        ),
      ],
    );
  },
);

我希望您能提供一些提示或好的解决方案 :)
1个回答

6
不要使用 showModalBottomSheet 参数的形状,而是将 showModalBottomSheet 设置为透明,然后将背景作为堆栈的一个子级添加并更改其形状。你可以在列中放置按钮。
请参考以下示例图:enter image description here
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  _onPressed() {
    showModalBottomSheet(
      backgroundColor: Colors.transparent,
      context: context,
      builder: (BuildContext context) {
        return Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Positioned(
              top: MediaQuery.of(context).size.height / 2 - 100,
              child: Container(
                height: 200,
                width: MediaQuery.of(context).size.width,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(top: Radius.elliptical(150, 30)),
                ),
              ),
            ),
            Positioned(
              bottom: 0,
              child: Column(
                children: <Widget>[
                  Container(
                    width: 50,
                    height: 50,
                    child: Image.asset('assets/images/add.png'),
                    decoration: const BoxDecoration(
                      gradient: LinearGradient(
                        begin: Alignment.topLeft,
                        end: Alignment.bottomRight,
                        colors: <Color>[
                          Colors.red,
                          Colors.blue,
                        ],
                      ),
                      borderRadius: BorderRadius.all(
                        Radius.circular(50.0),
                      ),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.purple,
                          blurRadius: 10.0,
                          spreadRadius: 5.0,
                          offset: Offset(0.0, 0.0),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 50),
                  Text('Add new task'),
                  RaisedButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    textColor: Colors.white,
                    padding: const EdgeInsets.all(0.0),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8.0),
                    ),
                    child: Container(),
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        child: RaisedButton(
      onPressed: _onPressed,
    ));
  }
}

太好了!非常简单。起初我只是在BottomSheet中使用了“RoundedRectangleBorder”,因为它是特别新的。 - Johan Walhout
欢迎您,如果回答有帮助,请点赞。StackOverflow评分系统不会计算特定标签评分中的已接受答案。 - Kherel
希望我能给你超过一个赞! - ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接