How to set the state of a stateful widget from a child stateless widget - flutter

Okay, so just to warn you, I'm 15 and I'm a complete flutter noob. This is my first ever project, so excuse the probably dumb question, and please go easy on me.
I have this stateful widget (ride) where the body is one of the child stateless widgets defined in _children. The if statement just changes between the 1st and 2nd child widgets depending on if the user has connected a BT device (that part is irrelevant).
What I need to do is set the state from the inside of the MaterialButton found on ln 68 so that ride shows the riding stateless widget, but obviously I can't change the state from inside startRide because it's a stateless widget. How would I go about doing this?
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' ;
import 'results.dart';
import 'settings.dart';
class ride extends StatefulWidget {
#override
_rideState createState() => _rideState();
}
class _rideState extends State<ride> {
int _currentState = 0;
final List<Widget> _children = [
notConnected(),
startRide(),
riding(),
];
bool connected = checkBT(); // Function defined in settings.dart
#override
Widget build(BuildContext context) {
if (connected == true){
_currentState = 1;
setState((){_currentState;});
}
return _children[_currentState];
}
}
class notConnected extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height:180,
padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
child: Center(
child: Text(
"Oops! Looks like your phone isn’t connected to your bluetooth device.",
style:Theme.of(context).textTheme.bodyText2,
textAlign: TextAlign.center,
),
),
),
);
}
}
class startRide extends StatelessWidget {
AudioPlayer _audioPlayer = AudioPlayer();
AudioCache player = AudioCache();
#override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child: Container(
width: 200,
height: 80,
child: MaterialButton(
onPressed:(){
player.play("beeps.mp3");
// I NEED TO SET THE STATE OF RIDE HERE
},
child: Text(
"Start!",
style: Theme.of(context).textTheme.headline1,
),
color: Colors.red[500],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
),
),
),
);
}
}
class riding extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(); //not finished writing this yet
}
}
I'm probably going about doing this in completely the wrong way, but I've come from python so it's very different. Any help would be greatly appreciated :)

You can create callback, i.e passing function
Here is a sample code
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' ;
import 'results.dart';
import 'settings.dart';
class ride extends StatefulWidget {
#override
_rideState createState() => _rideState();
}
class _rideState extends State<ride> {
int _currentState = 0;
final List<Widget> _children = [
notConnected(),
startRide((){
// you can setState((){}) here
}),
riding(),
];
bool connected = checkBT(); // Function defined in settings.dart
#override
Widget build(BuildContext context) {
if (connected == true){
_currentState = 1;
setState((){_currentState;});
}
return _children[_currentState];
}
}
class notConnected extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height:180,
padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
child: Center(
child: Text(
"Oops! Looks like your phone isn’t connected to your bluetooth device.",
style:Theme.of(context).textTheme.bodyText2,
textAlign: TextAlign.center,
),
),
),
);
}
}
class startRide extends StatelessWidget {
AudioPlayer _audioPlayer = AudioPlayer();
AudioCache player = AudioCache();
Function callback;
startRide(Function callback){
this.callback = callback;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child: Container(
width: 200,
height: 80,
child: MaterialButton(
onPressed:(){
player.play("beeps.mp3");
// I NEED TO SET THE STATE OF RIDE HERE
// callback function
callback();
},
child: Text(
"Start!",
style: Theme.of(context).textTheme.headline1,
),
color: Colors.red[500],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
),
),
),
);
}
}
class riding extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(); //not finished writing this yet
}
}
Edit :- Test code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Test(),
);
}
}
class Test extends StatefulWidget {
#override
_Test createState() => _Test();
}
class _Test extends State<Test> {
int current = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
body: root(),
);
}
Widget root() {
return Container(
child: TestingStateless((){
setState(() {
current++;
print(current);
});
}),
);
}
}
// ignore: must_be_immutable
class TestingStateless extends StatelessWidget{
Function func;
TestingStateless(Function func){
this.func = func;
}
#override
Widget build(BuildContext context) {
return InkWell(
onTap: (){
func();
},
child: Container(
height: 50,
color: Colors.green,
child: Text('TESTING'),
),
);
}
}

Related

How to change scaffold colour randomly on clicking separate file's gesturdetector button

I have made two files... one is main.dart and other is homescreen.dart. Homescreen is for scaffold body which is created separately. Now there is a button in home screen for changing colour of scaffold. How to do this?
The main purpose is to know access scaffold from other stateful widget class file...
main.dart
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(child: Scaffold(body: HomeScreen(),)),
);
}
}
homescreen.dart
class _HomeScreenState extends State<HomeScreen> {
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
//My query is to PLACE CODE HERE TO CHANGE SCAFFOLD COLOR ON CLICKING
},
child: Center(
child: Container(
color: Colors.red,
height: 60,
width: 200,
child: Center(child: Text('Change Color',)),
),
),
);
}
}
Try this:
main.dart
import 'package:flutter/material.dart';
import 'home.dart';
import 'dart:math' as math;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Color _color = Colors.white;
void changeColor(){
setState(() {
_color = Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(child: Scaffold(
backgroundColor: _color,
body: HomeScreen(changeColor: changeColor,),)),
);
}
}
home.dart
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
VoidCallback? changeColor;
HomeScreen({Key? key, this.changeColor}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: changeColor,
child: Center(
child: Container(
color: Colors.red,
height: 60,
width: 200,
child: const Center(
child: Text(
'Change Color',
),
),
),
),
);
}
}
You can do it like this :
/// EDIT :
I edit it to get the Color random
import 'dart:math' as math;
class _MyAppState extends State<MyApp> {
Color _newColor = Colors.white; // variable with the color you want to change
final rnd = math.Random(); // random
Color getRandomColor() =>
Color(rnd.nextInt(0xffffffff)); // little function to get the color random
void _changeNewColor() { // function that you are going to send to yout HomeScreen
setState(() {
_newColor = getRandomColor();
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: HomeScreen(change: _changeNewColor), // function
backgroundColor: _newColor, // here the variable
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({
Key? key,
this.change,
}) : super(key: key);
final Function()? change; // instance and get the funct
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: change,
child: Center(
child: Container(
color: Colors.red,
height: 60,
width: 200,
child: const Center(
child: Text(
'Change Color',
)),
),
),
);
}
}

Error: The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.'Function' is from 'dart:core'.onPressed: selectHandler

main.dart
import 'package:flutter/material.dart';
import './questions.dart';
import './answer.dart';
void main() {
runApp(AskMe());
}
class AskMe extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _AskMeState();
}
}
class _AskMeState extends State<AskMe> {
var _next_ques = 0;
void _Response() {
setState(() {
_next_ques += 1;
});
print(_next_ques);
}
#override
Widget build(BuildContext context) {
var questions = ["What is your Name ?", "What is your favourite color ?"];
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Ask Me"),
),
body: Column(
children: [
Questions(questions[_next_ques]),
Answer(_Response),
Answer(_Response),
Answer(_Response),
],
),
),
);
}
}
questions.dart
import 'package:flutter/material.dart';
class Questions extends StatelessWidget {
final String questions;
Questions(this.questions);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(20),
child: Text(
questions,
style: TextStyle(fontSize: 28),
textAlign: TextAlign.center,
),
);
}
}
answer.dart
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final Function selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: RaisedButton(
color: Colors.blueGrey,
child: Text("Question 1"),
onPressed: selectHandler,
),
);
}
}
The above code is for basic app that consists of 3 buttons for the questions shown as textview and clicking on button will display next question. The issue i'm facing is while making custom dart function in answer.dart i'm not able to call the pointer to the function _Response which is present in main.dart as it gives the error mentioned in the title. Thanks for the help.
Define function type like this:
class Answer extends StatelessWidget {
final Function() selectHandler;
...

Instantiate child button with given value, but it is not changed

I am learning new Dart/Flutter and try to make my own exercise. I believe I am missing something fundamental here.
Problem:
My button is showing question mark. But I instantiate it with value already
Question:
How to instantiate button with given text?
import 'package:complete_guide/question.dart';
import 'package:flutter/material.dart';
import 'answer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int index = 0;
List<String> questions = [
"How may I help you?",
"What is you favorite dog?"
];
void _answerQuestion(){
setState(() {
index = index + 1;
});
print(index);
}
void showText(inputText){
print(inputText);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Stay Strong")
),
body: Center(
child: Column(
children: [
Question(questions[index]),
Answer("iddqd", _answerQuestion),
Answer("idkfa", _answerQuestion),
Answer("show me the money", _answerQuestion),
],
),
),
)
);
}
}
class Answer extends StatelessWidget {
final Function selectHandler;
String answerText = "?";
Answer(answerText, this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
child: Text(answerText),
onPressed: selectHandler,
)
);
}
}
class Question extends StatelessWidget {
final String questionText;
Question(this.questionText);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: Text(
this.questionText,
style: TextStyle(fontSize: 20, color: Colors.orange),
textAlign: TextAlign.center,
),
);
}
}
Please do not ignore analyzer warnings. They are there for a reason.
First, answerText should be marked as final. It should not be initialized so that it can be assigned a value in the constructor. Finally, you need to use the this. syntax that you've used for all of your other constructor parameters.
There is also no need of providing a default value of '?' for answerText because the constructor uses it as a required parameter, so that default value will never be used.
class Answer extends StatelessWidget {
final Function selectHandler;
final String answerText;
Answer(this.answerText, this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
child: Text(answerText),
onPressed: selectHandler,
)
);
}
}

I am using notifyListner from flutter Provider package but my UI is not updating

I am using notifyListner from flutter Provider package but my UI is not updating whenever I am typing the letters in the TextField. I am making this app just to understand how Provider works. My appbar and text is supposed to change whenever I type the text in TextFiled. Here's my code,
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Data>(
create: (context) => Data(),
child: MaterialApp(
home: Scaffold(
appBar: AppBar(
title: MyAppBar(),
),
body: SafeArea(
child: Column(
children: [
MyTextField(),
Expanded(
child: MyTextWidget(),
),
],
),
),
),
),
);
}
}
class MyTextField extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: TextField(
onChanged: (newValue) {
Provider.of<Data>(context, listen: true).changeString(newValue);
},
),
);
}
}
class MyTextWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Text(Provider.of<Data>(context, listen: true).appName),
);
}
}
class MyAppBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Text(Provider.of<Data>(context, listen: true).appName);
}
}
class Data extends ChangeNotifier {
String appName = 'Understanding Provider';
void changeString(String newString) {
appName = newString;
notifyListeners();
}
}
Please somebody help, Thanks!
You should not listen to your provider when you update it inside MyTextField:
class MyTextField extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: TextField(
onChanged: (newValue) {
Provider.of<Data>(context, listen: false).changeString(newValue);
},
),
);
}
}
Set listen: false.

Call a setState of a statefull widget from the stateless widget

I have a stateless widget class that has a widget whose movements need to be tracked. I cannot keep this widget inside the stateful widgets as I don't want the state of this widget to be refreshed.
I have the following code.
import 'package:flutter/material.dart';
import 'package:control_pad/control_pad.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Column(
children: <Widget>[
Expanded(
child: JoystickView(
onDirectionChanged: (degree, direction) {
//Change the state here.
},
),
),
Expanded(
child: MyStateFull(),
),
],
),
);
}
}
class MyStateFull extends StatefulWidget {
#override
_MyStateFullState createState() => _MyStateFullState();
}
class _MyStateFullState extends State<MyStateFull> {
double degree = 10;
double direction = 10;
//Call this from the stateless Widget
void changedDirection(degree, direction) {
setState(() {
this.degree = degree;
this.direction = direction;
});
}
#override
Widget build(BuildContext context) {
return Container(
child: Text(
"The degree Moved is $degree and the direction is $direction",
style: TextStyle(fontSize: 25, color: Colors.black),
),
);
}
}
This code produces the following output.
I want the direction and degree values to be changed as the joystick is moved.
Thank You.
I tried it myself and found the solution. This can be done using streams. I will post the code just in case someone needs it in the future.
import 'package:flutter/material.dart';
import 'package:control_pad/control_pad.dart';
class MyStateLess extends StatelessWidget {
StreamController<List<double>> _controller = StreamController<List<double>>();
GlobalKey<_MyStateFullState> statefulKey = new GlobalKey<_MyStateFullState>();
#override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
JoystickView(
onDirectionChanged: (degree, direction) {
List<double> temp = new List<double>();
temp.add(degree);
temp.add(direction);
_controller.add(temp);
},
),
MyStateFull(stream: _controller.stream, key: statefulKey),
],
);
}
}
class MyStateFull extends StatefulWidget {
final Stream<List<double>> stream;
MyStateFull({Key key, #required this.stream}) : super(key: key);
#override
_MyStateFullState createState() => _MyStateFullState();
}
class _MyStateFullState extends State<MyStateFull> {
double _degree = 0.0;
double _direction = 0.0;
#override
void initState() {
super.initState();
widget.stream.listen((event) {
setState(() {
_degree = event[0];
_direction = event[1];
});
});
}
#override
Widget build(BuildContext context) {
return Container(
child: Text("$_degree, $_direction"),
);
}
}