Advertisement
tazboy

Untitled

Dec 12th, 2020
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.78 KB | None | 0 0
  1. /***** home_screen.widget *****/
  2. class HomeScreen extends ConsumerWidget {
  3.   final _diceProvider = StateProvider<List<DieWidget>>((ref) {
  4.     return [DieWidget(value: 3, min: 1, max: 6)];
  5.   });
  6.  
  7.   // TODO: Longpress delete from list --> delete from screen
  8.  
  9.   @override
  10.   Widget build(BuildContext context, ScopedReader watch) {
  11.     final diceWatcher = watch(_diceProvider).state;
  12.     return Container(
  13.       child: Consumer(
  14.         builder: (context, watch, child) {
  15.           return Column(
  16.             children: [
  17.               FlatButton(
  18.                 child: Text("Add die"),
  19.                 onPressed: () {
  20.                   context
  21.                       .read(_diceProvider)
  22.                       .state
  23.                       .add(DieWidget(value: 5, min: 1, max: 6));
  24.                   print(diceWatcher[0].value);
  25.                 },
  26.               ),
  27.               [..._diceProvider]
  28.             ],
  29.           );
  30.         },
  31.       ),
  32.     );
  33.   }
  34. }
  35.  
  36. /**** die_widget.dart *****/
  37. class DieWidget extends StateNotifier {
  38.   DieWidget({@required this.value, @required this.min, @required this.max})
  39.       : super(0);
  40.   var value = 3;
  41.   int min = 1;
  42.   int max = 6;
  43.   int id = 12;
  44.  
  45.   Widget build(BuildContext context) {
  46.     return Container(
  47.       width: 100,
  48.       height: 100,
  49.       child: FlatButton(
  50.           padding: EdgeInsets.all(0),
  51.           shape: RoundedRectangleBorder(
  52.             borderRadius: BorderRadius.circular(10),
  53.           ),
  54.           child: Image.asset('assets/dice/die-$value.png', scale: 2.5),
  55.           onPressed: () {
  56.             value = Random().nextInt(9) + 1;
  57.           },
  58.           onLongPress: () {
  59.             print("Long press");
  60.             print("id = " + id.toString());
  61.             // Delete die
  62.           }),
  63.     );
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement