【问题标题】:The 'body' part is not visible after using ' bottomNavigationBar' in Flutter在 Flutter 中使用“bottomNavigationBar”后,“body”部分不可见
【发布时间】:2020-05-04 07:42:24
【问题描述】:

每当我使用 bottomNavigationBar: 时,它都不会在屏幕上显示 body: 部分,但是当我删除 bottomNavigationBar: 时,它会显示身体: 这是代码

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home', textAlign: TextAlign.center),
        actions: <Widget>[],
        backgroundColor: Color(0xffd81b60),
      ),
      bottomNavigationBar: _getNavBar(context),

      body: ListView(children: <Widget>[
        SizedBox(height: 300.0),
        Container(
          height: 50,
          width: 10,
          child: Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => mealwisePage()));
              },
              color: Colors.pink,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  'Meal Wise',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),), ), ), ), ]),);}
_getNavBar(context) {
  return Stack(
    children: <Widget>[
      Positioned(
        bottom: 0,
        child: ClipPath(
          clipper: NavBarClipper(),
          child: Container(
            height: 50,
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                  Colors.pink,
                  Colors.pink.shade800,
                ])), ),),),],);}

没有错误显示只是身体在屏幕上不可见

请问有什么解决办法吗?

【问题讨论】:

    标签: flutter dart flutter-layout flutter-appbar flutter-design


    【解决方案1】:

    我发现这是因为使用 Stack 它与正文重叠,所以我改变了它 来自:

    return Stack(
        children: <Widget>[
          Positioned(
            bottom: 0,
            child: ClipPath(
              clipper: NavBarClipper(),
              child: Container(),),)],)
    
    

    到

    return Container(
          height: 90,
          child: Stack(
            alignment: Alignment.bottomCenter,
        children: <Widget>[
          Positioned(
            bottom: 0,
            child: ClipPath(
              clipper: NavBarClipper(),
              child: Container(),),),],),)
    

    【讨论】:

      【解决方案2】:

      底部导航是脚手架构造函数Widget bottomNavigationBar 的一部分,而不是主体,因此重构您的代码,如下所示

           Scaffold(
            appBar: AppBar(
              title: const Text('Home', textAlign: TextAlign.center),
              backgroundColor: Color(0xffd81b60),
            ),
            resizeToAvoidBottomPadding: false,
      
            body: ListView(children: <Widget>[
              Container(
                child:Column(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.fromLTRB(15.0, 40.0, 40.0, 25.0),
                      child: Center(
                        child: Text(
                          'Home',
                          textAlign: TextAlign.center,
                        ),),), ], ),),
              SizedBox(height: 200.0),         
      
          ]),//listview
       bottomNavigationBar: _getNavBar(context),
      );//scaffold
      

      更新编辑

      child: Container(
                  height: 50,
                  width: MediaQuery.of(context).size.width,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                          begin: Alignment.topCenter,
      

      你必须给这个width: MediaQuery.of(context).size.width, 例子width: MediaQuery.of(context).size.width * .98,一个值

      【讨论】:

      • 代码如你所说,实际上在发布问题时stackoverflow建议我减少代码,所以在减少代码的同时它变得像这样抱歉,现在我已经更新了代码问题。
      • 对我不起作用,实际上这不是完整的代码,我无法在问题中发布完整的代码。这是我使用的底部导航栏的完整代码:github.com/stevendz/customappbar_yt/blob/master/lib/main.dart
      • 我确定了,这是因为 Stack Widget,所以我在一个高度为 100 的容器中使用它跨度>
      猜你喜欢
      • 2020-08-09
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2021-05-15
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多