Skip to content

Instantly share code, notes, and snippets.

@riskers
Last active January 12, 2019 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riskers/fb174083be0064aaa1c232e468ee8ed9 to your computer and use it in GitHub Desktop.
Save riskers/fb174083be0064aaa1c232e468ee8ed9 to your computer and use it in GitHub Desktop.
flutter 组件化
import 'package:flutter/material.dart';
Widget helloRect() {
return Container(
color: Colors.purple,
width: 200,
height: 200,
margin: EdgeInsets.all(16),
child: Center(child: Text('Hi')),
);
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Startup Name Generator',
home: Scaffold(
body: Center(
child: helloRect(),
)
)
);
}
}
class TextForm extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _TextForm();
}
}
class _TextForm extends State<TextForm> {
final TextEditingController editController = TextEditingController();
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: TextField(
controller: editController,
),
),
RaisedButton(
child: Text('click'),
onPressed: () {
print('text: ${editController.text}');
},
)
],
);
}
}
import 'package:flutter/material.dart';
class HelloRect extends StatelessWidget {
final String text;
HelloRect({this.text});
@override
Widget build(BuildContext context) {
return Container(
color: Colors.purple,
width: 200,
height: 200,
margin: EdgeInsets.all(16),
child: Center(child: Text(text)),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Startup Name Generator',
home: Scaffold(
body: Center(
child: HelloRect(text: 'Hi')
)
)
);
}
}
import 'package:flutter/material.dart';
class HelloRect extends StatelessWidget {
String text;
Color background;
num w, h;
HelloRect({
@required this.text, // 必传参数
this.background = Colors.purple, // 参数默认值为 Colors.purple
this.w = 200.0,
this.h = 200.0,
});
@override
Widget build(BuildContext context) {
return Container(
color: this.background,
width: this.w,
height: this.h,
margin: EdgeInsets.all(16),
child: Center(child: Text(this.text)),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Startup Name Generator',
home: Scaffold(
body: Center(
child: HelloRect( // 以下几个参数均为命名参数
text: 'Hi',
background: Colors.green,
w: 150.0,
h: 100.0,
)
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment