I make my own widget using SingleChildRenderObjectWidget, RenderBox.
I want to update the drawing in setState() when certain actions are performed in the parent widget of this self-widget, but it may not work.
Below is the sample code.
I expect the color to change from black to red when I press the button, but it doesn't really change.
import'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extensions StatelessWidget {
@override
Widget build (BuildContext context) {
US>return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwitch:Colors.blue,
visualDensity —VisualDensity.adaptivePlatformDensity,
),
home:MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
US>class MyRenderData{
Color color;
MyRenderData(this.color);
}
class MyRenderBox extensions RenderBox {
MyRenderData;
MyRenderBox (this.data);
@override
boole get sizedByParent=>true;
@override
void performResize() {
size = constructs.biggest;
}
@override
void paint (PaintingContext context, Offset offset) {
Canvas c=context.canvas;
c.drawRect(
Rect.fromLTRB (offset.dx, offset.dy, offset.dx+100, offset.dy+100),
Paint()..color=data.color);
}
}
class MyRenderWidget extensions SingleChildRenderObjectWidget{
MyRenderData;
MyRenderWidget(this.data);
@override
RenderObject createRenderObject (BuildContext context) {
return MyRenderBox(data);
}
}
class MyHomePage extensions StatefulWidget{
MyHomePage({Key, this.title}):super(key:key);
// This widget is the home page of your application.It is stateful, meeting
// that has a State object (defined below) that contains fields that effect
// How it looks.
// This class is the configuration for the state.It holds the values (in this)
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState()=>_MyHomePageState();
}
class_MyHomePageState extensions State<MyHomePage>{
MyRenderData;
@override
void initState() {
super.initState();
This.data=MyRenderData(Colors.black);
}
@override
Widget build (BuildContext context) {
US>return Scaffold(
appBar —AppBar(
title:Text(widget.title),
),
body: Center(
child —MyRenderWidget(data),
),
floatingActionButton: FloatingActionButton(
onPressed:() {
setState((){
// This side moves.
// data.color=Colors.red;
// It doesn't work here.
This.data=MyRenderData(Colors.red);
});
},
),
);
}
}
The sample project was created as follows:
flutter create-t app render_object_test
This is a sample, so it is quite small, but
There is actually a list and a self-made widget for selecting items.
When you select an item from a list, the contents of the item appear in your own widget.
There is a similar problem with your code.
It seems that overwriting the object instead of changing it will cause this problem.
I don't understand why it works like that.
Also, if I want to update the state, is it the right implementation to change the existing instance instead of overwriting it?
< p > < code > as follows My Rent Reader Widget < / code > ni < code > Union Square I expect that the street. get your hopes up could actually hand these SAML-HolderOfKey < / code > < / >
class MyRenderWidget extensions SingleChildRenderObjectWidget{
final MyRenderData;
const MyRenderWidget(this.data, {Keykey}):super(key:key);
@override
RenderObject createRenderObject (BuildContext context) {
return MyRenderBox(data);
}
}
child:MyRenderWidget(data, key:UniqueKey()) ,
I think it will be easier to understand if you display the dart version.(Because the result depends on whether it is nullsafe or not)
There are two possibilities:
For one thing, Flutter's Widget change detection failed to detect the change.
Two errors occurred in lambda in nullsafe
setState(){
// If it's nullsafe, you can't substitute the value.
This.data=MyRenderData(Colors.red);
});
© 2024 OneMinuteCode. All rights reserved.