Jump pages in Pageview.builder with Consumer - flutter

I'm trying to make a feed where once the users uploads a post, the post gets added to the top of the feed and the user can see the post they just made. Right now I have a ChangeNotifierProvider and Consumer that is getting newly updated data from my view model. Inside the ChangeNotifierProvider, I have a pageview.builder. How would I get the pageview.builder to refresh pages so that the users post is now shown? I have tried using the onPageJumped but I can't use that inside the ChangeNotifier.
Widget _buildScreen(VideoPlayerViewModel viewModel) {
return ChangeNotifierProvider<VideoPlayerViewModel>(
create: (context) => viewModel,
child: Consumer<VideoPlayerViewModel>(
builder: (context, model, child) => PageView.builder(
controller: pageController,
scrollDirection: Axis.vertical,
itemCount: videoPlayerViewModel.intialVideoDataLength,
itemBuilder: (BuildContext context, int index) {
var data = videoPlayerViewModel.intialVideoData;
return VideoScaffoldWidget(videoData: data[index]);
})));
}

Try adding unique key to the PageView, it should do the trick
PageView.builder(
key: UniqueKey(),

Related

Flutter BLoC and Card widget in ListView

I am a beginner in programming. I am currently trying to make an application that displays news from the firestore database.
I have created a Bloc, Repository and Service for Firestore and it works fine for now. Inside BlocBuilder I use a ListView Builder that displays the articles inside a Card widget.
Now I need inside each Card Widget to use Bloc for options like "Bookmarks", "Comments", "Reactions".
My question is the following. Is it correct to use the following solution:
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return BlocProvider(
create: (BuildContext context) => CardBloc(),
child: CardWidget(...),
);
},
)

Flutter returning to same position in richtext in listview inside a pageview

I am trying to finish my first flutter app
it is a Quran app, and i was able so far to fetch and show the data in page view, each page has list view which contains the data depending on user selection, so one listview can be very long
in the list view there is a list of rich texts, each containing textspans,
here is the code
PageView.builder(
controller: _pageController,
itemCount: ConstantsHelper.selectionCount[widget.args.type],
scrollDirection: Axis.horizontal,
reverse: true,
itemBuilder: (context, index) => StreamBuilder<Object>(
stream: null,
builder: (context, snapshot) {
return FutureBuilder(
future: Provider.of<QuranProvider>(context)
.getSelection(widget.args.type, index + 1),
builder: (context, snapshot) =>
snapshot.connectionState == ConnectionState.waiting
? CircularProgressIndicator()
: ListView.builder(
itemCount: 1,
scrollDirection: Axis.vertical,
itemBuilder: (ctx, index) => Column(
children: WidgetHelper().getContent(
Provider.of<QuranProvider>(context)
.ayas,
context),
),
),
);
},
),
),
basically this is how the app looks like
one rich text can be so long that it requires lots of scrolling and can take the entire page
now, what I need to do, is getting the scrolling position, so when the user opens the app, the app will automatically scroll to the correct page and line
the page part is already done, however, when I try to implement the scrollController I get error ScrollController attached to multiple scroll views
I tried to set a listener in initstate to the scroll listener, the problem is that i am not sure where to add the controller and the listener
the controller.position doesnt change when i scroll, which is not useful.
so, how can I listen to the scrolling position change with a pageview parent?

How to display data from MySQL database in table format in flutter application?

This is my code. Here, I have used Listview.builder due to which whole data is displayed in 1 tablecell. What should I use at place of listview to display the data properly in different cells.
Or Any other way to display data dynamically from backend?
TableRow(children: [
TableCell(
child: FutureBuilder(
future: fetchProducts(),
builder: (context, snapshot){
if(snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
shrinkWrap: true,
itemBuilder: (BuildContext context, index){
Products product = snapshot.data[index];
return Text(//"Name :- "
'${product.pname}', style: TextStyle(fontSize: 15));
});
}},
)),]),
In place of tablerow use DataTable, it will automatically size itself, it has column and row children so its probably the best way to display your data, check this youtube vide out
https://www.youtube.com/watch?v=ktTajqbhIcY&vl=en

Load data from the Database

I followed the chat development app tutorial on the Flutter page, it's a single user app. I have now been able to create a database and store data in the database, but I now have a conundrum that I'm unable to resolve.
I'm loading the chats from the database using FutureBuilder
FutureBuilder(
future: _requestSqlDataAsync(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: Text("Loading...."),
),
);
}
else {
return ListView.builder(
padding: EdgeInsets.all(8.0),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Text(snapshot.data[index].chat);
}
);
It successfully loads the data from the database but then I'm unable to pass submit new chats from TextController to the display widget in the following manner:
ListView.builder(
padding: EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, int index) => _message[index],
itemCount: _message.length,
)
where _message is the widget that combines the data from the TextController along with animation, to display the chat.
Could anyone please guide me as to how I can load the data from the database and then pass the control for the chats to be loaded.

DraggableScrollableSheet doesn't give the current position of the sheet when dragging

In flutter we have a widget DraggableScrollableSheet. Now I want the current position or the current size of the child while it is dragging. There is currently no way to get that value. It gives a ScrollController in its builder method but that is for when the list is scrolling and not when its being dragged. So is there another way by which I can track the current drag position of the list?
I tried adding NotificationListener but that only gives me dragstartnofification and dragendnotification and nothing for dragupdate:
DraggableScrollableSheet(
initialChildSize: .70,
minChildSize: .70,
builder:
(BuildContext context, ScrollController scrollcontroller) {
return NotificationListener(
onNotification: (notification) {
print("$notification");
},
child: Container(
child: ListView.builder(
controller: scrollcontroller,
itemCount: widget.songs.length,
itemBuilder: (BuildContext context, int index) {
return buildSongRow(widget.songs[index]);
}),
),
);
}),
I found the solution to this. Flutter team updated the ScrollableDraggableSheet class in the Flutter v1.7.3. They added a class called DraggableScrollableNotification which you can listen to to get the current extend of the child when dragging.
NotificationListener<DraggableScrollableNotification>(
onNotification: (notification) {
print("${notification.extent}");
},
child: DraggableScrollableSheet(
initialChildSize: .70,
minChildSize: .70,
builder:
(BuildContext context, ScrollController scrollcontroller) {
return Container(
child: ListView.builder(
controller: scrollcontroller,
itemCount: widget.songs.length,
itemBuilder: (BuildContext context, int index) {
return buildSongRow(widget.songs[index]);
}),
);
}),
)
in the above sample notification.extent will give you the current extent(position) of the child when dragging.
Note: this is currently in the master channel and not in the stable
channel. Switch to the master channel by running flutter channel master
and then flutter upgrade to make it work for now.