tazboy

Untitled

Dec 12th, 2020
951
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.               // Supposed to show the dice in the list, but there's an error
  28.               [..._diceProvider]
  29.             ],
  30.           );
  31.         },
  32.       ),
  33.     );
  34.   }
  35. }
  36.  
  37. /**** die_widget.dart *****/
  38. class DieWidget extends StateNotifier {
  39.   DieWidget({@required this.value, @required this.min, @required this.max})
  40.       : super(0);
  41.   var value = 3;
  42.   int min = 1;
  43.   int max = 6;
  44.   int id = 12;
  45.  
  46.   Widget build(BuildContext context) {
  47.     return Container(
  48.       width: 100,
  49.       height: 100,
  50.       child: FlatButton(
  51.           padding: EdgeInsets.all(0),
  52.           shape: RoundedRectangleBorder(
  53.             borderRadius: BorderRadius.circular(10),
  54.           ),
  55.           child: Image.asset('assets/dice/die-$value.png', scale: 2.5),
  56.           onPressed: () {
  57.             value = Random().nextInt(9) + 1;
  58.           },
  59.           onLongPress: () {
  60.             print("Long press");
  61.             print("id = " + id.toString());
  62.             // Delete die
  63.           }),
  64.     );
  65.   }
  66. }
RAW Paste Data