Flutter Android PreferenceScreen settings page - dart

Android has PreferenceScreen which provides you the consistent Settings interface and handles a lot of SharedPreferences functionality on its own.
Is there anything similar in Flutter or do I have to create it by my own using custom ListView?

So, there doesn't exist any widget like that, you will have to create it on your own and handle their action in onChanged or onTap events. For example:
CheckBoxPreference
CheckboxListTile(
value: true,
title: Text("This is a CheckBoxPreference"),
onChanged: (value) {},
),
SwitchPreference
SwitchListTile(
value: false,
title: Text("This is a SwitchPreference"),
onChanged: (value) {},
),
ListPreference
ListTile(
title: Text("This is a ListPreference"),
subtitle: Text("Subtitle goes here"),
onTap: (){},
)

In Flutter the easiest way to make a PreferenceScreen page is with a ListView and ListTiles.
ListView(children: <Widget>[
ListTile(
title: Text('Enable Feature'),
trailing: Checkbox(
value: PrefService.getBool('feature_enabled'),
onChanged: (val) {
setState(() {
PrefService.setBool('feature_enabled', val);
});
},
),
onTap: () {
setState(() {
PrefService.setBool(
'feature_enabled', !PrefService.getBool('feature_enabled'));
});
},
)
]),
But you still need to listen for changes and save them.
To make that easier you can use the package preferences. It removes the boilerplate code and manages saving the changes. The example above would look like this with preferences:
PreferencePage([
CheckboxPreference(
'Enable Feature',
'feature_enabled',
)
]),
You can find it under https://pub.dartlang.org/packages/preferences.
For more features like subpages, hiding of preferences or more widgets look at the example.

According to my research now (I checked the https://pub.dartlang.org and other resources), the answer to your question is, no there is no page like that in Flutter (Check the official documentation). There is one plugin to have SharedPreferences which is from Flutter team, but if you check out its source code, you can see that it just does data modification. Other alternatives from other developers doesn't have anything visual too (I will keep checking, if I find one, I will edit my post).
There are some ways to do it, you can do it by calling android bridge and having android specific screen for only android (yeah I know, it doesn't make much sense) when you are with flutter or my real answer would be you can implement it by using list view as you mentioned and assign different child elements according to your needs.

Related

Cannot interact with Flutter web app TextButton (click, hover) on work computer using chrome, edge, or explorer, but firefox works

Update 2: created a new app with just the demo and it also doesn’t work on work computer except Firefox. Tried setting alerts throughout the index.html service_worker and they seem to trigger. The app that does work is older, so maybe something broken in the newer versions?
Update: I copied the working button into the non working one and it doesn’t work. So something about the entire web app doesn’t seem to work, not just the button.
Can someone suggest next steps to diagnose this issue? At home, everything works, so I'm assuming the issue lies in the workplace firewall (just a guess).
I'm using a simple TextButton, and on Firefox it works just fine (hover effect shows, and clicking does what it's supposed to).
However, in Chrome, Explorer, and Edge hovering over the button shows no hover effect, and clicking does nothing.
Unfortunately, the inspector is disabled at work, so I can't try and see what's going on, though that's limited anyway based on how Flutter draws out the app.
Interestingly (and perhaps helpfully), I have a second Flutter web app with a TextButton that does seem to work in all browsers at work. Here are the two buttons:
Working in all browsers:
TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(20)),
foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
overlayColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered))
return Colors.blue.withOpacity(0.8);
if (states.contains(MaterialState.focused) ||
states.contains(MaterialState.pressed))
return Colors.blue.withOpacity(0.12);
return null; // Defer to the widget's default.
},
),
),
child: new Text("Button Text", style: TextStyle(color: Colors.white)),
onPressed: () {
setState(() {
Navigator.pushNamed(context, '/route');
});
},
),
Working only in Firefox:
TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(20)),
foregroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered))
return colorButtonTextHovered;
if (states.contains(MaterialState.focused) ||
states.contains(MaterialState.pressed))
return colorButtonTextFocusPress;
return colorButtonTextDefault;
},
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.hovered))
return colorButtonBackgroundHovered;
if (states.contains(MaterialState.focused) ||
states.contains(MaterialState.pressed))
return colorButtonBackgroundFocusPress;
return colorButtonBackgroundDefault;
},
),
),
child: new Text("Button Text"),
onPressed: () {
setState(() {
Navigator.push(
context,
FadeRoute(
MyApp.mtAppKey,
'/route',
),
);
});
},
),
Any tips, suggestions, next steps to diagnosing and fixing this issue?

Is there a way to display an image if the requested image is not in my assets?

the title might be a bit confusing but i'll try to explain it better with an example.
I am using the widget
Image.asset(
'assets/images/${object.name}.jpg',
),
but i want to show a default image (from assets or network) if the selected one is not available.
Is this possible?
Thanks.
You can use the errorBuilder function to return another widget if the asset loading fails.
Widget createImage(String iconName) {
return Image.asset('assets/icons/${iconName}.png',
errorBuilder: (context, object, stacktrace) =>
Image.asset('assets/icons/default.png'));
}
If you are looking for a placeholder you could use the following package here.
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: new CircularProgressIndicator(),
errorWidget: new Icon(Icons.error),
),

How to display more than 3 items in a BottomNavigationBar while coding in flutter

I tried adding 5 BottomNavigationBarItem but, the compiler throws an error if I try to add more than 3 items. It looks something like this:
The following RangeError was thrown building BottomNavigationBar(dirty, state:
_BottomNavigationBarState#a56dd(tickers: tracking 3 tickers)):
RangeError (index): Invalid value: Not in range 0..2, inclusive: 3
I need to display 5 items in the BottomNavigationBar. Help me out on this one.
BottomNavigationBar missing
there is the link to the code and, currently there are just three items in there, I wanna add two more items without the compiler throwing error message
Just writing it for the future query regarding on this issue.
Just add a extra parameter in your BottomNavigationBar Constructor -
type : BottomNavigationBarType.fixed
Check also the Flutter Official Ref.
Optional : Restart your app from start for fixing rendering issue.
It's because the default NavigatioBar doesn't support more than 3 items, Use this parameter inside your BottomNavigationBar widget:
type: BottomNavigationBarType.fixed
OR Copy and Paste this code below
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed, ///This allow more than 3 items
backgroundColor: Theme.Colors.primaryDarkColor,
currentIndex: 1,
items: [
BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up,), title: Text("GLO",
style: TextStyle(color: Colors.black),),),
BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title:
Text("MTN"),),
BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title:
Text("Airtel"),),
BottomNavigationBarItem(icon: Icon(Icons.arrow_drop_up), title:
Text("9mobile"),),
],
),

What are the options of collapsable list widgets in Flutter?

I need to have some lists collapse by their topics in my project and am wondering if I'll need to implement this from zero or rather use a component from flutter. Does this component exist?
Thanks in advance :)
The Flutter Gallery has two examples that may be relevant for your accordion-like lists.
Expansion Panel Demo & Two-level List Demo
The Expansion Panel demo is probably what you want. If so, take a look at how the demo leverages ExpansionPanel and uses a headerBuilder and body. You can extends this to make the header and bodies as complex as you need. The Gallery demo adds a DemoItem helper class. You can use this pattern or come up with your own design.
Here is a snippet that shows the demo using ExpansionPanelList by passing a callback and the list of DemoItems:
child: new ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
_demoItems[index].isExpanded = !isExpanded;
});
},
children: _demoItems.map((DemoItem<dynamic> item) {
return new ExpansionPanel(
isExpanded: item.isExpanded,
headerBuilder: item.headerBuilder,
body: item.build()
);
}).toList()
),

Build transition for pageRouteBuilder in Flutter

I have implemented routing in my flutter app via on-the-fly route generation with
onPressed:() => Navigator.of(context).push(new PageRouteBuilder(
pageBuilder: (_, __, ___) => new Video(),
)),
The transition from one page to another however is instant and without the native 'in from left' or 'in from bottom' animation, depending on whether you target iOS or Android. Is there a way to implement the native OS transitions without having to implement the animation from scratch.
I know that you can pass the transitionBuilder parameter to the PageRouteBuilder to create transitions, but so far I haven't found any information on how to create the necessary transitions or whether premade transitions are available. Any help with the implementation of the native transitions mentioned above would be appreciated!
You can just use MaterialPageBuilder instead of PageRouteBuilder.
To play video, you can look into this.
example:
import 'package:chewie/chewie.dart';
final playerWidget = new Chewie(
new VideoPlayerController(
'https://flutter.github.io/assets-for-api-docs/videos/butterfly.mp4'
),
aspectRatio: 3 / 2,
autoPlay: true,
looping: true,
);
onPressed:() => Navigator.of(context).push(new MaterialPageRoute(
pageBuilder: (BuildContext context) {
return new Container(child: playerWidget);
},
)),
Hope this helped!

Resources