[Flutter翻译]GSoC ‘21:为Flutter创建一个桌面样本_flutter 桌面模板
这个代码实例是一个简化的Flutter应用程序,它展示了如何使用Flutter构建一个桌面应用程序的基本框架。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Desktop Template',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Desktop Template Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
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),
),
);
}
}
这段代码创建了一个基本的计数器应用程序,展示了如何在Flutter中构建一个带有导航栏、中心部件、浮动动作按钮以及响应用户操作的页面。这是学习Flutter桌面开发的一个很好的起点。
评论已关闭