我身上有一个Flare字符,当我更新节点时,我必须按其节点显示一些项目,并且该项目在角色上显示,这在我的ChimpCharacter
类中花费了一些时间并且被延迟了每当我通过点击项目选择项目时,整个类都会更新,并且其initState会再次调用,我使用ScopedModel
来更新状态。
这是我正在使用的耀斑人物的链接
https://drive.google.com/file/d/14hI_cezteapxiF8xDT6w-DrcIwPIhb8n/view?usp=sharing
我的main.dart
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
Item item1 = Item('brac1');
Item item2 = Item('glasses1');
return MaterialApp(
home: ScopedModel<AppModel>(
model: AppModel(),
child: Scaffold(
body: Container(
child: Column(
children: <Widget>[
Expanded(
flex: 6,
child: ScopedModelDescendant<AppModel>(
builder: (context, child, model) {
return ChimpCharacter(
itemNames: model.items,
key: UniqueKey(),
);
}),
),
Expanded(
flex: 4,
child: ScopedModelDescendant<AppModel>(
builder: (context, child, model) {
return Wrap(
spacing: 100.0,
children: <Widget>[
RaisedButton(
color: Colors.blue,
elevation: 3.0,
onPressed: () {
model.addItem(item1);
print('model is ${model.items}');
},
child: Text('Braclet'),
),
RaisedButton(
color: Colors.blue,
elevation: 3.0,
onPressed: () {
model.addItem(item2);
print('model is ${model.items}');
},
child: Text('Glasses'),
),
],
);
},
),
),
],
),
),
),
),
);
}
}
我的model.dart
import 'package:scoped_model/scoped_model.dart';
class AppModel extends Model {
List<Item> _items = [];
List<Item> get items => _items;
void addItem(Item item) {
this._items = items;
if (_items.contains(item)) {
_items.remove(item);
} else {
_items.add(item);
}
notifyListeners();
}
}
class Item {
String itemName;
Item(this.itemName);
}
我的chimpcharacter.dart
class ChimpCharacter extends StatefulWidget {
ChimpCharacter({Key key, this.itemNames}) : super(key: key);
final List<Item> itemNames;
@override
_ChimpCharacterState createState() => _ChimpCharacterState();
}
class _ChimpCharacterState extends State<ChimpCharacter>
implements FlareController {
List<String> items = [];
FlareController flareController;
@override
void initState() {
super.initState();
print('initState called ');
if (widget.itemNames != null) {
widget.itemNames.map((s) {
items.add(s.itemName);
}).toList(growable: false);
}
}
@override
Widget build(BuildContext context) {
return FlareActor(
'assets/chimp.flr',
alignment: Alignment.center,
fit: BoxFit.contain,
animation: 'walking',
controller: this,
);
}
@override
bool advance(FlutterActorArtboard artboard, double elapsed) {
// print('advance');
if (items != null) {
items.forEach((String item) {
final ActorNode acceName = artboard.getNode(item);
acceName.collapsedVisibility = false;
});
}
return false;
}
@override
void initialize(FlutterActorArtboard artboard) {}
@override
void setViewTransform(Mat2D viewTransform) {
print('setViewTransform');
}
@override
ValueNotifier<bool> isActive;
}