
About design system support
플러터 SDK에는 미리 만들어진 디자인 관련 컴포넌트가 존재한다.Material과Cupertino다.
플러터 커뮤니티인 pub.dev에서 더 많은 디자인 패키지를 제공한다.
(ex. Fluent UI, macOS UI 등)
int count = 0;
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
setState(() {
count += 1;
});
},
child: const Text('Enabled'),
);
}
많이 쓰이는 버튼 위젯은 아래와 같다.
style, callback, child가 가장 많이 쓰이는 속성들이다.
Widget build(BuildContext context) {
return const SelectableText('''
Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes''');
}
Widget build(BuildContext context) {
return RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
);
}
Text는 문구 highlight를 할 수 없다. 하고 싶으면 SelectableText를 사용하면 된다.
하나의 문장에서 특정 부분에만 style을 주고 싶으면 RichText를 사용하면 된다.
final TextEditingController _controller = TextEditingController();
Widget build(BuildContext context) {
return TextField(
controller: _controller,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Mascot Name',
),
);
}
InputDecoration, controller, onChanged, onSubmitted가 가장 많이 쓰이는 속성이다.
obscureText로 텍스트를 안 보이게 처리할 수도 있고, readOnly로 읽기만 가능하도록 만들 수도 있다.
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your email',
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// Process data.
}
},
child: const Text('Submit'),
),
),
],
),
);
}
Form은 form field 위젯들을 하나로 묶는 컨테이너다.
Form에서 사용하는 TextField는 TextFormField다.
Form은 FormState를 제공하는데 여기에 접근하여 save, reset, validate를 할 수 있다.
또한, GlobalKey로 Form을 특정할 수 있다.
이들 모두 ToggleableStateMixin을 기반으로 만들어진 위젯이다.
showDatePicker는 showDialog에서 DatePickerDialog를 호출하는 것과 같다.
그리고 showTimePicker는 showDialog에서 TimePickerDialog를 호출하는 것과 같다.