unleash_proxy_client_flutter 1.1.0
unleash_proxy_client_flutter: ^1.1.0 copied to clipboard
A Flutter/Dart client that can be used together with the unleash-proxy.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:unleash_proxy_client_flutter/unleash_proxy_client_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Unleash Integration Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Unleash Integration Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
bool _counterEnabled = false;
@override
void initState() {
super.initState();
var unleash = UnleashClient(
url: Uri.parse('https://app.unleash-hosted.com/demo/api/proxy'),
clientKey: 'proxy-123',
refreshInterval: 30,
appName: 'example-flutter-app');
void updateCounterEnabled(_) {
final counterEnabled = unleash.isEnabled('counter');
setState(() {
_counterEnabled = counterEnabled;
});
}
unleash.on('ready', updateCounterEnabled);
unleash.on('update', updateCounterEnabled);
unleash.start();
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('${widget.title} (counter is ${_counterEnabled ? 'enabled' : 'disabled'})'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _counterEnabled ? _incrementCounter : null,
tooltip: 'Increment',
child: _counterEnabled ? const Icon(Icons.add) : const Icon(Icons.disabled_by_default),
),
);
}
}