我的代码有什么问题?它只显示黑屏

时间:2019-09-02 11:14:43

标签: flutter flutter-layout

我正在尝试制作一个应用程序,现在我要做的就是创建一个带有阴影的容器。

它说了无效的代码,所以我删除了一些代码,然后尝试了其他一些事情。

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

class appBar extends StatelessWidget {
    appBar({this.title});

    final Widget title;

    @override 
    Widget build(BuildContext context) {
        return Container(
            height: 60.0,
            padding: const EdgeInsets.all(10.0));
            decoration: new BoxDecoration(color: Colors.cyan[500]);
            child: Container(
            decoration: new BoxDecoration(
                boxShadow:[
                    BoxShadow(
                        color: Colors.black12,
                        blurRadius: 20,
                        spreadRadius: 4.0,
                        offset: Offset(
                            8.0,
                            8.0,
                        )
                    ),
                ]
            ));
    }
}

我希望有一个容器,但黑屏。

5 个答案:

答案 0 :(得分:0)

您的代码中有太多错误,在每一行的末尾都不必要地使用了class SignUpAuthViewModel : ViewModel() { var username: String? = null var password: String? = null var town: String? = null var age: String? = null var phone: String? = null var passwordConfirmation: String? = null //this variable should hold the city of the user // from spinner of cities var cityOfUser: String? = null fun OnRegisterButtonClicked(view: View) { } fun onRegisterTextViewClicked(view: View) { } } ,并确保您的类名以大写字母开头。

这是100%的工作代码。

;

答案 1 :(得分:0)

  • appBar不是一个很好的类名。约定是使用大写字母开头类名称。因此AppBar会更好。
  • 但是AppBar是一个不确定的类,所以...不是一个好选择;
  • 您的代码中有一些“;”相反,您应该在其中使用“,”(您位于Container函数调用中,而不是普通的代码正文中)例如:padding: const EdgeInsets.all(10.0));

因此,我进行了更正并以此方式测试了您的代码(这是新创建的flutter项目中的main.dart文件):

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return AppBarCustom(title: Text('teste'));
  }
}

class AppBarCustom extends StatelessWidget {
  AppBarCustom({this.title});

  final Widget title;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60.0,
      padding: const EdgeInsets.all(10.0),
      decoration: new BoxDecoration(color: Colors.cyan[500]),
      child: Container(
        decoration: new BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
                color: Colors.black,
                blurRadius: 20,
                spreadRadius: 10.0,
                offset: Offset(
                  8.0,
                  8.0,
                )),
          ],
        ),
      ),
    );
  }
}

我为您的内部容器添加了白色背景色,只是为了更清楚地显示它,并弄乱了阴影...

在代码末尾注意您的课程。

他是最终的渲染图: enter image description here

答案 2 :(得分:-1)

缺少材料小部件时,您会看到黑屏。将整个小部件包装为Scafold即可。

我还看到了一些错误的语法,所以这里是解决方法。

class appBar extends StatelessWidget {
  appBar({this.title});

  final Widget title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        height: 60.0,
        padding: const EdgeInsets.all(10.0),
        decoration: new BoxDecoration(color: Colors.cyan[500]),
        child: Container(
          decoration: new BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: Colors.black12,
                blurRadius: 20,
                spreadRadius: 4.0,
                offset: Offset(
                  8.0,
                  8.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

答案 3 :(得分:-1)

不要在Container中使用装饰来获取颜色。只需将颜色小部件添加到您的容器中即可。一个例子如下

return Container(
     color:Colors.cyan
     )

答案 4 :(得分:-1)

您必须使用Scaffold小部件扭曲容器。你会得到爱你的问题!享受。因为在颤动的脚手架中,黑色屏幕带有白色帆布。

相关问题