极简起步
import 'package:flutter/material.dart';
void main() {
runApp(Text("hello world",
textAlign: TextAlign.center,));
//textAlign: TextAlign.center是不能少的,少了会报错
}
提取组件
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
//textAlign: TextAlign.center是不能少的,少了会报错
}
//分离出MyApp
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
"hello6",
textAlign: TextAlign.center,);
}
}
分离文件
提取出myApp.dart
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
"hello6",
textAlign: TextAlign.center,);
}
}
main.dart文件内容
import 'package:flutter/material.dart';
import 'package:myapp/myApp.dart';
void main() {
runApp(MyApp());
}
引入Center
myApp.dart文件内容
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text(
"hello",
textAlign: TextAlign.center,
textDirection: TextDirection.rtl,
),
);
}
}
引入scaffold
引入scaffold同时必须引入MaterialApp,否则会报错
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(
"hello",
textAlign: TextAlign.center,
textDirection: TextDirection.rtl,
),
)));
}
}
操作中存在的问题
编写极简示例的过程中,一些代码变化不能成功热更新,这个时候可以重新启动
修改主文件以外的文件造成的代码变化,不能触发热更新,需要ctr+F5,引发热更新,如果热更新不能体现代码变化,尝试重新启动