使用 Flutter 的基本布局,可用于显示用户个人资料并,编辑个人资料:
实现代码:
import 'package:flutter/material.dart';
class UserProfile extends StatefulWidget {
_UserProfileState createState() => _UserProfileState();
}
class _UserProfileState extends State<UserProfile> {
// Define your variables here
String name = "John Doe";
String email = "johndoe@example.com";
String imageUrl = "https://via.placeholder.com/150";
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User Profile"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(imageUrl),
),
SizedBox(height: 16.0),
Text(
name,
style: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8.0),
Text(
email,
style: TextStyle(
fontSize: 16.0,
),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
// Navigate to Edit Profile screen
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EditProfile(
name: name,
email: email,
imageUrl: imageUrl,
),
),
);
},
child: Text("Edit Profile"),
),
],
),
),
);
}
}
class EditProfile extends StatefulWidget {
final String name;
final String email;
final String imageUrl;
EditProfile({this.name, this.email, this.imageUrl});
_EditProfileState createState() => _EditProfileState();
}
class _EditProfileState extends State<EditProfile> {
TextEditingController nameController;
TextEditingController emailController;
TextEditingController imageUrlController;
void initState() {
super.initState();
nameController = TextEditingController(text: widget.name);
emailController = TextEditingController(text: widget.email);
imageUrlController = TextEditingController(text: widget.imageUrl);
}
void dispose() {
nameController.dispose();
emailController.dispose();
imageUrlController.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Edit Profile"),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(imageUrlController.text),
),
SizedBox(height: 16.0),
TextField(
controller: nameController,
decoration: InputDecoration(
labelText: "Name",
),
),
SizedBox(height: 8.0),
TextField(
controller: emailController,
decoration: InputDecoration(
labelText: "Email",
),
),
SizedBox(height: 8.0),
TextField(
controller: imageUrlController,
decoration: InputDecoration(
labelText: "Image URL",
),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
// Save changes and navigate back to User Profile screen
Navigator.pop(context);
},
child: Text("Save Changes"),
),
],
),
),
);
}
}
Flutter用户个人页面局包,括两个布局:用户配置文件屏幕和编辑布局。用户信息展示布局。