设置状态/更新数据颤动



所以我有一个页面,里面有一个appbar和一个body(gridview(。在我的应用程序栏中,我有一个按钮,该按钮会向我显示showModalBottomSheet,我还有一个按钮来保存所选项目。示例:

在此处输入图像描述

当我点击按钮"时,我需要刷新我的网格视图;Guardar";,所以我必须将从showModalBottomSheet中选择的da值传递给gridview。我想我会那样做,但不知道如何刷新屏幕。

ty

网格视图

class PostsList extends StatefulWidget {
String num;
int startIndex;
int postLimit;
String atrib;
PostsList(
{Key? key,
required this.num,
required this.startIndex,
required this.postLimit,
required this.atrib})
: super(key: key);
@override
State<PostsList> createState() =>
_PostsListState(num, startIndex, postLimit, atrib);
}
class _PostsListState extends State<PostsList> {
String num;
int startIndex;
int postLimit;
String atrib;
_PostsListState(this.num, this.startIndex, this.postLimit, this.atrib);
final _scrollController = ScrollController();
@override
void initState() {
super.initState();
_scrollController.addListener(_onScroll);
}

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => PostBloc(RepositoryProvider.of<PostRepository>(context),
num, startIndex, postLimit, atrib)
..add(PostFetched()),
child: BlocBuilder<PostBloc, PostState>(
builder: (context, state) {
switch (state.status) {
case PostStatus.failure:
return const Center(child: Text('Falha ao carregar produtos'));
case PostStatus.success:
if (state.posts.isEmpty) {
return const Center(child: Text('Lista de produtos vazia'));
}
return Column(
children: [
Expanded(
child: Container(
padding: const EdgeInsets.fromLTRB(3, 0, 0, 0),
color: Colors.grey[350],
child: GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
childAspectRatio: 0.55),
itemCount: state.hasReachedMax
? state.posts.length
: state.posts.length + 2,
controller: _scrollController,
itemBuilder: (BuildContext context, int index) {
if (state.posts[index].quantity > 0) {
return index >= state.posts.length
? const BottomLoader()
: PostListItemdisp(post: state.posts[index]);
} else {
return index >= state.posts.length
? const BottomLoader()
: PostListItemIndisp(post: state.posts[index]);
}
},
),
),
),
],
);
case PostStatus.initial:
return const Center(child: CircularProgressIndicator());
}
},
),
);
}
@override
void dispose() {
_scrollController
..removeListener(_onScroll)
..dispose();
super.dispose();
}
void _onScroll() {
if (_isBottom) context.read<PostBloc>().add(PostFetched());
}
bool get _isBottom {
if (!_scrollController.hasClients) return false;
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.offset;
return currentScroll >= (maxScroll * 0.8);
}
} 

底板

const List<Widget> opcoes = <Widget>[
Text('Referencia'),
Text('Preço'),
Text('Nome'),
Text('Quantidade'),
];
class teste extends StatefulWidget {
String num;
int startIndex;
int postLimit;
teste({
Key? key,
required this.num,
required this.startIndex,
required this.postLimit,
}) : super(key: key);
@override
State<teste> createState() => _testeState(num, startIndex, postLimit);
}
class _testeState extends State<teste> {
String num;
int startIndex;
int postLimit;
_testeState(this.num, this.startIndex, this.postLimit);
final List<bool> _selectedOpcoes = <bool>[true, false, false, false];
final List<String> _selectedTexto = <String>[
'Reference',
'Price',
'Name',
'Quantity'
];
bool vertical = false;
String finalTexto = 'Reference';
@override
Widget build(BuildContext context) {
return ListView(
children: [
Padding(
padding: const EdgeInsets.only(
bottom: 10,
left: 270,
right: 10,
top: 10,
),
child: ElevatedButton(
onPressed: () {
PostsList(
num: num,
startIndex: startIndex,
postLimit: postLimit,
atrib: finalTexto,
);
},
child: const Text('Guardar', style: TextStyle(fontSize: 18)),
),
),
const SizedBox(
height: 70,
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text(
'Ordenar por:',
style: TextStyle(
fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),
),
),
const SizedBox(
height: 10,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: ToggleButtons(
direction: vertical ? Axis.vertical : Axis.horizontal,
onPressed: (int index) {
setState(() {
for (int i = 0; i < _selectedOpcoes.length; i++) {
_selectedOpcoes[i] = i == index;
if (_selectedOpcoes[i] == true) {
finalTexto = _selectedTexto[i];
}
}
});
},
borderRadius: const BorderRadius.all(Radius.circular(8)),
selectedBorderColor: primeColor,
selectedColor: primeColor,
fillColor: Colors.white,
color: Colors.black,
constraints: const BoxConstraints(
minHeight: 40.0,
minWidth: 80.0,
),
isSelected: _selectedOpcoes,
children: opcoes,
),
),
],
);
}
}

页面后

class PostsPage extends StatelessWidget {
String num;
String nome;
String atrib;
int startIndex;
int postLimit;
PostsPage({Key? key, required this.num, required this.nome, required this.atrib, required this.postLimit, required this.startIndex})
: super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => PostBloc(RepositoryProvider.of<PostRepository>(context),
num, startIndex, postLimit, atrib)
..add(PostFetched()),
child: Scaffold(
appBar: AppBar(
title: Text(nome),
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: const Icon(Icons.arrow_back_ios),
),
actions: [
Padding(
padding: const EdgeInsets.all(10.0),
child: FloatingActionButton.extended(
onPressed: () {
showModalBottomSheet(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
)),
context: context,
builder: (context) {
return SizedBox(
height: 250,
child: BuildSheet(
num: num,
startIndex: startIndex,
postLimit: postLimit,
),
);
},
);
},
label: const Text(
'Filtros',
style: TextStyle(
color: Colors.blue, fontWeight: FontWeight.bold),
),
backgroundColor: Colors.white,
icon: const Icon(
Icons.arrow_drop_down_sharp,
size: 30,
color: Colors.blue,
),
),
),
],
),
body: PostsList(
startIndex: startIndex,
postLimit: postLimit,
num: num,
atrib: atrib,
),
),
);
}
}

Rafael,您可以为此使用回调函数:

在您的帖子列表中:

updateGrid(){
setState(() => {

});
}

///点击FILTROS按钮时:

MyBottomSheetWidget(updateGrid: updateGrid);

在您的BottomSheetWidget中:

String num;
int startIndex;
int postLimit;
final Function() updateGrid;
teste({
Key? key,
required this.num,
required this.startIndex,
required this.postLimit,
required this.updateGrid,
}) : super(key: key);

///单击GUARDAR时

widget.updateGrid();

所以。。。当Guardar按钮执行操作时,函数updateGrid将在PostList小部件内调用。

最新更新