Flutter 将ListView.separated过滤器芯片包裹在颤振中
我正在制作一个使用过滤芯片的过滤屏幕,我试图将这些芯片包装成多行,以避免剪切/水平滚动,但我无法实现这一点。我试图将Wrap()类放在返回中,但它们只是堆叠在一起,而不是均匀分布 这是我当前的代码:Flutter 将ListView.separated过滤器芯片包裹在颤振中,flutter,dart,Flutter,Dart,我正在制作一个使用过滤芯片的过滤屏幕,我试图将这些芯片包装成多行,以避免剪切/水平滚动,但我无法实现这一点。我试图将Wrap()类放在返回中,但它们只是堆叠在一起,而不是均匀分布 这是我当前的代码: import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class Screen1 extends StatefulWidget { @override _Screen1State c
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class Screen1 extends StatefulWidget {
@override
_Screen1State createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
var data = ['Chip 1', 'Chip 2', 'Chip 3', 'Chip 4', 'Chip 5'];
var selected = [];
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.black87,
),
leading: IconButton(
icon: Icon(Icons.close),
),
centerTitle: true,
title: Text(
'Screen',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.bold,
),
),
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
padding: EdgeInsets.all(15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: Row(
children: <Widget>[
Text(
"Label 1",
),
],
),
),
SizedBox(height: size.height * 0.02),
Container(
height: size.height * 0.05,
child: ListView.separated(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: data.length,
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
width: size.width * 0.03,
);
},
itemBuilder: (BuildContext context, int index) => FilterChip(
padding: EdgeInsets.all(10),
label: Text(
data[index],
style: TextStyle(fontSize: 20),
),
onSelected: (bool value) {
if (selected.contains(index)) {
selected.remove(index);
} else {
selected.add(index);
}
setState(() {});
},
selected: selected.contains(index),
selectedColor: Colors.deepOrange,
labelStyle: TextStyle(
color: Colors.white,
),
backgroundColor: Colors.green,
),
),
),
],
),
),
);
}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
类Screen1扩展了StatefulWidget{
@凌驾
_Screen1State createState()=>\u Screen1State();
}
类_Screen1State扩展状态{
var数据=[‘芯片1’、‘芯片2’、‘芯片3’、‘芯片4’、‘芯片5’];
所选var=[];
@凌驾
小部件构建(构建上下文){
Size Size=MediaQuery.of(context).Size;
返回脚手架(
appBar:appBar(
iconTheme:IconThemeData(
颜色:颜色。黑色87,
),
领先:IconButton(
图标:图标(Icons.close),
),
标题:对,
标题:正文(
“屏幕”,
样式:TextStyle(
颜色:颜色。黑色87,
fontWeight:fontWeight.bold,
),
),
背景颜色:Colors.green,
),
正文:SingleChildScrollView(
填充:边缘设置。全部(15),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
居中(
孩子:排(
儿童:[
正文(
“标签1”,
),
],
),
),
SizedBox(高度:size.height*0.02),
容器(
高度:size.height*0.05,
子项:ListView.separated(
滚动方向:轴水平,
收缩膜:对,
itemCount:data.length,
separatorBuilder:(BuildContext,int索引){
返回大小框(
宽度:size.width*0.03,
);
},
itemBuilder:(BuildContext上下文,int索引)=>FilterChip(
填充:边缘设置。全部(10),
标签:文本(
数据[索引],
样式:TextStyle(字体大小:20),
),
onSelected:(布尔值){
如果(选中。包含(索引)){
选中。删除(索引);
}否则{
选中。添加(索引);
}
setState((){});
},
已选:已选。包含(索引),
selectedColor:Colors.deepOrange,
标签样式:文本样式(
颜色:颜色,白色,
),
背景颜色:Colors.green,
),
),
),
],
),
),
);
}
}
您需要使用过滤芯片的小部件包装 请看教程