简体   繁体   English

Flutter 当我调用我新创建的自定义小部件时,其他小部件不显示

[英]Flutter other widget doesn`t show when I call my new created custom widget

Hello when ever I tried to call the bottombar widget (the custom widget that I created) The other widget elements dissapears您好,当我尝试调用底部栏小部件(我创建的自定义小部件)时,其他小部件元素消失

here is the home_page.dart code:这是home_page.dart代码:

import 'package:clothing_app/utils/bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int buttonSelected = 1;
  @override
  Widget build(BuildContext context) {
    return Material(
      child: Scaffold(
        bottomNavigationBar: BottomBar(),
        appBar: AppBar(
          elevation: 0,
          backgroundColor: SCAFFOLD_BG_COLOR,
          leading: IconButton(
            onPressed: () {},
            icon: Icon(
              LineAwesomeIcons.bars,
              color: MAIN_BLACK_COLOR,
            ),
          ),
          actions: <Widget>[
            IconButton(
              onPressed: () {},
              icon: Icon(
                LineAwesomeIcons.search,
                color: MAIN_BLACK_COLOR,
              ),
            ),
          ],
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: SingleChildScrollView(
                            padding: EdgeInsets.symmetric(
                                vertical: 12, horizontal: 16),
                            scrollDirection: Axis.horizontal,
                            child: Row(
                              children: <Widget>[
                                _topSlideItem(),
                                SizedBox(
                                  width: 24,
                                ),
                                _topSlideItem(),
                                SizedBox(
                                  width: 24,
                                ),
                                _topSlideItem(),
                                SizedBox(
                                  width: 24,
                                ),
                                _topSlideItem(),
                                SizedBox(
                                  width: 24,
                                ),
                                _topSlideItem(),
                                SizedBox(
                                  width: 24,
                                ),
                                _topSlideItem(),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                    Padding(
                      padding:
                          EdgeInsets.symmetric(vertical: 12, horizontal: 16),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            'Best of the week',
                            style: TextStyle(
                                fontSize: 22, fontWeight: FontWeight.bold),
                          ),
                          SizedBox(
                            height: 24,
                          ),
                          verticalSlideItem(),
                          SizedBox(height: 16),
                          verticalSlideItem(),
                          SizedBox(height: 16),
                          verticalSlideItem(),
                          SizedBox(height: 16),
                          verticalSlideItem(),
                          SizedBox(height: 16),
                          verticalSlideItem(),
                          SizedBox(height: 16),
                          verticalSlideItem(),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _topSlideItem() {
    return Material(
      borderRadius: BorderRadius.circular(32),
      color: Colors.white,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 32),
        width: MediaQuery.of(context).size.width * .55,
        child: Row(
          children: <Widget>[
            Image.asset(
              'assets/img.jpeg',
              height: 140,
              width: 50,
              fit: BoxFit.fitHeight,
            ),
            SizedBox(width: 24),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Flower patterned khaki dress',
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Row(
                    children: <Widget>[
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Text(
                    '\$89,99',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Color.fromRGBO(180, 184, 190, 1),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget verticalSlideItem() {
    return Material(
      borderRadius: BorderRadius.circular(32),
      color: Colors.transparent,
      child: Container(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(8),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(24)),
              child: Center(
                child: Image.asset(
                  'assets/img.jpeg',
                  height: 110,
                  width: 80,
                  fit: BoxFit.fitHeight,
                ),
              ),
            ),
            SizedBox(width: 24),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Flower patterned\nkhaki dress',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Row(
                    children: <Widget>[
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.red,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.blue,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Container(
                        height: 12,
                        width: 12,
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius: BorderRadius.circular(4),
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 12,
                  ),
                  Text(
                    '\$89,99',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Color.fromRGBO(180, 184, 190, 1),
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

here is the custom widget I created (bottom_bar.dart);这是我创建的自定义小部件(bottom_bar.dart);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.call),
              label: 'Calls',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.camera),
              label: 'Camera',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.chat),
              label: 'Chats',
            ),
          ],
        ),
      ),
    );
  }
}

here is the main.dart file;这里是main.dart文件;

import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(fontFamily: 'Cera'),
      home: HomePage(),
    );
  }
}

here is the image when I call the bottom navigationbar;这是我调用底部导航栏时的图像;

在此处输入图像描述

and here is an image when I remove the bottom navigationbar code line ( bottomNavigationBar: BottomBar(), );这是我删除底部导航栏代码行时的图像( bottomNavigationBar: BottomBar(), );

在此处输入图像描述

You are wrapping your custom BottomNavigationBar with Scaffold and it covers you HomePage Scaffold, so that's why the other widgets couldn't be shown.您正在用 Scaffold 包装自定义的 BottomNavigationBar,它会覆盖您的 HomePage Scaffold,这就是其他小部件无法显示的原因。

Remove the Scaffold from the custom BottomNavigationBar:从自定义的 BottomNavigationBar 中移除 Scaffold:

@override
Widget build(BuildContext context) {
  return BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.call),
        label: 'Calls',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.camera),
        label: 'Camera',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.chat),
        label: 'Chats',
      ),
    ],
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM