如何在代码中添加浮动操作按钮

时间:2019-07-18 09:56:37

标签: flutter dart scaffold

我正在尝试在flutter应用程序中添加浮动动作按钮,由于只能实现一个孩子,所以我不知道应该将浮动动作按钮的代码放在哪里。

我可以将其作为子项放在列中,但它不会出现在屏幕的左下角。我还尝试创建另一个类,然后将其放在脚手架小部件中,但是我的_favCity将不包含在内

....
class _favCity extends State<favCity> {
  String city1, city2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Email Login"),
        centerTitle: true,
      ),
      backgroundColor: Colors.cyan,
      body: Container(
        margin: EdgeInsets.all(40.0),
        color: Colors.brown,
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10.0),
              //color: Colors.white,
              child: TextField(
                decoration: new InputDecoration(
                  labelText: "Enter City 1",
                  fillColor: Colors.orange,
                  border: new OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                ),
                onSubmitted: (String userInput) {
                  setState(() {
                    city1 = userInput;
                  });
                },
              ),
            ),
            Container(
              padding: EdgeInsets.all(10.0),
             // color: Colors.white,
              child: TextField(
                decoration: InputDecoration(
                  labelText: "Enter city 2",
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                  fillColor: Colors.orange,
                ),
                onChanged: (String UserInput) {
                  setState(() {
                    city2 = UserInput;
                  });
                },
              ),
            ),
            Text("$city1 Is better than $city2"),

          ],
        ),

      ),
    );
  }
}

3 个答案:

答案 0 :(得分:1)

floating action button也是widget,可以使用以下代码放置在应用中。

import 'package:flutter/material.dart';

class FAButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FAB Button Example'),
      ),
      body: Center(
        child: Text('Floating Action Button Screen'),
      ),
      //FAB is a property of the `Scaffold` Widget
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        //Widget to display inside Floating Action Button, can be `Text`, `Icon` or any widget.
        onPressed: () {
          //Code to execute when Floating Action Button is clicked
          //...
        },
      ),
    );
  }
}

有关FAB的更多参考或进一步对其进行自定义,请参见此 Link

答案 1 :(得分:0)

对于floatActionButton,您可以看到此document

我以这种方式使用它没有问题:

          floatingActionButton: FloatingActionButton(
            backgroundColor: Theme.of(context).primaryColor,
            child: Icon(
              Icons.comment,
              color: Colors.white,
            ),
            onPressed: () {
              // do something
            },
          ),

答案 2 :(得分:0)

要在您的脚手架中添加FloatingActionButton,您需要使用以下代码:

@override
Widget build(BuildContext context) {
    return Scaffold(
        body: Container() // something you want to put in the body
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.check),
            onPressed: () { // ... function you want use when on pressed }
            backgroundColor: Color(0xFFE57373),
        ),
        // if you want change position of floating action button is here
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
}