How to use flutter negation operator in stateful widget? - flutter

In normal approach to use negation operator is like this
isValid = !isValid
but this approach is not working inside stateful widget constructor variable like
widget.isValid = !widget.isValid
How to make this second approach valid .

You are adding the negation to just widget, It needs to be added to widget.isValid . Hence wrap it within brackets to work.
Change !widget.isValid to (!widget.isValid)
Example:
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
bool value = true;
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: SafeArea(
child: Scaffold(
body: Center(child: Text((!widget.value).toString())),
),
),
);
}
}
Output:

Related

No Directionality widget found ; I want to use only Container and Text

I just run the basic flutter code.
I want to make stateful widget, only containing Text("hi")
so I write my code like below
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Container(child: Text("hi");
}
}
but I got error
No Directionality widget found.
RichText widgets require a Directionality widget ancestor
I can fix my error to add textDirection attribute,
but I wonder when I use Text I didn't get that error even if I don't use that attribute.
I just want to use only Text, Container
i think its because you dont have MaterialApp
try it like this will work
void main() {
runApp(const MyApp1());
}
class MyApp1 extends StatelessWidget {
const MyApp1({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyApp(),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Container(child: Text("hi"));
}
}
Text needs a Directionality widget to provide information on whether the text is "left-to-right" or "right-to-left". Usually this is done (behind the scene) by MaterialApp widget. If you don't want to use that, you could provide one yourself, and specify the text direction.
Minimal code for a hello world:
import 'package:flutter/material.dart';
void main() {
runApp(
Directionality(
textDirection: TextDirection.ltr,
child: Text("Hello world."),
),
);
}

Detect power off button when i press four time in flutter

I am creating an application where I detect a 4-time press power button and perform some action on it I try my self but I unable to perform it please if you have any solution tell me
I am using hardware_buttons: ^1.0.0 packages in my this application.
my code
import 'dart:async';
import 'package:hardware_buttons/hardware_buttons.dart' as HardwareButtons;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, home: MyHomeScreen());
}
}
class MyHomeScreen extends StatefulWidget {
MyHomeScreen({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomeScreenState createState() => _MyHomeScreenState();
}
class _MyHomeScreenState extends State<MyHomeScreen> {
String _latestHardwareButtonEvent;
var counter = 0;
StreamSubscription<HardwareButtons.LockButtonEvent> _lockButtonSubscription;
#override
void initState() {
super.initState();
_lockButtonSubscription =
HardwareButtons.lockButtonEvents.listen((event) {
setState(() {
_latestHardwareButtonEvent = 'LOCK_BUTTON';
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Hardware Interaction"),
centerTitle: true,
),
body: Container(
child: Column(
children: [],
),
));
}
}

Why does my custom flutter scaffold prevent the state from being refreshed?

Below is the minimal version of what i'm attempting to do with my app that maintains the issue. The slider still responds to being interacted with by showing it's animation, however it does not change the value or actually move. This only seems to occur because the Scaffold has been separated out to it's own widget.
I know that changing it to a stateless widget would fix it in this case, however the version in my app requires state changes.
I've tried adding UniqueKeys up and down the widget tree with no luck, though i'll admit I don't fully understand their use in this case.
Any advice, or a point in the right direction? Should I keep playing around with keys, or am I going about this in the wrong way?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Scaffold Test',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage();
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _value = 20.0;
#override
Widget build(BuildContext context) {
return MyScaffold(
child: Center(
child: Slider(
min: 0,
max: 100,
value: _value,
onChanged: (double value) {
setState(() {
_value = value;
});
},
),
),
);
}
}
class MyScaffold extends StatefulWidget {
final Widget child;
MyScaffold({this.child});
#override
State<StatefulWidget> createState() => new MyScaffoldState(child);
}
class MyScaffoldState extends State<MyScaffold> {
Widget child;
MyScaffoldState(this.child);
#override
Widget build(BuildContext context) {
return Scaffold(
body: child,
);
}
}
As commented by user #pskink, removing the state constructor fixes the issue.
class MyScaffold extends StatefulWidget {
final Widget child;
MyScaffold({this.child});
#override
State<StatefulWidget> createState() => MyScaffoldState();
}
class MyScaffoldState extends State<MyScaffold> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: widget.child,
);
}
}
In flutter, you should never have constructors for states.
if you need to initialize state properties, use initState.

Trigger animations on widget load flutter

So, I have a AnimatedPositioned widget in my widget tree, which contains a form. I want the AnimatedPositioned widget to slide up from bottom when user navigates to the screen. Now, there are many tutorials which show how to do this when user clicks a button using the setState method. But how do I trigger this automatically when this screen is loaded?
The "right" way to do that would be not to use an implicit animation widget like AnimatedPositioned and instead use an explicit animation. Where you start your AnimationController in the initState of a StatefulWidget.
But just technically you could set the "destination" values for your AnimatedPositioned widget in a Future so that the value changes on the next frame. You try remoing the Future to see that otherwise the widget renders at its end position.
This way is not recommended but possible:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _dist = 0.0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: [
AnimatedPositioned(
child: Text('Hello Words'),
duration: Duration(seconds: 5),
left: _dist,
top: _dist,
),
]),
);
}
#override
initState() {
super.initState();
Future(() {
setState(() {
_dist = 250.0;
});
});
}
}

How to call a method in another stateful widget

I am building a podcasting type app, so need to call the record, stop, and play functions in many places, I created the methods, but difficulty to call these methods in other places.
main.dart
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String statusText = "";
bool isComplete = false;
void startRecord() //Need to call all of these method in coming stateful widgets
void stopRecord() //
void pauseRecord()//
void resumeRecord()//
void play() //
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(
builder: (context) => Scaffold(
drawer: Drawer(
elevation: 2.0,
child: ListView(
children: <Widget>[
ListTile(
title: Text('Home'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return MyApp();
},
),
);
},
),
//more code is here
Expanded(
child: GestureDetector(
child: IconButton(
icon: Icon(Icons.mic),
color: Colors.white,
iconSize: 40,
onPressed: () async {
startRecord();
}),
),
),
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return MaterialApp(
onPressed: () {
startRecord()
// need to call the method here.
}
Pressed: () {
stopRecord()
// need to call the method here.
}
Pressed: () {
play()
// need to call the method here.
}
),
}
Need to call all the methods from a first stateful widget for bottom stateful widgets
also, need to call these methods for other classes when code progress
both stateful widgets are in the main.dart. I could not call the method from the first class for the second stateful widget
This is not a rocket science, just a simple line of code, and you are done.
What you have to do, is to just call the MyHomePage() and let it accept the startRecording() to be used inside the Widget
1. Passing the data from MyApp() to MyHomePage()
#override
Widget build(BuildContext context) {
return MaterialApp(
// here you pass the your function
home: MyHomePage(onPressed: startRecording)
);
}
2. Receiving the data in MyHomePage()
class MyHomePage extends StatefulWidget {
// let it accept a function type onPressed argument
final Function onPressed;
// constructor
MyHomePage({Key key, this.onPressed}): super(key: key);
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return MaterialApp(
// simply call the onPressed which received your startRecording() from MyApp
onPressed: () => widget.onPressed()
}
You can get the state of a parent widget using the BuildContext of the child widget like so:
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
static _MyAppState of(BuildContext context) {
return context.findAncestorStateOfType<_MyAppState>();
}
}
class _MyAppState extends State<MyApp> {
String statusText = "";
bool isComplete = false;
void startRecord() {
print('Hello');
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
MyApp.of(context).startRecord();
return Scaffold(
body: Placeholder(),
);
}
}
Simply define that function outside the class as a stand-alone function like this But if you want to call from inside the class. Heres the code.
inside a different class as a static function:
onPressed: () {
_MyAppState().startRecord(); //call using the class name.
}
Like this inside your onpressed Statement.
Should work.
Or else what you can do is define the function outside the class. Then use it where ever you want. Like this:
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
void startRecord(){
.
.
.
} /// Like here outside the class
class _MyAppState extends State<MyApp> {
String statusText = "";
bool isComplete = false;
#override
Widget build(BuildContext context) {
return MaterialApp(.....
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return MaterialApp(
onPressed: () {
startRecord(); // call Here as follows.
}),
}