今日的程式碼 => GITHUB
Flutter 會有三個方式
Extends
的時候,需要使用 super
來繼承父類別的 method.super
@override
void animal() {
// TODO: implement animal
super.animal();
}
class Animal {
void animal() {}
}
class Fly {
String className = "carton";
void fly() {}
}
class Swim {
void swimming() {}
}
class Eat {
void eat() {}
}
class Draink {
void drink() {}
}
class AAA extends Animal with Eat, Draink implements Fly, Swim {
@override
String className = "AAA";
void turnOn() {
super.animal();
_test();
eat();
drink();
}
void _test() {}
@override
void fly() {
// TODO: implement Fly
}
@override
void swimming() {
// TODO: implement Swim
}
}
class MyWidget extends StatelessWidget {
final String title;
const MyWidget({Key? key, this.title = 'abc'}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(title);
}
}
MyWidget(title: 'cc'), // 顯示 cc
MyWidget(), // 顯示 abc
class MyWidget extends StatelessWidget {
final String title;
const MyWidget({Key? key,required this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Text(title);
}
}
MyWidget(title: 'cc'), // 顯示 cc
class MyWidget extends StatelessWidget {
final String _title;
const MyWidget({String title = '3'}) : _title = title;
// const MyWidget({required String title}) : _title = title;
@override
Widget build(BuildContext context) {
return Text(_title);
}
}
class MyWidget extends StatefulWidget {
final String title;
const MyWidget({Key? key, this.title = 'abc'}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return Text(widget.title);
}
}
MyWidget(title: 'cc'), // 顯示 cc
MyWidget(), // 顯示 abc
class MyWidget extends StatefulWidget {
final String title;
const MyWidget({Key? key,required this.title}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return Text(widget.title);
}
}
MyWidget(title: 'cc'), // 顯示 cc
class MyWidget extends StatefulWidget {
final String _title;
const MyWidget({String title = '3'}) : _title = title;
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
Widget build(BuildContext context) {
return Text(widget._title);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: TextView(
callback: (String text) => _showDialog(context, text)),
);
}
void _showDialog(BuildContext context, String text) {
showDialog<void>(
context: context, builder: (context) => AlertDialog(title: Text(text)));
}
}
class TextView extends StatefulWidget {
const TextView({required this.callback});
final void Function(String text) callback;
@override
_TextViewState createState() => _TextViewState();
}
class _TextViewState extends State<TextView> {
final _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(controller: _controller),
IconButton(
onPressed: () async => widget.callback(_controller.text),
icon: Icon(Icons.check),
)
],
);
}
}
我們現在假設有一個情境:
Scaffold
,裡面有一個 showDialog 的 methodfinal void Function(String text) callback;
final Function callback; // 這樣也可以,但是不建議,因為這樣很難 DeBug
const TextView({required this.callback});
onPressed: () async => widget.callback(_controller.text),
TextView(callback: (String text) { },),