reactjs 如何修复材质用户界面中的导航栏?

lmvvr0a8  于 11个月前  发布在  React
关注(0)|答案(1)|浏览(98)

x1c 0d1x的数据
我目前正在开发一个React应用程序,我遇到了一个关于Material-UI AppBar组件中组件对齐的问题。我的目标是为三个关键组件实现无缝和直线对齐:徽标图像,标题和登录按钮。
Navbar.js

import { AppBar, Typography, Toolbar, Avatar, Button, Box,Container } from "@mui/material";
import React from "react";
import { Link } from 'react-router-dom'
import useStyles from './styles'
import memories from '../../images/memories.png'

const Navbar = () => {
    const classes = useStyles()
    const user = null;
    return (
        // <Box sx={{ flexGrow: 1 }}>
        <>
            <AppBar className={classes.appBar} position='static' color='inherit'>
            <Container maxWidth="xl">
            <div className={classes.brandContainer}>
                <img className={classes.image} src={memories}  alt="memories" height="50" />
                <Typography variant='h4' nowrap className={classes.heading} >Travel Blog</Typography>
            </div>
            <Toolbar className={classes.toolbar}>
                {user ? (
                    <div className={classes.profile}>
                        <Avatar className={classes.purple} alt = {user.result.name} src = {user.result.imageUrl}>{user.result.name.charAt(0)}</Avatar>
                        <Typography className={classes.userName} variant = 'h6'>{user.result.name}</Typography>
                        <Button variant="contained" className={classes.logout} color="secondary">Logout</Button>

                    </div>
                ) : (
                    
                    <Button component={Link} className={classes.login} to='/auth' variant="contained" color="primary"> Sign In</Button>
                )}
            </Toolbar>
            </Container>
        </AppBar>
        </>
    )
}

export default Navbar

字符串
Styles.js

import { makeStyles } from "@mui/styles";
import { useTheme } from '@mui/material/styles';
import { deepPurple } from "@mui/material/colors";

export default makeStyles(() => ({
  appBar: {
    borderRadius: 15,
    margin: '30px 0',
    display: 'flex',
    flexDirection: 'row',
    justifyContent: 'space-between',
    // verticalAlign: 'middle',
    alignItems: 'center',
    padding: '10px 50px',
  },
  heading: {
    color: 'rgba(0,183,255, 1)',
    textDecoration: 'none',
  },
  image: {
    marginLeft: '15px',
  },
  toolbar: {
    display: 'flex',
    flexDirection: 'row',
    justifyContent: 'flex-end',
    alignItems: 'center'
    // width: '400px',

  },
  profile: {
    display: 'flex',
    justifyContent: 'space-between',
    width: '400px',
    gap: '15px', 
  },
  userName: {
    display: 'flex',
    alignItems: 'center',
  },
  brandContainer: {
    display: 'flex',
    alignItems: 'center',
  },
  purple: {
    color: useTheme().palette.getContrastText(deepPurple[500]),
    backgroundColor: deepPurple[500],
  },
}));

qcuzuvrc

qcuzuvrc1#

尝试将显示Flex和Flex-Direction行添加到容器

相关问题