docs(main.dart, basic_provider_page.dart): 更新注释和格式

在 main.dart 文件中添加了对 Flutter 应用程序结构的详细注释,增强代码可读性。
在 basic_provider_page.dart 文件中调整了注释格式,确保一致性和清晰性。
This commit is contained in:
2025-05-16 16:32:17 +08:00
parent 68259775e7
commit 8df8e4f840
2 changed files with 49 additions and 24 deletions

View File

@ -16,7 +16,9 @@ class UserInfo extends ChangeNotifier {
_email = email;
String get name => _name;
int get age => _age;
String get email => _email;
void updateName(String newName) {
@ -47,6 +49,7 @@ class AppConfig extends ChangeNotifier {
_language = language;
String get theme => _theme;
String get language => _language;
void toggleTheme() {
@ -88,9 +91,7 @@ class BasicProviderPage extends StatelessWidget {
),
],
child: Scaffold(
appBar: AppBar(
title: const Text('Basic Provider')
),
appBar: AppBar(title: const Text('Basic Provider')),
body: const SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(16.0),
@ -129,8 +130,7 @@ class UpdateSection extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Update Examples:',
style: TextStyle(fontWeight: FontWeight.bold)),
const Text('Update Examples:', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 10),
// 用户信息更新按钮
@ -140,10 +140,7 @@ class UpdateSection extends StatelessWidget {
children: [
ElevatedButton(
onPressed: () {
context.read<UserInfo>().updateName(
context.read<UserInfo>().name == 'John Doe'
? 'Jane Doe'
: 'John Doe');
context.read<UserInfo>().updateName(context.read<UserInfo>().name == 'John Doe' ? 'Jane Doe' : 'John Doe');
},
child: const Text('Toggle Name'),
),
@ -154,10 +151,9 @@ class UpdateSection extends StatelessWidget {
ElevatedButton(
onPressed: () {
final currentEmail = context.read<UserInfo>().email;
context.read<UserInfo>().updateEmail(
currentEmail == 'john@example.com'
? 'jane@example.com'
: 'john@example.com');
context
.read<UserInfo>()
.updateEmail(currentEmail == 'john@example.com' ? 'jane@example.com' : 'john@example.com');
},
child: const Text('Toggle Email'),
),
@ -226,8 +222,7 @@ class WatchExample extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Watch Example:',
style: TextStyle(fontWeight: FontWeight.bold)),
const Text('Watch Example:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('Message: $message'),
Text('User: ${userInfo.name}, Age: ${userInfo.age}'),
],
@ -251,8 +246,7 @@ class ReadExample extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Read Example: ${userInfo.name}',
style: TextStyle(fontWeight: FontWeight.bold)),
Text('Read Example: ${userInfo.name}', style: TextStyle(fontWeight: FontWeight.bold)),
ElevatedButton(
onPressed: () {
final userInfo = context.read<UserInfo>();
@ -284,8 +278,7 @@ class SelectExample extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Select Example:',
style: TextStyle(fontWeight: FontWeight.bold)),
const Text('Select Example:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('Language: $language'),
Text('Theme: $theme'),
],
@ -298,6 +291,7 @@ class SelectExample extends StatelessWidget {
// 使用 Consumer 方式访问数据
class ConsumerExample extends StatelessWidget {
const ConsumerExample({super.key});
@override
Widget build(BuildContext context) {
return Card(
@ -306,8 +300,7 @@ class ConsumerExample extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Consumer Example:',
style: TextStyle(fontWeight: FontWeight.bold)),
const Text('Consumer Example:', style: TextStyle(fontWeight: FontWeight.bold)),
Consumer<UserInfo?>(
builder: (context, userInfo, child) {
if (userInfo == null) {
@ -330,7 +323,6 @@ class ConsumerExample extends StatelessWidget {
}
}
// 使用 Provider.of 方式访问数据
class OfExample extends StatelessWidget {
const OfExample({super.key});
@ -348,8 +340,7 @@ class OfExample extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Provider.of Example:',
style: TextStyle(fontWeight: FontWeight.bold)),
const Text('Provider.of Example:', style: TextStyle(fontWeight: FontWeight.bold)),
Text('Theme: ${appConfig.theme}'),
Text('User: ${userInfo.name}'),
],