在Flutter应用中获取浮动操作按钮错误

普拉泰克·绍里娅

我是新手,我正在尝试制作一个应用程序,但我仅停留在初始阶段,无法弄清楚问题出在哪里。下面是我的代码:

import 'package:flutter/material.dart';

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

class BMICalculator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('BMI CALCULATOR'),
        ),
        body: InputPage(),
      ),
    );
  }
}

class InputPage extends StatefulWidget {
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('Body Text'),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {},
      ),
    );
  }
}

我的浮动操作按钮上出现错误。

下面是错误消息:

Compiler message:
lib/main.dart:29:7: Error: No named parameter with the name 'floatingActionButton'.
      floatingActionButton: FloatingActionButton(
      ^^^^^^^^^^^^^^^^^^^^
../../../desktop/flutter/flutter/packages/flutter/lib/src/widgets/basic.dart:1870:9: Context: Found this candidate, but the arguments don't match.
  const Center({ Key key, double widthFactor, double heightFactor, Widget child })
        ^^^^^^

我需要在屏幕中央显示正文文本,并在右下角单击按钮。

模棱两可的

您的代码几乎是正确的。唯一的错误是,您正在尝试将“浮动操作按钮”(FAB)设置为“中心”的参数。而是将其放在这样的列中:

import 'package:flutter/material.dart';

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

class BMICalculator extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('BMI CALCULATOR'),
        ),
        body: InputPage(),
      ),
    );
  }
}

class InputPage extends StatefulWidget {
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text('Body Text'),
          FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {},
          ),
        ],
      ),
    );
  }
}

虽然我刚才键入的代码可以正常工作,但您可能想知道FAB通常与脚手架一起使用。它不是必须的,但这就是大多数人使用它的方式。这就是为什么FAB有专用的脚手架参数的原因。您可以这样做:

Scaffold(
    floatingActionButton: FloatingActionButton(
      child: const Icon(Icons.add),
      onPressed: () {},
    ),
    appBar: AppBar(
      ....... 
  )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章