1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import 'package:flutter/material.dart';
void main() { runApp(MainPage()); }
class MainPage extends StatefulWidget { const MainPage({super.key});
@override State<MainPage> createState() => _MainPageState(); }
class _MainPageState extends State<MainPage> { List<String> _list = [ "钻石剑", "下届合金剑", "木剑", "石剑", "铁剑", "金剑", "铜剑", "三叉戟", "弓箭", ]; @override Widget build(BuildContext context) { return MaterialApp( title: "终末地AI群聊", home: GridView.count( padding: EdgeInsets.all(10), crossAxisCount: 2, mainAxisSpacing: 10, crossAxisSpacing: 10, children: List.generate(_list.length, (int index) { return Child( weaponname: _list[index], index: index, onDelete: (int i) { setState(() { _list.removeAt(i); }); }, ); }), ), ); } }
class Child extends StatefulWidget { final String weaponname; final int index; final Function(int) onDelete; const Child({ super.key, required this.weaponname, required this.index, required this.onDelete, });
@override State<Child> createState() => _ChildState(); }
class _ChildState extends State<Child> { @override Widget build(BuildContext context) { return Stack( alignment: Alignment.bottomRight, children: [ Container( alignment: Alignment.center, decoration: BoxDecoration( border: Border.all(color: Colors.black, width: 5), color: Colors.blueGrey, borderRadius: BorderRadius.all(Radius.circular(40)), ), child: Text( widget.weaponname, style: TextStyle( fontSize: 20, color: Colors.white, fontWeight: FontWeight.bold, ), ), ), IconButton( onPressed: () { widget.onDelete(widget.index); }, icon: Icon(Icons.delete, color: Colors.cyanAccent, size: 30), ), ], ); } }
|
评论区