I was trying to achieve this outlook. [1]: https://i.stack.imgur.com/6COFI.png It's the first time I'm using provider. The same code worked well with a todo app. But I could not get to work the code for this app. I've tried to tweak with the code, but only in vain.
The error message is, ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ The following assertion was thrown while handling a gesture: Tried to listen to a value exposed with provider, from outside of the widget tree.
This is likely caused by an event handler (like a button's onPressed) that called Provider.of without passing listen: false
.
To fix, write: Provider.of(context, listen: false);
It is unsupported because may pointlessly rebuild the widget associated to the event handler, when the widget tree doesn't care about the value.
The context used was: AddPlayersScreen(state: _AddPlayersScreenState#da0e7) 'package:provider/src/provider.dart': Failed assertion: line 242 pos 7: 'context.owner.debugBuilding || listen == false || debugIsInInheritedProviderUpdate'
When the exception was thrown, this was the stack: #2 Provider.of (package:provider/src/provider.dart:242:7) #3 _AddPlayersScreenState.build. (package:badminton_app/screens/add_players_screen.dart:73:34) #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19) #5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:1098:38) #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24) #7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11) #8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5) #9 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7) #10 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27) #11 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20) #12 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22) #13 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7) #14 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7) #15 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7) #19 _invoke1 (dart:ui/hooks.dart:267:10) #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5) (elided 5 frames from class _AssertionError and dart:async)
Handler: "onTap" Recognizer: TapGestureRecognizer#65790 ════════════════════════════════════════════════════════════════════════════════════════════════════
import 'package:badminton_app/constants.dart';
import 'package:badminton_app/model/player_list.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/constants.dart';
import 'package:badminton_app/model/players_data.dart';
import 'package:badminton_app/widgets/check_cards.dart';
TextEditingController _controller = TextEditingController();
class AddPlayersScreen extends StatefulWidget {
@override
_AddPlayersScreenState createState() => _AddPlayersScreenState();
}
class _AddPlayersScreenState extends State<AddPlayersScreen> {
String newText;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff07021A),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 30.0,
),
Text(
'Add Players',
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
fontFamily: 'Montserrat'),
),
SizedBox(
width: 400.0,
height: 50.0,
child: Divider(
height: 10.0,
color: Color(0xff525274),
),
),
Container(
constraints: BoxConstraints.tight(Size(400.0, 70.0)),
child: TextField(
cursorColor: Color(0xffA8A3BE),
style: TextStyle(color: Color(0xffA8A3BE), fontSize: 20.0),
controller: _controller,
onChanged: (newValue) {
newText = newValue;
},
enabled: true,
decoration: InputDecoration(
suffix: IconButton(
onPressed: () {
_controller.clear();
},
icon: CircleAvatar(
radius: 13.0,
backgroundColor: Color(0xff939393),
child: Icon(
Icons.clear,
size: 25.0,
color: Color(0xff585179),
),
),
),
suffixIcon: IconButton(
onPressed: () {
Provider.of<PlayerData>(context).changeString(newText);
},
icon: Container(
height: 100.0,
width: 50.0,
decoration: BoxDecoration(
color: Colors.yellow,
borderRadius: BorderRadius.only(
topRight: Radius.circular(5.0),
bottomRight: Radius.circular(5.0))),
child: Icon(
Icons.add,
color: Colors.black,
)),
),
hintText: "New member......",
hintStyle: TextStyle(
fontSize: 20.0,
fontFamily: "Montserrat",
color: Color(
0xffA199C6,
),
),
filled: true,
fillColor: Color(0xff585179),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
borderSide: BorderSide.none),
),
),
),
SizedBox(
height: 20.0,
),
Expanded(
child: PlayerList(),
),
],
),
),
),
);
}
}
import 'dart:collection';
import 'package:flutter/cupertino.dart';
import 'package:badminton_app/model/players.dart';
class PlayerData extends ChangeNotifier {
List<Players> _playerData = [];
UnmodifiableListView<Players> get playerData {
return UnmodifiableListView(_playerData);
}
toggleSelect(Players players) {
players.toggle();
notifyListeners();
}
int get getLength {
return playerData.length;
}
changeString(newString) {
_playerData.add(Players(name: newString));
notifyListeners();
}
removeString(Players player) {
_playerData.remove(player);
notifyListeners();
}
}
import 'package:badminton_app/model/players_data.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/model/players.dart';
class CheckCards extends StatelessWidget {
final String name;
final bool isSelected;
final Function callBack;
CheckCards({this.name, this.isSelected = false, this.callBack});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 20.0),
child: Stack(
children: [
Positioned(
left: 25.0,
top: 17.0,
child: Center(
child: InkWell(
onTap: callBack,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(7.0)),
color: Colors.yellow),
child: Padding(
padding: const EdgeInsets.all(1.0),
child: isSelected
? Icon(
Icons.brightness_1,
size: 30.0,
color: Color(0xff585179),
)
: Icon(
Icons.stop,
size: 30.0,
color: Color(0xff585179),
),
),
),
),
),
),
Positioned(
left: 80.0,
top: 22.0,
child: Text(
'Sai Deepak',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white,
fontFamily: 'Montserrat'),
),
),
],
),
height: 70.0,
width: 500.0,
decoration: BoxDecoration(
color: isSelected ? Color(0xff21213C) : Color(0xff585179),
borderRadius: BorderRadius.all(Radius.circular(10.0))),
);
}
}
import 'package:badminton_app/model/players.dart';
import 'package:badminton_app/model/players_data.dart';
import 'package:badminton_app/widgets/check_cards.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badminton_app/widgets/tile.dart';
class PlayerList extends StatefulWidget {
@override
_PlayerListState createState() => _PlayerListState();
}
class _PlayerListState extends State<PlayerList> {
@override
Widget build(BuildContext context) {
return Consumer<PlayerData>(builder: (context, data, child) {
return ListView.builder(
itemBuilder: (context, index) {
final playo = data.playerData[index];
return CheckCards(
name: playo.name,
isSelected: playo.isDone,
callBack: () {
data.toggleSelect(playo);
},
);
},
itemCount: data.getLength,
);
});
}
}
Can somebody please help me with this. This thing is screwing up mind for days. Thanks in advance.
setState((){})
after that code should rebuild the UI – Aldy Yuan