Spaces:
Sleeping
Sleeping
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_localizations/flutter_localizations.dart'; | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: '中文 Flutter 项目', // 项目标题 | |
| theme: ThemeData( | |
| colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
| useMaterial3: true, | |
| ), | |
| // 配置中文本地化支持 | |
| localizationsDelegates: const [ | |
| GlobalMaterialLocalizations.delegate, | |
| GlobalWidgetsLocalizations.delegate, | |
| GlobalCupertinoLocalizations.delegate, | |
| ], | |
| supportedLocales: const [ | |
| Locale('zh', 'CN'), // 设置支持的语言为中文简体 | |
| ], | |
| home: const MyHomePage(title: 'Flutter 中文示例首页'), // 设置首页 | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| const MyHomePage({super.key, required this.title}); | |
| final String title; | |
| State<MyHomePage> createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| int _counter = 0; // 计数器变量 | |
| void _incrementCounter() { | |
| setState(() { | |
| // 增加计数器并触发 UI 更新 | |
| _counter++; | |
| }); | |
| } | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
| title: Text(widget.title), // 显示标题 | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| const Text( | |
| '你点击按钮的次数如下:', // 中文提示文字 | |
| ), | |
| Text( | |
| '$_counter', // 显示计数值 | |
| style: Theme.of(context).textTheme.headlineMedium, | |
| ), | |
| ], | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: _incrementCounter, | |
| tooltip: '增加计数', // 按钮提示 | |
| child: const Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } | |