Flutter bottomNavigationBar (change only body section) - flutter

Ok so I have got the following bottomNavigationBar working, however it's not clean enough for me and I am hoping there is a better way that someone knows.
Basically I only want to change the body section and not the full page.
However I want to have each section in different classes to keep it neat (ideally new .dart files - as they will all have different functions)
Currently all the page info is inside the body tag
body: PageView(
controller: _myPage,
onPageChanged: (int) {
print('Page Changes to index $int');
},
children: <Widget>[
Center(
child: Container(
child: Text('Empty Body 0'),
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('images/pic1.jpg'),
Text('images/pic2.jpg'),
Text('images/pic3.jpg'),
],
),
),
Center(
child: Container(
child: Text('Empty Body 2'),
),
),
Center(
child: Container(
child: Text('DRN1 app is made using Google Flutter. While every attempt was made to make sure that this app works on as many devices as possible. We are unable to test every device. If you have issues running this app please let us know'),
),
)
],
physics: NeverScrollableScrollPhysics(), // Comment this if you need to use Swipe.
),
what I would like is something like this.
body: PageView(
controller: _myPage,
onPageChanged: (int) {
print('Page Changes to index $int');
},
children: <Widget>[
home(),
news() , // this is the main body for home
shows(), // this one shows the class shows() which fetches JSON and returns a different body layout
about(), // about text
]
Does anyone know of a better way to do this?

What you can do is create separate Dart files and import them into your main.dart
Then inside your State class define a list which contains the pages that you want to show
List<Widget> _myPage = <Widget>[Page1class(),Page2class(),Page3class()];
After that you can use the below code which looks quite clean. Using the builder() method will allow you to create pages based on the size of _myPage,
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
appBar: AppBar(title: Text('sdf')),
body: PageView.builder(
itemBuilder: (context, position) => _myPage[position],
itemCount: _myPage.length,
),
)
);
Here is a sample program: PageView sample

Related

Flutter: Is it possible to use single JSON file in bodies of more expansion panels?

In my app, I have one JSON file with some static data which I use to produce List of a widgets. Then, I have one screen with ExpansionPanelRadio showing few items and each of them, when expanded, (in their bodies) are containing that list of a widgets made using JSON file.
I am using provider and I am able to display that list of widgets inside body of expansionpanel but the lists are somehow repeating.
For example, I expand one panel and in its body list is displayed few times, like in a loop. I guess, that the problem is in provider but I don't understand it quite well.
I am pretty new to flutter and would appreciate if someone could explain me why is this happening and what approach should I use to solve it.
here is part of a code where i make those expansion panels with provided JSON in a body:
SingleChildScrollView(
child: ExpansionPanelList.radio(
elevation: 0,
children: MyList.map<ExpansionPanelRadio>((Item item) {
return ExpansionPanelRadio(
value: MyList.indexOf(item),
headerBuilder: (BuildContext context, bool isExpanded) {
return Row(
children: [
Padding(
padding: const EdgeInsets.all(10),
child: SvgPicture.asset(
"assets/images/some_image.svg"
),
),
Text('some label'),
],
);
},
body: ChangeNotifierProvider(
create: (context) => FeaturesProvider(),
builder: (context, child) {
Provider.of<FeaturesProvider>(context, listen: false)
.readFeatures();
return SingleChildScrollView(
child: Container(
child: Features(item.objectId.toString()),
),
);
}));
}).toList(),
))

How to pre-cache flutter flare animation

I have a flare loading animation which takes time to load. Is there a way to pre-cache the flutter animation?
final AssetProvider assetProvider = AssetFlare(bundle: rootBundle, name: 'assets/animations/loop.flr');
cachedActor(assetProvider);
Is this the code to cache the actor?
Then How do I load the cached animation?
From https://github.com/2d-inc/Flare-Flutter/issues/180#issuecomment-550584347
You can use FlareCacheBuilder to help you preload flr files for certain sections of your app
code snippet
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: FlareCacheBuilder(
["assets/Filip.flr"],
builder: (BuildContext context, bool isWarm) {
return !isWarm
? Container(child: Text("Loading..."))
: FlareActor(
"assets/Filip.flr",
alignment: Alignment.center,
fit: BoxFit.contain,
animation: _animationName,
);
},
),
)
],
),
),
);
}
If you want a 'global' load to your .flr files the correct way is using cachedActor just like you posted.
Let's say you previously executed:
final assetProvider = AssetFlare(bundle: rootBundle, name: 'assets/animations/loop.flr');
await cachedActor(assetProvider);
When you try to use the same .flr in a FlareActor it will already try to first load this asset from the same cache you populated before.
In this example the Flare team does exactly this to warmup some files. But notice that they are using an old sintaxe for cachedActor.
You can see at the source code here and here, that when loading the asset it will try at first to load from the cache.
One last thing to finish, FlareCacheBuilder uses the same cachedActor method internally.

How to use provider beetween several classes in flutter?

I am creating a register app in flutter, but there is too much information tu use a single screen, so I decided to use tab views in the screens and separate the screens in objects of a drawer.
but I needed to use the information from diferent screens to saved it. so I think about using a Multiprovider. but when I try to use the information in the provider it throws:
Could not find the correct Provider<General> above this Baseformularios Widget
my implementation is like this:
Here is my drawer with the first field General, that is basic information about the client,
as you can see the provider is created here and the body: is supplied by a variable called bodycontent, that will be over written when a try to change the form, but when I try to print the name of the user it throws the error above.
MultiProvider(
providers: [
ChangeNotifierProvider<General>(
create: (context)=>General(),
)
],
child: Scaffold(
appBar: AppBar(
title: Text("Historias clinicas"),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.check),
onPressed: (){
print(Provider.of<General>(context).nombre);
Provider.of<General>(context).addCLiente();
},
)
],
),
body: bodycontent,
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
AspectRatio(
aspectRatio: 0.001/0.001,
child: DrawerHeader(
decoration: BoxDecoration(
gradient: LinearGradient(colors: [Colors.white,Colors.blueAccent]
),
),
),
ListTile(
title: Text("General"),
onTap: (){
setState(() {
bodycontent = I_II_III_IV();
});
Navigator.pop(context);
},
),
],
),
),
),
);
and here is the usage on the form general:
Provider.of<General>(context).set(nombre.text,
"Apellido hay que quitar en la funcion",
edad.text,
_currentsexo,
estado_civil.text,
direccion.text,
emergencia.text,
procedencia.text,
telefono.text,
ocupacion.text,
referencia.text,
fecha_inicio.text,
"",userid);
this save well the information, I checked it in the debugger, but the problem is in the other side.
I tried using the multiprovider in the screen before the one with the drawer, but it throwed the same error but with "above generalform". notice that I removed the multiprovider from the screen with the drawer when I tried this.
so should I declare the multiprovider in every screen to use it into it's son? because I supposed that provider where variables that you could use in every screen after the one where it's declare, or am I missunderstanding something?... Every help is very appreciated, thanks before hand.
To get access to the Provider from every where in your app, you should declare it in the main:
void main() {
runApp(MultiProvider(
providers: [
ChangeNotifierProvider<General>(
create: (context) => General(),
),
],
child: MyApp(),
));
}

how to display detailed information after clicking a gridview in flutter

Sorry bad english.. I want to navigate from one screen to another base on id from gridview items.
when one of the listview item11 is clicked it will be directed to the detail page item11..
So, how do create a detailed page design when one of the ListView's clicks is different.
full my code:
class CategoryScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new SafeArea(
child: new Scaffold(
appBar: new BankAndaAppBar(),
body: new Container(
color: Colors.grey[100],
child: new ListView(
physics: ClampingScrollPhysics(),
children: <Widget>[
new Container(
padding: EdgeInsets.only(left: 12.0, right: 12.0, top: 12.0),
color: Colors.grey[100],
child: new Column(
children: <Widget>[
_bodyGridView(),
],
)),
],
),
),
),
);
}
Widget _bodyGridView() {
return SingleChildScrollView(
child: new Column(
children: <Widget>[
GridView.count(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 3,
children: CATEGORIES_DUMMY_DATA
.map((cat) => ListCategory(cat.id, cat.title, cat.titleDetail, cat.description, cat.image))
.toList(),
),
],
),
);
}
}
I want display detailed information after clicking a gridview item. example : every clicking a item 1 show detail view item=1.
enter image description hereThanks.
As per my understanding, you want to display detailed information after clicking a grid item.
If so, there are many ways to do it.
Pass the data within pages : follow
Create a model class, store the data in memory and display it. follow
If you want to navigate from one screen to another follow

ListView lagging only when widgets in the list are Video_Player widgets

I'm trying to achieve a scrollable List with videos. I'm using the video_player widget and wrapping the player in a Card with a simple button.
Now i noticed that whenever I use ListView.builder with videos in the list, it is extremely lagging, especially when scrolling back up. i'm posting a GiF bellow if you would like to see the behaviour.
I have this problem ONLY when I have Videos in the list.
If I replace the videos with a simple Image widget, the scrolling is smooth and runs as intended. (Also provided a GiF below)
When I scroll through the list I get this message in my Console:
flutter: Another exception was thrown: setState() or markNeedsBuild() called during build.
And I think (but not sure) that this is the cause of the problem, maybe the way I implemented the video_player plugin (?)
class VideoPlayPause extends StatefulWidget {
VideoPlayPause(this.controller);
final VideoPlayerController controller;
#override
_VideoPlayPauseState createState() => _VideoPlayPauseState();
}
class _VideoPlayPauseState extends State<VideoPlayPause> {
//This Part here
_VideoPlayPauseState() {
listener = () {
setState(() {});
};
}
Maybe its setting state every time I scroll ?
I tried flutter run --release but saw no difference at all.
I'm running the app on a physical Iphone X.
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('MVP_Test1'),
),
body: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: videoUrl.length,
itemBuilder: (context, i) {
return Card(
child: Column(
children: <Widget>[
Column(
children: <Widget>[
const ListTile(
leading: Icon(Icons.live_tv),
title: Text("Nature"),
),
Stack(
alignment: FractionalOffset.bottomRight +
const FractionalOffset(-0.1, -0.1),
children: <Widget>[
// If I replace this with Image.asset("..."), the scrolling is very smooth.
AssetPlayerLifeCycle(
videoUrl[i],
(BuildContext context,
VideoPlayerController controller) =>
AspectRatioVideo(controller)),
]),
],
),
ButtonTheme.bar(
child: ButtonBar(
children: <Widget>[
FlatButton(
child: const Text('ADD VIDEO'),
onPressed: () {
/* ... */
},
),
],
),
),
],
),
);
},
),
);
}
Result when I run Flutter Analyze
flutter analyze
Analyzing mvp_1...
No issues found! (ran in 1.9s)
You can see how when scrolling back up it looks like the app is skipping frames or something. Here's a video:
https://giphy.com/gifs/u48BNQ13r15Zay5SnN
Here's a video with photos instead of videos:
https://giphy.com/gifs/236RKyA8y1pfecmR1d