setState not changing how to setstate StatefulWidget to StatefulWidget? - dart

StatefulWidget to StatefulWidget
How to Change String TimeSetdata in class Test2 setstate
class Test1 extends StatefulWidget {
#override
_Test1State createState() => _Test1State();
}
class _Test1State extends State<Test1> {
String TimeSetdata = "9.00 AM";
#override
Widget build(BuildContext context) {
...
Text(TimeSetdata);
}
}
class Test2 extends StatefulWidget {
#override
_Test2State createState() => _Test2State();
}
class _Test2State extends State<Test2> {
#override
Widget build(BuildContext context) {
...
onPressed: () {
setState(() {
TimeSetdata = "11.00 AM";
});
};
}
}
How to setState in Class Test2 Change String TimeSetdata in Widget Text(TimeSetdata) to "11.00 AM";

Allow the parent to pass a callback function to the child and pass a callback, when called from the child, updates the value in the parent.
This assumes that Test2 is a child of Test1 (you didn't make this clear in your question)
typedef StringCallback = void Function(String);
class Test2 extends StatefulWidget {
Test2({#required this.onPressed});
final StringCallback onPressed;
#override
_Test2State createState() => _Test2State(onPressed: onPressed);
}
class _Test2State extends State<Test2> {
_Test2State({#required this.onPressed});
final StringCallback onPressed;
#override
Widget build(BuildContext context) {
...
onPressed: () => onPressed(),
}
}
class _Test1State extends State<Test1> {
String TimeSetdata = "9.00 AM";
#override
Widget build(BuildContext context) {
...
Test2(onPressed: (s) => setState(() => TimeSetdata = s),
...
Text(TimeSetdata);
}
}

Related

is this right way to send state data from parent to child

Are there any other better ways to send data
import 'package:flutter/material.dart';
import './bottomNav.dart';
void main()=>runApp(Parent());
class Parent extends StatefulWidget {
State<StatefulWidget> createState() {
// TODO: implement createState
return _ParentState();
}
}
class _ParentState extends State<Parent>{
int count = 1;
#override
Widget build(BuildContext context) {
// TODO: implement build
return (
MaterialApp(
home:Material(
child:Center(
child:Child1(cont:count)
)
)
)
);
}
}
class Child1 extends StatelessWidget {
int cont;
Child1({this.cont});
#override
Widget build(BuildContext context) {
// TODO: implement build
print('context ${cont}');
return Text('This is child ${cont}',);
}
}
You can use InheritedModel for pass data between classes.
watch this

flutter error:The argument type 'Future<Image>' can't be assigned to the parameter type 'Widget'

i'm using flutter flame library and got this error
The argument type 'Future<Image>' can't be assigned to the parameter
type 'Widget'.
my code is:
import 'package:flutter/material.dart';
import 'package:flame/flame.dart';
class TileMap extends StatefulWidget {
#override
_TileMapState createState() => _TileMapState();
}
class _TileMapState extends State<TileMap> {
#override
Widget build(BuildContext context) {
return Container(
child: Flame.images.load('grass_05.png'),
);
}
}
how to fix it?
and what's the problem?
thank's to all
class _TileMapState extends State<TileMap> {
#override
void initState() {
super.initState();
_loadImage()
}
void _loadImage() async {
var img = await Flame.images.load('grass_05.png');
setState(() => _image = img);
}
Image _image;
#override
Widget build(BuildContext context) {
return Container(
child: _image,
);
}
}

how to send data through different classes in different screens in flutter

i was struck here while making an application my code went like this
void main() {
runApp(Myapp());
}
class Myapp extends StatelessWidget {
bool s=false;
#override
Widget build(BuildContext context) {
return (MaterialApp(
debugShowCheckedModeBanner: false,
title: "haha app",
theme: ThemeData(primarySwatch: Colors.lime),
home: s ? HomeScreen(null) : LoginPage()));
}
}
the above code is of main.dart file
and this is my another file called Login.dart and the code goes like this
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
Widget build(BuildContext context) {
return(some button ontap:(\\ on tap on this i have to change the bool s value in main.dart to true how to do that){
}
)
}
on tap the button the value s in main dart file should change to true but without navigator because we are not navigating here just a click.
please help me,
thanks in advance
You can use callbacks to communicate your widgets, like this
Create a method to get the callback , in this case : onChangeBool , pass the callback to your LoginPage Widget.
class Myapp extends StatelessWidget {
bool s=false;
onChangeBool(){
//change your var here
s = true;
//refresh the state
setState(() {
});
}
#override
Widget build(BuildContext context) {
return (MaterialApp(
debugShowCheckedModeBanner: false,
title: "haha app",
theme: ThemeData(primarySwatch: Colors.lime),
home: s ? HomeScreen(null) : LoginPage(onPressed: () => onChangeBool() ));
}
}
Receive the callBack , and call it when you press the button
class LoginPage extends StatefulWidget {
final VoidCallback onPressed;
LoginPage({this.onPressed});
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
Widget build(BuildContext context) {
return RaisedButton(
child: Text("button"),
onPressed: (){
widget.onPressed();
},
)
}
)
}
In case you want to pass Data, you can use ValueChanged callback , or if you want to pass complex data, create your own callback using typedef/
A sample using ValueChanged.
class Myapp extends StatelessWidget {
bool s=false;
receiveData(String data){
print("your text here : $data");
}
#override
Widget build(BuildContext context) {
return (MaterialApp(
debugShowCheckedModeBanner: false,
title: "haha app",
theme: ThemeData(primarySwatch: Colors.lime),
home: s ? HomeScreen(null) : LoginPage(onPressed: receiveData ));
}
}
class LoginPage extends StatefulWidget {
final ValueChanged<String> onPressed;
LoginPage({this.onPressed});
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
Widget build(BuildContext context) {
return RaisedButton(
child: Text("button"),
onPressed: (){
widget.onPressed("passing this data");
},
)
}
)
}

Scrollable Listview in Flutter with Dart

Can someone explain me where I should define a scroll controller? I have chat list view which is the body of a scrollable view. I want to be able to control the scrolling behaviour from MainView but don't know how to pass the controller down to _ChatListView. Any ideas?
mainview.dart
class MainView extends StatelessWidget {
...
// is this the correct place?
final ScrollController scrollController = ScrollController();
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new ChatListView()
);
}
}
chatlistview.dart
class ChatListView extends StatefulWidget {
#override
_ChatListView createState() => _ChatListView();
}
class _ChatListView extends State< ChatListView > {
Widget build(BuildContext context) {
return ListView.builder(
controller: scrollController,
);
}
}
Add a constructor and pass the controller as parameter
class MainView extends StatelessWidget {
...
// is this the correct place?
final ScrollController scrollController = ScrollController();
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new ChatListView(scrollController: scrollController)
);
}
}
class ChatListView extends StatefulWidget {
ChatListView({#required this.scrollController});
final ScrollController scrollController;
#override
_ChatListView createState() => _ChatListView();
}
class _ChatListView extends State< ChatListView > {
Widget build(BuildContext context) {
return ListView.builder(
controller: widget.scrollController,
);
}
}

Pass StatefulWidget data to the State class without using constructor

I managed to pass Stateful class variables' values to the State class through constructor like below:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
home: MyHomePage('John', 'Morison'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage(this.fname, this.lname);
final String fname;
final String lname;
#override
_MyHomePageState createState() => _MyHomePageState(fname, lname);
}
class _MyHomePageState extends State<MyHomePage> {
_MyHomePageState(this.fname, this.lname);
final String fname;
final String lname;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Hello $fname $lname'),
)
);
}
}
That's weird and I had to do lot of work as there is more than two variables. Is there a better way?
Yes, there is widget:
From Doc:
/// The current configuration.
///
/// A [State] object's configuration is the corresponding [StatefulWidget]
/// instance. This property is initialized by the framework before calling
/// [initState]. If the parent updates this location in the tree to a new
/// widget with the same [runtimeType] and [Widget.key] as the current
/// configuration, the framework will update this property to refer to the new
/// widget and then call [didUpdateWidget], passing the old configuration as
/// an argument.
T get widget => _widget;
T _widget;
Code should look like below:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
home: MyHomePage('John', 'Morison'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage(this.fname, this.lname);
final String fname;
final String lname;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Hello ${widget.fname} ${widget.lname}'),
)
);
}
}

Resources