Switch Button is not changing [closed] - flutter

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
class _PageOne extends State<Page> {
bool notifications = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
drawer: Drawer(),
body: Container(
child: Column(
children: [
SwitchListTile(
title: Text("Allow Notifications"),
secondary: Icon(Icons.notifications_active),
value: notifications,
onChanged: (val) {
notifications = val;
print(notifications);
})
],
),
));
}
}

onChanged: (bool val) {
setState(() {
notifications = val;
print(notifications);
});
},
As mentioned in the comments by esentis you need setState(){} so that the widget will rebuild itself when a change happens.

Assign notification to value inside the body of setState meanwhile call setState I hope you understand

Related

enum variable has to be initialized in dart >>>>>>>>>>> Can you please tell me how to declare this variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How can I declare the selectedGender variable? I'm having an initialize error.
The variable is inside a StatefullWidget.
enum Gendertype {male, female}
class _MyHomePageState extends State<MyHomePage> {
Gendertype selectedGender;
#override
Widget build(BuildContext context) {
return Column(
child: Expanded(
child: GestureDetector(
onTap: (){
setState(() {
selectedGender= Gendertype.male;
});
},
child: ReusedCard(
labledcolor: selectedGender==Gendertype.male?
inactivekcardcolor:activekcardcolor,
childCard: CardIcon(genderIcon: FontAwesomeIcons.mars,
labletype: "Male",
),
),
)
),
);
}
According to the null safety rules, you have these options:
Declare selectedGenderas a nullable variable:
Gendertype? selectedGender;
Define a first value to the variable:
Gendertype selectedGender = Gendertype.male

How to fill the space between two text elements with dots in Flutter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to show data like this:
Burger..........................$9.99
Steak and Potato...............$14.99
Mac and Cheese..................$6.99
How can I implement it in Flutter?
as an option
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) => MaterialApp(home: Home());
}
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Row(
children: [
Text('Burger'),
Expanded(child: Text('.' * 100, maxLines: 1)),
Text('\$9.99'),
],
),
);
}
}
I personally use fDottedLine package https://pub.dev/packages/fdottedline to draw a dotted line in any form.
: Row(
children: [
Text('Burger'),
FDottedLine(
color: Colors.black,
width: 160.0,
strokeWidth: 2.0,
dottedLength: 10.0,
space: 2.0,
),
Text('\$9.99'),
],
),
You have to give the dynamic widgth using MediaQuery

How to see apps usage using flutter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I’m trying to develop an app using flutter , I want to have all apps usage on the mobile , I have been searching for some packages for this purpose I didn’t find something for this specific purpose.
You can copy paste run full code below
You can use package https://pub.dev/packages/usage_stats
In working demo you can see package and event timestamp history, then you can do summary
Step 1: AndroidManifest.xml, add xmlns:tools and android.permission.PACKAGE_USAGE_STATS
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package name" xmlns:tools="http://schemas.android.com/tools">
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
Step 2: Permit usage access, see working demo picture at left
working demo
full code
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:usage_stats/usage_stats.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<EventUsageInfo> events = [];
#override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
initUsage();
});
}
Future<void> initUsage() async {
UsageStats.grantUsagePermission();
DateTime endDate = new DateTime.now();
DateTime startDate = DateTime(2020, 1, 1, 0, 0, 0);
List<EventUsageInfo> queryEvents =
await UsageStats.queryEvents(startDate, endDate);
this.setState(() {
events = queryEvents.reversed.toList();
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Usage Stats"),
),
body: Container(
child: ListView.separated(
itemBuilder: (context, index) {
return ListTile(
title: Text(events[index].packageName),
subtitle: Text(
"Last time used: ${DateTime.fromMillisecondsSinceEpoch(int.parse(events[index].timeStamp)).toIso8601String()}"),
trailing: Text(events[index].eventType),
);
},
separatorBuilder: (context, index) => Divider(),
itemCount: events.length)),
floatingActionButton: FloatingActionButton(
onPressed: () {
initUsage();
},
child: Icon(
Icons.refresh,
),
mini: true,
),
),
);
}
}

Is it possible to control tabs(Hide/Visible) in Flutter programmatically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two tabs each with some data to be filled by the user. In Tab-1 (Personal Data) I have a form with some widgets where the user has to enter his personal details.
After filling all the details in the Tab-1 (form-1) only the Tab-2 (Medical History) should be navigated and visible. How to achieve this in Flutter?
We can achieve this by maintaining the count of tab bar items to be displayed.
Basically we have to create two List<Widget> which will depend on tab bar count to be displayed.
Example:
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _tabBarCount = 1;
List<Widget> getTabBarList() { // Tab Bar items displayed on the App Bar
switch (_tabBarCount) {
case 1:
return [Tab(icon: Icon(Icons.search))];
case 2:
return [
Tab(icon: Icon(Icons.search)),
Tab(icon: Icon(Icons.file_download))
];
default:
return [];
}
}
List<Widget> getTabScreen() { // Screens to be displayed on Tab Bar
switch (_tabBarCount) {
case 1:
return [
RaisedButton(onPressed: () {
setState(() {
_tabBarCount = 2; // Click event, here tab bar count should increse, so that multiple tab bar can be visible.
});
}, child: Text('Save'),)
];
case 2:
return [
Text('First Screen'),
Text('Second Screen')
];
default:
return [];
}
}
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: _tabBarCount,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
bottom: TabBar(
tabs: getTabBarList(),
),
title: Text('Tabs Demo'),
),
body: TabBarView(
children: getTabScreen(),
),
),
),
);
}
}

How to make a responsive play button in flutter? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I was able to make a play button but I could not able to make it responsive. If you can please tell me the code it would be helpful. Also I need to understand it as I am a beginner flutter user.
I think you should first learn about stateful widgets and basic flutter components first.
Introduction to widgets : https://flutter.dev/docs/development/ui/widgets-intro
Flutter Basic Widgets : https://flutter.dev/docs/development/ui/widgets
You can start from below code :
import 'package:flutter/material.dart';
class MusicPlayer extends StatefulWidget {
#override
_MusicPlayerState createState() => _MusicPlayerState();
}
class _MusicPlayerState extends State<MusicPlayer> {
var isPlaying = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Music Player"),
),
body: Center(
child: IconButton(
icon: isPlaying
? Icon(
Icons.pause_circle_outline,
size: 40.0,
)
: Icon(Icons.play_circle_outline, size: 40.0),
onPressed: () {
setState(() {
isPlaying = !isPlaying;
});
}),
),
);
}
}
This will help you get started for your project.