So i have one page with a appbar and a body (gridview). In my appbar i have one button, that button show me a showModalBottomSheet and there i have one button to save the selected item. Exemple:
I need refresh my gridview when i click the button "Guardar", so i have to pass da value selected from showModalBottomSheet to gridview. i think i do that but dont kwon how i refresh the screen.
ty
Gridview
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);
}
}
BOTTOMSHEET
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,
),
),
],
);
}
}
Rafael, you can use a callback function for this:
In your PostsList:
updateGrid(){
setState(() => {
});
}
/// When button FILTROS is clicked:
MyBottomSheetWidget(updateGrid: updateGrid);
And in your 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);
/// when GUARDAR is clicked
widget.updateGrid();
So... when the Guardar button perform an action the function updateGrid will be called inside PostList widget.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.