如何为 n 个可移动卡构建此视图?
How to build this view for n number of removable cards?
哪个小部件最适合使用?如果 listview.builder 那么你能分享一个示例代码吗? This is the UI.
您可以通过两种方式实现它:
您可以使用 GridView 小部件
喜欢
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text("Flutter GridView Demo"),
),
body: GridView.count(
primary: false,
padding: const EdgeInsets.all(30),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return Center(
child: SelectCard(choice: choices[index]),
);
}))),
));
}
}
class Choice {
const Choice({required this.title, required this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = <Choice>[
Choice(title: 'Home', icon: Icons.home),
Choice(title: 'Contact', icon: Icons.contacts),
Choice(title: 'Map', icon: Icons.map),
Choice(title: 'Phone', icon: Icons.phone),
Choice(title: 'Camera', icon: Icons.camera_alt),
Choice(title: 'Setting', icon: Icons.settings),
Choice(title: 'Album', icon: Icons.photo_album),
Choice(title: 'WiFi', icon: Icons.wifi),
];
class SelectCard extends StatelessWidget {
const SelectCard({Key? key, required this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
return Card(
color: Colors.orange,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(child: Icon(choice.icon, size: 50.0)),
Text(choice.title),
]),
));
}
}
GridView
是您正在寻找的小部件。只需使用 GridView.count
即可获得所需结果
实施GridView.count()
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 2,
children: List.generate(10, (index) => const DemoCard()));
}
这里是完整的例子:
import 'package:flutter/material.dart';
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 2,
children: List.generate(10, (index) => const DemoCard()));
}
}
class DemoCard extends StatelessWidget {
const DemoCard({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8),
width: 200,
height: 200,
child: Card(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))),
elevation: 10,
child: Padding(
padding: const EdgeInsets.all(18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
Icons.developer_mode,
color: Colors.blue,
size: 50,
),
const SizedBox(height: 10),
Text(
'Demo Text',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
const SizedBox(height: 2),
Text(
'Sub-Demo Text',
style: Theme.of(context).textTheme.titleMedium!.copyWith(
fontWeight: FontWeight.w600, color: Colors.lightBlue),
),
const Expanded(child: SizedBox()),
Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: TextButton(
child: const Text('Edit Privilage'),
onPressed: () {},
),
),
),
const Icon(Icons.delete, color: Colors.red),
],
),
],
),
),
),
);
}
}
结果将是:
哪个小部件最适合使用?如果 listview.builder 那么你能分享一个示例代码吗? This is the UI.
您可以通过两种方式实现它:
您可以使用 GridView 小部件
喜欢
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text("Flutter GridView Demo"),
),
body: GridView.count(
primary: false,
padding: const EdgeInsets.all(30),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return Center(
child: SelectCard(choice: choices[index]),
);
}))),
));
}
}
class Choice {
const Choice({required this.title, required this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = <Choice>[
Choice(title: 'Home', icon: Icons.home),
Choice(title: 'Contact', icon: Icons.contacts),
Choice(title: 'Map', icon: Icons.map),
Choice(title: 'Phone', icon: Icons.phone),
Choice(title: 'Camera', icon: Icons.camera_alt),
Choice(title: 'Setting', icon: Icons.settings),
Choice(title: 'Album', icon: Icons.photo_album),
Choice(title: 'WiFi', icon: Icons.wifi),
];
class SelectCard extends StatelessWidget {
const SelectCard({Key? key, required this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
return Card(
color: Colors.orange,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(child: Icon(choice.icon, size: 50.0)),
Text(choice.title),
]),
));
}
}
GridView
是您正在寻找的小部件。只需使用 GridView.count
即可获得所需结果
实施GridView.count()
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 2,
children: List.generate(10, (index) => const DemoCard()));
}
这里是完整的例子:
import 'package:flutter/material.dart';
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisCount: 2,
children: List.generate(10, (index) => const DemoCard()));
}
}
class DemoCard extends StatelessWidget {
const DemoCard({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8),
width: 200,
height: 200,
child: Card(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))),
elevation: 10,
child: Padding(
padding: const EdgeInsets.all(18.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Icon(
Icons.developer_mode,
color: Colors.blue,
size: 50,
),
const SizedBox(height: 10),
Text(
'Demo Text',
style: Theme.of(context)
.textTheme
.titleMedium!
.copyWith(fontWeight: FontWeight.bold),
),
const SizedBox(height: 2),
Text(
'Sub-Demo Text',
style: Theme.of(context).textTheme.titleMedium!.copyWith(
fontWeight: FontWeight.w600, color: Colors.lightBlue),
),
const Expanded(child: SizedBox()),
Row(
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: TextButton(
child: const Text('Edit Privilage'),
onPressed: () {},
),
),
),
const Icon(Icons.delete, color: Colors.red),
],
),
],
),
),
),
);
}
}
结果将是: