1

在此处输入图像描述

需要Alexis Sanchez文本位于图片下方,但由于某种原因,它位于屏幕底部,我无法弄清楚原因。这是我的代码:

 import 'package:flutter/material.dart';

import '../../app_theme.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({Key? key}) : super(key: key);

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

class _ProfileScreenState extends State<ProfileScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: AppTheme.lightBlue,
        title: Text(
          'Profile',
          style: TextStyle(color: AppTheme.black100),
        ),
        elevation: 0,
      ),
      body: Column(
        children: [
          SizedBox(
            height: 50,
          ),
          Stack(
            clipBehavior: Clip.none,
            alignment: Alignment.center,
            children: [
              Container(
                width: double.infinity,
                height: 150,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                      topRight: Radius.circular(16),
                      topLeft: Radius.circular(16),
                    ),
                    color: AppTheme.lightGrey),
              ),
              Positioned(
                top: -50,
                child: _buildProfilePhoto(),
              ),
              Positioned(
                top: 60,
                child: Text(
                  'Alexis Sanchez',
                  style: TextStyle(
                    color: AppTheme.black100,
                    fontSize: 22,
                    fontWeight: FontWeight.w600,
                    fontStyle: FontStyle.normal,
                    fontFamily: 'Poppins',
                    height: 33,
                  ),
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          )
        ],
      ),
      backgroundColor: AppTheme.lightBlue,
      drawer: Drawer(),
    );
  }

  Widget _buildProfilePhoto() {
    return Stack(
      clipBehavior: Clip.none,
      children: [
        CircleAvatar(
          radius: 50,
          backgroundImage: AssetImage('assets/images/profile_photo.jpg'),
        ),
        Positioned(
          left: 78,
          top: 60,
          child: CircleAvatar(
            radius: 14,
            backgroundColor: AppTheme.blue,
            child: Icon(Icons.camera_alt_outlined),
          ),
        ),
      ],
    );
  }
}

有人可以在这里帮助我吗?

4

2 回答 2

2

尝试使用Column这样的:

Positioned(
  top: -50,
  child: Column(
     crossAxisAlignment: CrossAxisAlignment.center,
     children: [
       _buildProfilePhoto(),
       SizedBox(height: 5),
       Text(
          'Alexis Sanchez',
           style: TextStyle(
             color: AppTheme.black100,
             fontSize: 22,
             fontWeight: FontWeight.w600,
             fontStyle: FontStyle.normal,
             fontFamily: 'Poppins',
             height: 33,
           ),
           textAlign: TextAlign.center,
       ),
     ]
  )
),
于 2022-01-28T09:37:50.697 回答
1

完整的工作代码:

import 'package:flutter/material.dart';

class ProfileScreen extends StatefulWidget {
  const ProfileScreen({Key? key}) : super(key: key);

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

class _ProfileScreenState extends State<ProfileScreen> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text(
            'Profile',
          ),
          elevation: 0,
        ),
        body: Padding(
          padding: const EdgeInsets.only(top: 20.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              _buildProfilePhoto(),
              Text(
                'Alexis Sanchez',
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w600,
                  fontStyle: FontStyle.normal,
                  fontFamily: 'Poppins',
                ),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
        drawer: Drawer(),
      ),
    );
  }

  Widget _buildProfilePhoto() {
    return Center(
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          CircleAvatar(
            radius: 50,
            backgroundImage: AssetImage('assets/images/profile_photo.jpg'),
          ),
          Positioned(
            left: 78,
            top: 60,
            child: CircleAvatar(
              radius: 14,
              backgroundColor: Colors.blue,
              child: Icon(Icons.camera_alt_outlined),
            ),
          ),
        ],
      ),
    );
  }
}

结果:在此处输入图像描述

于 2022-01-28T09:44:34.623 回答