【问题标题】:Blank screen displayed when I added container widget in flutter当我在颤动中添加容器小部件时显示空白屏幕
【发布时间】:2020-10-26 12:09:08
【问题描述】:

我想在第一个屏幕上按下按钮时显示第二个屏幕。为此,我正在使用navigation.push。直到我在第二个屏幕布局的行小部件中添加一个容器小部件之前,它都可以正常工作。当我删除它时,它工作正常。谁能告诉我我在这里做错了什么? 这是我第二个屏幕的代码

import 'package:flutter/material.dart';

class NewScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.only(top: 50.0, bottom: 60.0),
        child: Column(
           children: <Widget>[
           
             ClipRRect(
                 borderRadius: BorderRadius.circular(200.0),
                 child: Image.asset(
                   'assets/images/test.png',
                   height: 200.0,
                   width: 200.0,)
             ),


             SizedBox(height: 20.0,),
             Text(
               'Remote User',
               style: TextStyle(
                   color: Colors.black,
                   fontWeight: FontWeight.w700,
                   fontSize: 25),
             ),
             SizedBox(height: 20.0,),
             Text(
               'Incoming Call',
               style: TextStyle(
                   color: Colors.black,
                   fontWeight: FontWeight.w400,
                   fontSize: 15),
             ),
             SizedBox(height: 120.0,),
             
             Row(
               children: <Widget>[
               //when I add single container it works fine
                 Container(
                   height: 70.0,
                   width: 70.0,
                   child: FittedBox(
                     child: FloatingActionButton(
                       onPressed: () {},
                       elevation: 20.0,
                       //shape: CircleBorder(side: BorderSide(color: Colors.red)),
                       mini: false,
                       child: Icon(
                         Icons.call,
                         color: Colors.white,
                       ),
                       backgroundColor: Colors.green,
                     ),
                   ),
                 ),
                 // this widget cause blank screen
                 Container(
                   height: 70.0,
                   width: 70.0,
                   child: FittedBox(
                     child: FloatingActionButton(
                       onPressed: () {},
                       elevation: 20.0,
                       //shape: CircleBorder(side: BorderSide(color: Colors.red)),
                       mini: false,
                       child: Icon(
                         Icons.call,
                         color: Colors.white,
                       ),
                       backgroundColor: Colors.green,
                     ),
                   ),
                 ),
               ],
             ),
        ]
      ),
      ),
    );
  }
}

这里是第一屏的代码:

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Second Screen'),
          onPressed: () {
          Navigator.push(context, MaterialPageRoute(builder: (context) => NewScreen()),);
          },
        ),
      ),
    );
  }
}

【问题讨论】:

标签: flutter dart flutter-layout


【解决方案1】:

有一些不需要的填充,比如

left: 400.0, right: 400.0

提到正确的,删除​​那些你可以看到你的小部件,试试下面的代码

padding:
        EdgeInsets.only(top: 50.0, bottom: 60.0),

【讨论】:

  • 感谢您的回答:)。我删除了填充,但它仍然不起作用。它仅在我删除第二个小部件时才有效。
  • 重启应用。
  • 我这样做了,但结果相同。当我在 main.dart 中使用它时,它会正确显示。但是当我使用 Navigation.push 从 firstscreen.dart 启动它时,会发生这种情况
【解决方案2】:

首先,去掉第9行的左右内边距

padding: EdgeInsets.only(top: 50.0, bottom: 60.0, left: 400.0, right: 400.0),

您正面临这个问题,因为您在一个屏幕上添加了两个浮动操作按钮

试试这个:

import 'package:flutter/material.dart';

 class NewScreen extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
  return Scaffold(
   body: Container(
    color: Colors.white,
    padding: EdgeInsets.only(top: 50.0, bottom: 60.0),
    child: Column(
       children: <Widget>[
       
         ClipRRect(
             borderRadius: BorderRadius.circular(200.0),
             child: Image.asset(
               'assets/images/test.png',
               height: 200.0,
               width: 200.0,)
         ),


         SizedBox(height: 20.0,),
         Text(
           'Remote User',
           style: TextStyle(
               color: Colors.black,
               fontWeight: FontWeight.w700,
               fontSize: 25),
         ),
         SizedBox(height: 20.0,),
         Text(
           'Incoming Call',
           style: TextStyle(
               color: Colors.black,
               fontWeight: FontWeight.w400,
               fontSize: 15),
         ),
         SizedBox(height: 120.0,),
         
         Row(
           children: <Widget>[

           //when I add single container it works fine
           //EDITED PART

             Container(
               height: 70.0,
               width: 70.0,
               decoration: BoxDecoration(
                 color: Colors.green,
                 shape: BoxShape.circle
               ),
               child: GestureDetector(
                 onTap: () {},
                 //shape: CircleBorder(side: BorderSide(color: Colors.red)),
                //  mini: false,
                 child: Icon(
                   Icons.call,
                   color: Colors.white,
                 ),
                //  backgroundColor: Colors.green,
               ),
             ),

             // this widget cause blank screen
             //EDITED PART

             Container(
               height: 70.0,
               width: 70.0,
               decoration: BoxDecoration(
                 color: Colors.green,
                 shape: BoxShape.circle
               ),
               child: GestureDetector(
                 onTap: () {},
                 //shape: CircleBorder(side: BorderSide(color: Colors.red)),
                //  mini: false,
                 child: Icon(
                   Icons.call,
                   color: Colors.white,
                 ),
                //  backgroundColor: Colors.green,
               ),
             ),
           ],
         ),
       ]
     ),
    ),
  );
 }
}

可能对你有帮助

【讨论】:

  • 感谢您的回答。但它仍然不起作用。当我直接从 main.dart 显示它时它的工作原理。但是当我从我的第一个屏幕推送它时,就会出现这个问题
  • 我认为您遇到了这个问题,因为您在一个屏幕上添加了两个浮动操作按钮。让我为你更正一下。
  • 哇。谢谢老哥给我指路。你是对的,这是因为使用了两个浮动按钮。当它将“HeroTag”的值设置为 null 时,它起作用了。
猜你喜欢
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2019-10-27
  • 2021-11-16
  • 2021-10-22
  • 2013-12-13
  • 2021-09-03
  • 2021-11-26
相关资源
最近更新 更多