랜덤하게 색상 만들기
class UniqueColorGenerator {
static Random random = Random();
static Color getColor() {
return Color.fromARGB(
255, random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
}
두 Widget의 색을 랜덤하게 설정 후 Swap 해보기
class MyTile extends StatelessWidget {
MyTile({Key? key}) : super(key: key);
final Color myColor = UniqueColorGenerator.getColor();
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: myColor,
);
}
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> myTiles = [ MyTile(), MyTile() ];
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Row(
children: myTiles,
),
);
}
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> myTiles = [ MyTile(), MyTile() ];
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Row(
children: myTiles,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.swap_calls),
onPressed: () {
setState(() {
myTiles.insert(1, myTiles.removeAt(0));
});
},
),
);
}
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> myTiles = [ MyTile(key: UniqueKey(),), MyTile(key: UniqueKey(),) ];
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Row(
children: myTiles,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.swap_calls),
onPressed: () {
setState(() {
myTiles.insert(1, myTiles.removeAt(0));
});
},
),
);
}
}
class MyTile extends StatefulWidget {
MyTile({Key? key}) : super(key: key);
State<MyTile> createState() => _MyTileState();
}
class _MyTileState extends State<MyTile> {
final Color myColor = UniqueColorGenerator.getColor();
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: myColor,
);
}
}

class _MyHomePageState extends State<MyHomePage> {
List<Widget> myTiles = [ Padding(
key: UniqueKey(),
padding: const EdgeInsets.all(8.0),
child: MyTile(),
), Padding(
key: UniqueKey(),
padding: const EdgeInsets.all(8.0),
child: MyTile(),
) ];
ListView.builder로 뷰를 한번에 여러개 생성하기
class _MyHomePageState extends State<MyHomePage> {
List items = List.generate(20, (i) => i);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (c,i) {
return ListTile(
title: Text('Student ${items[i]}'),
leading: const Icon(Icons.home),
trailing: const Icon(Icons.navigate_next),
);
})
);
}
}

class _MyHomePageState extends State<MyHomePage> {
List items = List.generate(20, (i) => i);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: ReorderableListView.builder(
itemCount: items.length,
itemBuilder: (c,i) {
return ListTile(
key: ValueKey(items[i]),
title: Text('Student ${items[i]}'),
leading: const Icon(Icons.home),
trailing: const Icon(Icons.navigate_next),
);
},
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
items.insert(newIndex, items.removeAt(oldIndex));
});
},
)
);
}
}

class _MyHomePageState extends State<MyHomePage> {
List items = List.generate(20, (i) => i);
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: ReorderableListView.builder(
itemCount: items.length,
itemBuilder: (c,i) {
return Dismissible(
background: Container(color: Colors.green),
key: ValueKey(items[i]),
child: ListTile(
title: Text('Student ${items[i]}'),
leading: const Icon(Icons.home),
trailing: const Icon(Icons.navigate_next),
),
onDismissed: (direction) {
setState(() {
items.removeAt(i);
});
},
);
},
onReorder: (int oldIndex, int newIndex) {
setState(() {
if (oldIndex < newIndex) {
newIndex -= 1;
}
items.insert(newIndex, items.removeAt(oldIndex));
});
},
)
);
}
}
