Hi.Ihave a widget that shows parent(ParentWd) and child data(ChildWd).When I select/press one parent,child data is loaded and shown.it works but I need to show a CircularProgressIndicator everytime child loads data by provider:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider.value(
value: Child(),
),
],
child: MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: StartPage(),
));
}
}
class StartPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
ParentWd(),//parent
ChildWd(),//child
],
));
}
}
class ParentWd extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(children: <Widget>[
OutlineButton(
child: Text('Parent 1'),
shape: StadiumBorder(),
onPressed: () async {
await Provider.of<Child>(context, listen: false).getData(1);//load child data
},
),
OutlineButton(
child: Text('Parent 2'),
shape: StadiumBorder(),
onPressed: () async {
await Provider.of<Child>(context, listen: false).getData(2);/load child data
},
),
]);
}
}
class ChildWd extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Provider.of<Child>(context, listen: false).getData(1),
builder: (ctx, prevData) {
if (prevData.connectionState == ConnectionState.waiting) {
return Column(
children: <Widget>[
SizedBox(
height: 150,
),
Center(child: CircularProgressIndicator()),//data is shown but not the CircularProgressIndicator
],
);
} else {
if (prevData.error == null) {
return Consumer<Child>(
builder: (ctx, data, child) => GridView(
padding: EdgeInsets.all(2),
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: data.children
.map(
(c) => OutlineButton(
child: Text(c.name),
shape: StadiumBorder(),
onPressed: () {},
),
)
.toList(),
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 80,
childAspectRatio: 3 / 2,
crossAxisSpacing: 5,
mainAxisSpacing: 10,
),
),
);
} else {
return Text('Error');
}
}
},
);
}
}
class Child with ChangeNotifier {
List<ChildItem> _children = [];
List<ChildItem> get children {
return [..._children];
}
Future<void> getData(int idP) async {
Future.delayed(const Duration(seconds: 2), () {//simulate server get data with delay
List<ChildItem> ch = [];
ch.add(ChildItem(id: 1, name: 'Child 1', idParent: 1));
ch.add(ChildItem(id: 2, name: 'Child 2', idParent: 1));
ch.add(ChildItem(id: 3, name: 'Child 3', idParent: 2));
_children = ch.where((c) => c.idParent == idP).toList();
notifyListeners();
});
}
}
class ChildItem {
final int id;
final String name;
final int idParent;
ChildItem({
@required this.id,
@required this.name,
@required this.idParent,
});
}github code thanks
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/d5cd496c-ab8c-4816-a582-85a146930bdc%40googlegroups.com.
| Phương Danh | |||
| Software Engineer | |||
| email: dan...@jobsv.net mobile: +84 349 194 952 | phone: 1900 234554 Dantek JSC , 15 Quang Trung, Đà Nẵng | |||