text
stringlengths 1
372
|
|---|
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;
|
return const TextStyle(
|
color: colors.black54,
|
decoration: TextDecoration.lineThrough,
|
);
|
}
|
@override
|
widget build(BuildContext context) {
|
return ListTile(
|
onTap: () {
|
onCartChanged(product, inCart);
|
},
|
leading: CircleAvatar(
|
backgroundColor: _getColor(context),
|
child: text(product.name[0]),
|
),
|
title: text(product.name, style: _getTextStyle(context)),
|
);
|
}
|
}
|
void main() {
|
runApp(
|
MaterialApp(
|
home: scaffold(
|
body: center(
|
child: ShoppingListItem(
|
product: const product(name: 'chips'),
|
inCart: true,
|
onCartChanged: (product, inCart) {},
|
),
|
),
|
),
|
),
|
);
|
}
|
<code_end>
|
the ShoppingListItem widget follows a common pattern
|
for stateless widgets. it stores the values it receives
|
in its constructor in final member variables,
|
which it then uses during its build() function.
|
for example, the inCart boolean toggles between two visual
|
appearances: one that uses the primary color from the current
|
theme, and another that uses gray.
|
when the user taps the list item, the widget doesn’t modify
|
its inCart value directly. instead, the widget calls the
|
onCartChanged function it received from its parent widget.
|
this pattern lets you store state higher in the widget
|
hierarchy, which causes the state to persist for longer periods of time.
|
in the extreme, the state stored on the widget passed to
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.