showModalBottomSheet() 在Flutter中没有打开底部模态窗口。

huangapple go评论71阅读模式
英文:

showModalBottomSheet() not opening a bottom modal in flutter

问题

The showModalBottomSheet方法没有弹出模态框。我是Flutter和Dart的新手,但我认为这是一个问题,我不需要任何帮助。简单的页面弹出底部模态框似乎没有起作用。

英文:

The showModalBottomSheet does not pop up a modal. I am new to flutter and dart but I think this is a problem, I would not any help. The simple page to pop a bottom sheet modal below.. did not work.

```
  import 'package:flutter/material.dart';

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

   class MyApp extends StatelessWidget {
     @override
      Widget build(BuildContext context) {

        return new MaterialApp(
           home: new Scaffold(
             appBar: new AppBar(title: const Text('Modal bottom sheet')),
             body: new Center(
               child: new RaisedButton(
                child: const Text('SHOW BOTTOM SHEET'),
               onPressed: () {
                 showModalBottomSheet<void>(context: context, builder: (BuildContext context) 
               {
            return new Container(
              child: new Padding(
                padding: const EdgeInsets.all(32.0),
                child: new Text('This is the modal bottom sheet. Click anywhere to dismiss.',
                  textAlign: TextAlign.center,
                  style: new TextStyle(
                    color: Theme.of(context).accentColor,
                    fontSize: 24.0
                  )
                )
              )
            );
          });
        }
      )
    )
  )
);

}
}
```

I cannot seem to figure out what the probl

答案1

得分: 3

你可以将你的身体转化为一个新的小部件。

Dart Pad 示例

完整代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('模态底部表单')),
        body: Sheet(), // 新的小部件
      ),
    );
  }
}

class Sheet extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        child: const Text('显示底部表单'),
        onPressed: () {
          // 在这里显示表单
          showModalBottomSheet<void>(
            context: context,
            builder: (BuildContext context) {
              return Container(
                child: Padding(
                  padding: const EdgeInsets.all(32.0),
                  child: Text(
                    '这是模态底部表单。单击任意位置以关闭。',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: Theme.of(context).accentColor,
                      fontSize: 24.0,
                    ),
                  ),
                ),
              );
            },
          );
        },
      ),
    );
  }
}
英文:

You can convert your body to a new widget.

Dart Pad Example

Full code:

import &#39;package:flutter/material.dart&#39;;

void main() =&gt; runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(title: const Text(&#39;Modal bottom sheet&#39;)),
            body: Sheet(), // new Widget
   ));
  }
}


class Sheet extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Center(
      child: new RaisedButton(
          child: const Text(&#39;SHOW BOTTOM SHEET&#39;),
          onPressed: () {
            // Show sheet here
            showModalBottomSheet&lt;void&gt;(
                context: context,
                builder: (BuildContext context) {
                  return new Container(
                    child: new Padding(
                      padding: const EdgeInsets.all(32.0),
                      child: new Text(
                        &#39;This is the modal bottom sheet. Click anywhere to dismiss.&#39;,
                        textAlign: TextAlign.center,
                        style: new TextStyle(
                            color: Theme.of(context).accentColor,
                            fontSize: 24.0),
                      ),
                    ),
                  );
                });
          }),
    );
  }
}

答案2

得分: 0

<h3>大部分情况下它不起作用是因为它存在上下文问题</h3>

确保提供上下文

_showBottomSheet(BuildContext context) {
  showModalBottomSheet<void>(
    context: context,
    builder: (BuildContext context) {
      return new Container(
        child: new Padding(
          padding: const EdgeInsets.all(32.0),
          child: new Text(
            '这是模态底部表。单击任何位置以关闭。',
            textAlign: TextAlign.center,
            style: new TextStyle(
              color: Theme.of(context).accentColor,
              fontSize: 24.0,
            ),
          ),
        ),
      );
    },
  );
}
英文:

<h3>Mostly it does not work because it has a problem of context</h3>

make sour you give context

_showBottomSheet(BuildContext context) {
showModalBottomSheet&lt;void&gt;(
    context: context,
    builder: (BuildContext context) {
      return new Container(
        child: new Padding(
          padding: const EdgeInsets.all(32.0),
          child: new Text(
            &#39;This is the modal bottom sheet. Click anywhere to dismiss.&#39;,
            textAlign: TextAlign.center,
            style: new TextStyle(
                color: Theme.of(context).accentColor,
                fontSize: 24.0),
          ),
        ),
      );
    });
}

答案3

得分: -1

为解决这种问题,您需要延迟触发模态底部表单。

onTap: () {
  Future.delayed(Duration(seconds: 0)).then((value) {
    showModalBottomSheet<void>(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          child: new Padding(
            padding: const EdgeInsets.all(32.0),
            child: new Text(
              '这是模态底部表单。单击任何位置以关闭。',
              textAlign: TextAlign.center,
              style: new TextStyle(
                color: Theme.of(context).accentColor,
                fontSize: 24.0),
            ),
          ),
        );
      });
  });
}
英文:

To solve this kind of problem, you have to delay triggering the modal bottomsheet.

    onTap :() {
    Future.dalayed(Duration(seconds: 0).then((value) {
      showModalBottomSheet&lt;void&gt;(
      context: context,
       builder: (BuildContext context) {
       return new Container(
        child: new Padding(
        padding: const EdgeInsets.all(32.0),
         child: new Text(
        &#39;This is the modal bottom sheet. Click anywhere to dismiss.&#39;,
        textAlign: TextAlign.center,
        style: new TextStyle(
            color: Theme.of(context).accentColor,
            fontSize: 24.0),
                   ),
                  ),
                 );
                });

              }

          }

huangapple
  • 本文由 发表于 2020年1月6日 16:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定