Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***** home_screen.widget *****/
- class HomeScreen extends ConsumerWidget {
- final _diceProvider = StateProvider<List<DieWidget>>((ref) {
- return [DieWidget(value: 3, min: 1, max: 6)];
- });
- // TODO: Longpress delete from list --> delete from screen
- @override
- Widget build(BuildContext context, ScopedReader watch) {
- final diceWatcher = watch(_diceProvider).state;
- return Container(
- child: Consumer(
- builder: (context, watch, child) {
- return Column(
- children: [
- FlatButton(
- child: Text("Add die"),
- onPressed: () {
- context
- .read(_diceProvider)
- .state
- .add(DieWidget(value: 5, min: 1, max: 6));
- print(diceWatcher[0].value);
- },
- ),
- [..._diceProvider]
- ],
- );
- },
- ),
- );
- }
- }
- /**** die_widget.dart *****/
- class DieWidget extends StateNotifier {
- DieWidget({@required this.value, @required this.min, @required this.max})
- : super(0);
- var value = 3;
- int min = 1;
- int max = 6;
- int id = 12;
- Widget build(BuildContext context) {
- return Container(
- width: 100,
- height: 100,
- child: FlatButton(
- padding: EdgeInsets.all(0),
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- child: Image.asset('assets/dice/die-$value.png', scale: 2.5),
- onPressed: () {
- value = Random().nextInt(9) + 1;
- },
- onLongPress: () {
- print("Long press");
- print("id = " + id.toString());
- // Delete die
- }),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement