componentt 0.1.0
componentt: ^0.1.0 copied to clipboard
A library for creating fully functional visual components.
import 'package:componentt/componentt.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class IncrementIntent extends Intent {}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends Component<MyHomePage> {
final _maxValue = 15;
final _counter = ValueNotifier(0);
late final ComponentAction<IncrementIntent, void> _incrementAction;
@override
void initState() {
super.initState();
_incrementAction = _onIncrement.toAction().attachTo(this);
}
void _onIncrement(IncrementIntent intent, [BuildContext? context]) {
_counter.value += 1;
if (_counter.value == _maxValue) {
_incrementAction.isActionEnabled = false;
if (context != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('You reach count limit equal to $_maxValue'),
),
);
}
}
}
@override
Widget build(BuildContext context) {
return withActions(
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text('Button become disabled when counter reach $_maxValue.'),
ValueListenableBuilder(
valueListenable: _counter,
builder: (context, value, child) {
return Text(
'$value',
style: Theme.of(context).textTheme.headline4,
);
}),
],
),
),
floatingActionButton: Builder(
builder: (context) {
return FloatingActionButton(
onPressed: context.handler(IncrementIntent()),
tooltip: 'Increment',
child: const Icon(Icons.add),
);
},
), // This trailing comma makes auto-formatting nicer for build methods.
),
);
}
}