text
stringlengths 1
474
|
|---|
hierarchy by way of callbacks, while current state flows
|
“down” to the stateless widgets that do presentation.
|
The common parent that redirects this flow is the State.
|
The following slightly more complex example shows how
|
this works in practice:
|
<code_start>import 'package:flutter/material.dart';
|
class CounterDisplay extends StatelessWidget {
|
const CounterDisplay({required this.count, super.key});
|
final int count;
|
@override
|
Widget build(BuildContext context) {
|
return Text('Count: $count');
|
}
|
}
|
class CounterIncrementor extends StatelessWidget {
|
const CounterIncrementor({required this.onPressed, super.key});
|
final VoidCallback onPressed;
|
@override
|
Widget build(BuildContext context) {
|
return ElevatedButton(
|
onPressed: onPressed,
|
child: const Text('Increment'),
|
);
|
}
|
}
|
class Counter extends StatefulWidget {
|
const Counter({super.key});
|
@override
|
State<Counter> createState() => _CounterState();
|
}
|
class _CounterState extends State<Counter> {
|
int _counter = 0;
|
void _increment() {
|
setState(() {
|
++_counter;
|
});
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Row(
|
mainAxisAlignment: MainAxisAlignment.center,
|
children: <Widget>[
|
CounterIncrementor(onPressed: _increment),
|
const SizedBox(width: 16),
|
CounterDisplay(count: _counter),
|
],
|
);
|
}
|
}
|
void main() {
|
runApp(
|
const MaterialApp(
|
home: Scaffold(
|
body: Center(
|
child: Counter(),
|
),
|
),
|
),
|
);
|
}<code_end>
|
Notice the creation of two new stateless widgets,
|
cleanly separating the concerns of displaying the counter
|
(CounterDisplay) and changing the counter (CounterIncrementor).
|
Although the net result is the same as the previous example,
|
the separation of responsibility allows greater complexity to
|
be encapsulated in the individual widgets,
|
while maintaining simplicity in the parent.For more information, check out:<topic_end>
|
<topic_start>
|
Bringing it all together
|
What follows is a more complete example that brings together
|
these concepts: A hypothetical shopping application displays various
|
products offered for sale, and maintains a shopping cart for
|
intended purchases. Start by defining the presentation class,
|
ShoppingListItem:
|
<code_start>import 'package:flutter/material.dart';
|
class Product {
|
const Product({required this.name});
|
final String name;
|
}
|
typedef CartChangedCallback = Function(Product product, bool inCart);
|
class ShoppingListItem extends StatelessWidget {
|
ShoppingListItem({
|
required this.product,
|
required this.inCart,
|
required this.onCartChanged,
|
}) : super(key: ObjectKey(product));
|
final Product product;
|
final bool inCart;
|
final CartChangedCallback onCartChanged;
|
Color _getColor(BuildContext context) {
|
// The theme depends on the BuildContext because different
|
// parts of the tree can have different themes.
|
// The BuildContext indicates where the build is
|
// taking place and therefore which theme to use.
|
return inCart //
|
? Colors.black54
|
: Theme.of(context).primaryColor;
|
}
|
TextStyle? _getTextStyle(BuildContext context) {
|
if (!inCart) return null;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.