Line data Source code
1 : import 'package:apptive_grid_core/apptive_grid_options.dart'; 2 : import 'package:apptive_grid_core/apptive_grid_network.dart'; 3 : import 'package:flutter/widgets.dart'; 4 : import 'package:provider/provider.dart'; 5 : 6 : /// A Base Widget needed to add general ApptiveGrid functionality to a Flutter App 7 : /// 8 : /// This is used to look up things like the [ApptiveGridClient] for other ApptiveGrid Widgets. 9 : /// It uses [Provider] to distribute dependencies to other ApptiveGrid Widgets 10 : class ApptiveGrid extends StatefulWidget { 11 : /// Creates ApptiveGrid 12 1 : const ApptiveGrid({ 13 : Key? key, 14 : this.child, 15 : this.options = const ApptiveGridOptions(), 16 : }) : client = null, 17 1 : super(key: key); 18 : 19 : /// Creates ApptiveGrid with an defined ApptiveGridClient 20 : /// 21 : /// Used testing to Provide a MockedClient 22 1 : @visibleForTesting 23 : const ApptiveGrid.withClient({ 24 : Key? key, 25 : required this.client, 26 : this.child, 27 : this.options = const ApptiveGridOptions(), 28 1 : }) : super(key: key); 29 : 30 : /// Widget that should be wrapped. Normally this is something like [MaterialApp] 31 : final Widget? child; 32 : 33 : /// Configuration options for ApptiveGrid 34 : final ApptiveGridOptions options; 35 : 36 : /// [ApptiveGridClient] to use 37 : /// 38 : /// Used for supplying a Mocked Client for testing 39 : @visibleForTesting 40 : final ApptiveGridClient? client; 41 : 42 1 : @override 43 1 : _ApptiveGridState createState() => _ApptiveGridState(); 44 : 45 : /// Get direct Access to [ApptiveGridClient] 46 : /// 47 : /// uses [Provider] to return the client 48 1 : static ApptiveGridClient getClient( 49 : BuildContext context, { 50 : bool listen = true, 51 : }) { 52 1 : return Provider.of<ApptiveGridClient>(context, listen: listen); 53 : } 54 : } 55 : 56 : class _ApptiveGridState extends State<ApptiveGrid> { 57 : late ApptiveGridClient _client; 58 : 59 1 : @override 60 : void initState() { 61 1 : super.initState(); 62 3 : _client = widget.client ?? 63 1 : ApptiveGridClient( 64 2 : options: widget.options, 65 : ); 66 2 : _client.sendPendingActions(); 67 : } 68 : 69 1 : @override 70 : Widget build(BuildContext context) { 71 1 : return Provider<ApptiveGridClient>.value( 72 1 : value: _client, 73 2 : child: widget.child, 74 : ); 75 : } 76 : 77 1 : @override 78 : void dispose() { 79 2 : _client.dispose(); 80 1 : super.dispose(); 81 : } 82 : }