[Flutter翻译]GSoC ‘21:为Flutter创建一个桌面样本_flutter 桌面模板
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// 此处省略了MaterialApp的其他属性设置
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const HomePage(), // 使用HomePage作为根页面
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample App'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
这个代码示例展示了如何在Flutter中创建一个简单的计数器应用,它遵循了Flutter的桌面应用开发规范。代码中使用了StatefulWidget
和State
来管理应用状态,这是Flutter开发中推荐的做法。同时,代码示例中也展示了如何使用Scaffold
、AppBar
、FloatingActionButton
等常用的组件来构建应用的UI。
评论已关闭