import 'package:flutter/material.dart';
class User {
final String name;
final int age;
final String email;
User({required this.name, required this.age, required this.email});
}
class UserProfileScreen extends StatelessWidget {
final User user = User(name: 'John Doe', age: 30, email: 'john@example.com');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('User Profile')),
body: Center(
child: Card(
margin: EdgeInsets.all(16),
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Name: ${user.name}'),
Text('Age: ${user.age}'),
Text('Email: ${user.email}'),
],
),
),
),
),
);
}
}
void main() {
runApp(MaterialApp(home: UserProfileScreen()));
}We created a User class with a constructor that uses named parameters. Each parameter is marked required to ensure the caller provides all values. This makes the code clear and safe.
In UserProfileScreen, we create a User object by passing values with parameter names. Then we display these values in the UI using string interpolation inside Text widgets.
This approach helps beginners understand how named parameters work in Dart constructors and how to use them in Flutter widgets.