【问题标题】:How to stack a keyboard on flutter如何在颤振上堆叠键盘
【发布时间】:2019-05-22 23:12:09
【问题描述】:

目前我有一个小部件列表视图,并且我在堆栈顶部堆叠了一个底部按钮以始终显示。但是当我点击文本字段时,底部按钮被推到顶部,非常难看。如何将键盘推到 Button 上?

谢谢

这是一个代码示例:

 class MyHomePage extends StatefulWidget {

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

class _MyHomePageState extends State<MyHomePage> {

 final TextEditingController _controller = new TextEditingController();


  @override
  Widget build(BuildContext context) {

  return new Scaffold(
   appBar: new AppBar(
  ),
  body:   new Stack(
      children: <Widget>[

        new ListView(
      children: <Widget>[

            new Column(
              children: <Widget>[
  new Padding(
  padding: const EdgeInsets.all(100.0),),
  new Card(
    child: new Padding(
      padding: const EdgeInsets.all(10.0),
      child: new Row(
        children: <Widget>[
          new Expanded(
            child: new TextField(
              controller: _controller,
              decoration: new InputDecoration(hintText: "Enter an address"),
            ),
          ),

        ],
      ),
    ),
   ),
              ],

            )
          ],
        ),
        new Align
          (
          alignment: Alignment.bottomCenter,

          child :   new RaisedButton(
              child: new  Text("Button", style: TextStyle(color: Colors.black, fontWeight: FontWeight.w700, fontSize: 18.0,)),
              onPressed: (){
              },
              highlightElevation: 4.0,
              highlightColor  : Colors.white,
              shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
          ),
        ),
       ]
      )
    );
  }
}

【问题讨论】:

    标签: keyboard flutter stack


    【解决方案1】:

    我建议将“按钮”放在 floatingActionButton 内 Stack 之外的底部。

    floatingActionButton: FloatingActionButton.extended(
                elevation: 4.0,
                label: Text(Appliquer),
                onPressed: () {},
              ),
    floatingActionButtonLocation:
                  FloatingActionButtonLocation.centerDocked,
    

    添加代码后编辑:
    正如我在 cmets 中所说,我会在 AppBar 中使用 FloatingActionButton/BottomNaviagtionBar 或 Save-Icon

    在这里我将 FloatingActionButton 添加到您的代码中:

    class _MyHomePageState extends State<MyHomePage> {
      final TextEditingController _controller = new TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(),
            body: new Stack(children: <Widget>[
              new ListView(
                children: <Widget>[
                  new Column(
                    children: <Widget>[
                      new Padding(
                        padding: const EdgeInsets.all(100.0),
                      ),
                      new Card(
                        child: new Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: new Row(
                            children: <Widget>[
                              new Expanded(
                                child: new TextField(
                                  controller: _controller,
                                  decoration: new InputDecoration(
                                      hintText: "Enter an address"),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
    
                    ],
                  )
                ],
              ),
            ],
          ),
          floatingActionButton: FloatingActionButton.extended(
            icon: Icon(Icons.save), label: 
            new Text('Appliquer'), 
            onPressed: () { /*perform your Action here*/ },
          ),
        );
      }
    } 
    

    【讨论】:

    • 代码太长但结构简单。我不使用底部导航栏或浮动操作按钮。只是我使用 Stack 在第二个计划上显示一个列表视图,在第一个计划上显示一个按钮。当我调用键盘时,键盘是从第二个计划调用的,所以他和他一起按下了第一个计划的按钮。
    • 我测试了您的代码,这就是 Stack 的工作方式(有关详细信息,请参阅文档)。但我仍然建议您使用 FloatingActionButton 或 BottomNaviagtionBar 来达到您的目的(猜测是为了保存您的设置)或在 AppBar 中使用保存图标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2020-06-29
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多