Line data Source code
1 : library apptive_grid_grid_builder; 2 : 3 : import 'package:apptive_grid_core/apptive_grid_core.dart'; 4 : import 'package:apptive_grid_core/apptive_grid_model.dart'; 5 : import 'package:flutter/material.dart'; 6 : 7 : export 'package:apptive_grid_core/apptive_grid_core.dart'; 8 : 9 : /// Builder for building Widgets that use [Grid] as the Data Source 10 : class ApptiveGridGridBuilder extends StatefulWidget { 11 : /// Creates a Builder Widet 12 1 : const ApptiveGridGridBuilder({ 13 : Key? key, 14 : required this.gridUri, 15 : this.initialData, 16 : required this.builder, 17 1 : }) : super(key: key); 18 : 19 : /// GridUri of the grid that should be used 20 : final GridUri gridUri; 21 : 22 : /// Initial [Grid] data that should be shown 23 : final Grid? initialData; 24 : 25 : /// Callback that is used to build the widget 26 : final Widget Function(BuildContext, AsyncSnapshot<Grid?>) builder; 27 : 28 1 : @override 29 1 : ApptiveGridGridBuilderState createState() => ApptiveGridGridBuilderState(); 30 : } 31 : 32 : /// State of a [ApptiveGridGridBuilder] Widget 33 : class ApptiveGridGridBuilderState extends State<ApptiveGridGridBuilder> { 34 : late AsyncSnapshot<Grid?> _snapshot; 35 : 36 1 : @override 37 : void initState() { 38 1 : _snapshot = 39 3 : AsyncSnapshot<Grid?>.withData(ConnectionState.none, widget.initialData); 40 1 : super.initState(); 41 : } 42 : 43 1 : @override 44 : void didChangeDependencies() { 45 1 : super.didChangeDependencies(); 46 1 : reload(listen: true); 47 : } 48 : 49 1 : @override 50 : Widget build(BuildContext context) { 51 3 : return widget.builder(context, _snapshot); 52 : } 53 : 54 : /// Call this to reload the data. 55 : /// 56 : /// For example used when doing pull to refresh 57 1 : Future<void> reload({ 58 : bool listen = false, 59 : }) { 60 2 : return ApptiveGrid.getClient(context, listen: listen) 61 3 : .loadGrid(gridUri: widget.gridUri) 62 1 : .then( 63 3 : (value) => setState(() { 64 2 : _snapshot = AsyncSnapshot.withData(ConnectionState.done, value); 65 : }), 66 : ) 67 1 : .catchError( 68 3 : (error) => setState(() { 69 2 : _snapshot = AsyncSnapshot.withError(ConnectionState.none, error); 70 : }), 71 : ); 72 : } 73 : }