伙计们,我从flutter开始,我决定制作一个类似"饼干点击器"的迷你游戏来训练,但我刚刚遇到了国家管理的问题。。。
我使用一个提供者来传播"游戏控制器",它旨在更新玩家的资源并进行计算。
我设法让"GameController"无处不在,但当我与一个资源("游戏控制器"中的对象(交互时,这个是最新的,但不在UI中,它不会改变。。。
main.dart:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'controllers/GameController.dart';
void main() => runApp(MokaOnline());
class MokaOnline extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => GameController()),
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource =
gameControler.essentialResourceController.mokaResource;
return Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
);
},
);
}
}
GameController.dart:
import 'package:flutter/foundation.dart';
import 'package:mokaonline/controllers/EssentialResourceController.dart';
class GameController {
EssentialResourceController _essentialResourceController =
EssentialResourceController();
EssentialResourceController get essentialResourceController =>
_essentialResourceController;
}
EssentialResourceController.dart:
import 'package:mokaonline/models/essentialResources/MokaResourceModel.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class EssentialResourceController {
ResourceEssentialInterface _mokaResource = MokaResourceModel();
ResourceEssentialInterface get mokaResource => _mokaResource;
}
MokaResourceModel.dart:
import 'package:flutter/foundation.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class MokaResourceModel extends ChangeNotifier
implements ResourceEssentialInterface {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
我知道UI没有刷新,因为它不是真正更新的"游戏控制器"。。。但我不知道如何以其他方式构建代码。
不管怎样,谢谢你的病人,祝你今天愉快!
您正在提供程序中寻找类似嵌套对象的东西
类似Flutter提供程序嵌套对象
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource = gameControler.essentialResourceController.mokaResourceModel;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {_MokaResourceModel = MokaResourceModel();}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
有问题,但可能您发现错误
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider2<MokaResourceModel, WoodResource, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, wood, essR) {
essR..mokaResourceModel=moka;
essR..woodResource=wood;
},
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _moka = gameControler.essentialResourceController.mokaResourceModel;
final _wood = gameControler.essentialResourceController.woodResource;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_moka.getResource()}'),
FlatButton(
onPressed: () {
_moka.increment();
},
child: Text('Add Moka'),
),
new Container(height: 100.0,),
Text('Wood : ${_wood.getResource()}'),
FlatButton(
onPressed: () {
_wood.increment();
},
child: Text('Add Wood'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {
_MokaResourceModel = MokaResourceModel();
_WoodResource = WoodResource();
}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
WoodResource _WoodResource = WoodResource();
WoodResource get woodResource => _WoodResource;
set woodResource(WoodResource value)
{
_WoodResource = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
class WoodResource extends ChangeNotifier {
int _wood = 0;
void increment() {
_wood++;
notifyListeners();
}
@override
int getResource() {
return _wood;
}
}
这也不是更好的
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<WoodResource, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, wood, essR) => essR..woodResource=wood
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)