[flutter] widget 2 : ํ๋ฉด ๋ฐฐ์น๋ฅผ ์ํ ๋ ์ด์์-2
5. SingleChildScrollView
Column์ ์ฌ์ฉํด ์์ ฏ๋ค์ ๋์ดํ๋ค ๋ณด๋ฉด ํ๋ฉด์ ํฌ๊ธฐ๋ฅผ ๋์ด์ค ์ ์๋ค. ์ด๋, ์คํฌ๋กค ๊ธฐ๋ฅ์ด ํ์ํ๋ค.
์ด ์์ ฏ์, ํ๋์ ์์์ ์คํฌ๋กคํ ์ ์๊ฒ ํ๋ ์์ ฏ์ด๋ค.
๋ณดํต column๋ณด๋ค๋ ListBody๋ฅผ ์ฌ์ฉํด ์คํฌ๋กค ์์ ฏ์ ๋ง๋ ๋ค. column์ ์์ ฏ์ ํฌ๊ธฐ๋งํผ ๊ฐ๋ก ๊ธธ์ด๋ฅผ ๊ฐ์ ธ ์คํฌ๋กค ๊ฐ๋ฅ ์์ญ์ด ์ข์ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final items = List.generate(100, (i) => i).toList();
return Scaffold(
appBar: AppBar(
title: Text("hello!"),
),
body: SingleChildScrollView(
child : ListBody(
children : items.map((i) => Text('$i')).toList(),
),
)
);
}
}
6. ListView, ListTile
LiseView : ๋ฆฌ์คํธ๋ฅผ ํ์ํ๋ ์์ ฏ. ์ข ๋ ํจ์จ์ ์ธ SingleChildScrollView + ListBody.
ListTile : ๋ฆฌ์คํธ ์์ดํ ์ ์ฝ๊ฒ ์์ฑํ ์ ์์.
leading(๋งจ ์ผ์ชฝ. ๋ณดํต ์์ด์ฝ), title(์ค์, ๋ณดํต ํ ์คํธ), trailing(๊ผฌ๋ฆฌ, ๋ง์ง๋ง ๋ถ๋ถ), onTap(๋๋ ์ ๋ ์ก์ )์ผ๋ก ๊ตฌ์ฑ๋จ.
ListView์ children ์์ฑ์ ๋ค์์ ListTile ์์ ฏ์ ๋ฐฐ์นํ๋ฉด ์ฝ๊ฒ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค ์ ์์.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello!"),
),
body: ListView(
scrollDirection : Axis.vertical,
children : <Widget> [
ListTile(
leading : Icon(Icons.home),
title : Text("one"),
trailing : Icon(Icons.navigate_next),
onTap: (){},
),
ListTile(
leading : Icon(Icons.access_alarm),
title : Text("two"),
trailing : Icon(Icons.navigate_next),
onTap: (){},
),
ListTile(
leading : Icon(Icons.cake),
title : Text("three"),
trailing : Icon(Icons.navigate_next),
onTap: (){},
),
]
)
);
}
}
7. GridView
์ด ์๋ฅผ ์ง์ ํด ๊ทธ๋ฆฌ๋ ํํ๋ก ํ์ํ๋ ์์ ฏ.
crossAxisCount์ ์ด ์๋ฅผ ์ง์ ํ ์ ์๋ค.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello!"),
),
body: GridView.count(
crossAxisCount : 3,//grid ์
children : <Widget> [
Container(
color : Colors.red,
width : 100,
height : 100,
),
Container(
color : Colors.blueGrey,
width : 100,
height : 100,
),
Container(
color : Colors.lime,
width : 100,
height : 100,
),
])
);
}
}
8. PageView
ํ์ด์ง๋ฅผ ์ข์ฐ๋ก ์ฌ๋ผ์ด๋ํด์ ๋๊ธธ ์ ์๊ฒ ํ๋ ์์ ฏ.
children ์์ฑ์ ๋ฐฐ์น๋๋ ์์ ฏ ํ๋ํ๋๊ฐ ํ์ด์ง๊ฐ ๋๋ค. ๊ทธ๋ฌ๋ ๋จ๋ ์ผ๋ก๋ ์ ์ฌ์ฉํ์ง ์๋ ์ถ์ธ.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello!"),
),
body: PageView(children: <Widget>[
Container(
color: Colors.red,
width: 100,
height: 100,
),
Container(
color: Colors.blueGrey,
width: 100,
height: 100,
),
Container(
color: Colors.lime,
width: 100,
height: 100,
),
]));
}
}
9. AppBar, TabBar, Tab, TabBarView
์ด ์์ ฏ๋ค์ ์กฐํฉํด ํ์ด์ง์ ํญ์ด ์ฐ๋๋๊ฒ ๊ตฌ์ฑํ ์ ์๋ค.
์ด๋, scaffold๋ฅผ DefaultTabController๋ก ๊ฐ์ธ์ผ ํ๋ค๋ ๊ฒ์ ์ ์ํ์.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length : 3, //ํญ ์
child :Scaffold(
appBar: AppBar(
title: Text("hello!"),
bottom: TabBar(
tabs : <Widget>[
Tab(icon : Icon(Icons.home)),
Tab(text: '๋ฉ๋ด 2'),
Tab(icon : Icon(Icons.info), text:'๋ฉ๋ด 3'),
]
)
),
body: TabBarView(
children: <Widget>[
Container(
color: Colors.red,
),
Container(
color: Colors.blueGrey,
),
Container(
color: Colors.lime,
),
]))
);
}
}
10. BottomNavigtionBar
ํ๋จ์ ํญ ๋ฉ๋ด๋ฅผ ๊ตฌ์ฑํ๋ ์์ ฏ. ํ๋ฉด ์ ํ์ ์ฌ์ฉ.
icon๊ณผ label ์์ฑ์ ์ด์ฉํด ํญ ๋ฐ๋ฅผ ๊ตฌ์ฑํ ์ ์๋ค.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("hello!"),
),
bottomNavigationBar: BottomNavigationBar(items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'profile',
),
BottomNavigationBarItem(
icon: Icon(Icons.cake),
label: 'cake',
),
]));
}
}
์ฐธ์กฐ
Flutter ๊ฐ์ข - [List] ListView ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
Flutter ๊ฐ์ข ๋ชฉ๋ก : https://here4you.tistory.com/120 ์ด๋ฒ ๊ฐ์ข์์๋ ListView๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณธ๋ค. Flutter์์์ ListView๋ ListTile๋ค๋ก ๊ตฌ์ฑ๋๋ค. ์์ค์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค. import 'pack..
here4you.tistory.com