Flutter 2024年最新
import 'package:flutter/widgets.dart'; void main() => runApp();
//main 函数为应用程序的入口 void main() { //runApp(Widget参数),它的功能是启动Flutter应用 runApp(const MyApp()); } //MyApp对象 class MyApp extends StatelessWidget { const MyApp({super.key}); // 应用的根组件 @override Widget build(BuildContext context) { // 调用自定义的 widget return Echo(text: "hello world"); } } // 自定义 widget class Echo extends StatelessWidget { //使用命名参数,定义 widget 的构造函数 const Echo({ Key? key, //在继承 widget 时,第一个参数通常应该是Key required this.text, //必需要传的参数要添加required关键字 this.backgroundColor = Colors.grey, //默认为灰色 }):super(key:key); // widget 的属性应尽可能的被声明为final,防止被意外改变 final String text; final Color backgroundColor; @override Widget build(BuildContext context) { return Center( child: Container( color: backgroundColor, child: Text(text), //child或children参数通常应被放在参数列表的最后 ), ); }
import 'package:flutter/widgets.dart'; void main() => runApp(const mywidget) class mywidget extends StatelessWidget { const mywidget({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: const Text('Flutter Demo'), ), body: const Center( child: Text('Hello, World!'), )
import 'package:flutter/widgets.dart'; void main() => runApp( const Center( child: Text( 'Hello, world!', key: Key('title'), textDirection: TextDirection.ltr, ), ), );