Flutter 如何将继承的小部件传递到整个Material应用程序
所以我有一个继承的小部件,看起来像:Flutter 如何将继承的小部件传递到整个Material应用程序,flutter,Flutter,所以我有一个继承的小部件,看起来像: class InheritedStateWidget extends StatefulWidget { final Widget child; InheritedStateWidget({ @required this.child }); @override InheritedStateWidgetState createState() => new InheritedStateWidgetState(); sta
class InheritedStateWidget extends StatefulWidget {
final Widget child;
InheritedStateWidget({
@required this.child
});
@override
InheritedStateWidgetState createState() => new InheritedStateWidgetState();
static of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(_MyInheritedWidget) as _MyInheritedWidget).data;
}
}
class InheritedStateWidgetState extends State<InheritedStateWidget> {
String _userName;
// Getter methods
String get tasks => _userName;
void changeUserName(String name) {
setState(() {
_userName = name;
});
}
@override
Widget build(BuildContext context) {
return new _MyInheritedWidget(
data: this,
child: widget.child,
);
}
}
class _MyInheritedWidget extends InheritedWidget {
final InheritedStateWidgetState data;
_MyInheritedWidget({
Key key,
this.data,
Widget child}): super(key: key, child: child);
@override
bool updateShouldNotify(_MyInheritedWidget old) {
return true;
}
}
类继承StateWidget扩展StatefulWidget{
最后一个孩子;
InheritedStateWidget({
@需要这个孩子
});
@凌驾
InheritedStateWidgetState createState()=>新的InheritedStateWidgetState();
静态的(构建上下文){
返回(context.inheritFromWidgetOfExactType(_MyInheritedWidget)作为_MyInheritedWidget);
}
}
类InheritedStateWidgetState扩展了状态{
字符串\u用户名;
//吸气剂法
字符串get tasks=>\u用户名;
void changeUserName(字符串名称){
设置状态(){
_用户名=名称;
});
}
@凌驾
小部件构建(构建上下文){
返回新的\u MyInheritedWidget(
资料:这,,
child:widget.child,
);
}
}
类_MyInheritedWidget扩展InheritedWidget{
最终继承的StateWidgetState数据;
_MyInheritedWidget({
关键点,
这个数据,,
Widget child}):超级(key:key,child:child);
@凌驾
bool updateShouldNotify(\u MyInheritedWidget old){
返回true;
}
}
还有一个像这样的主镖:
void main() => runApp(InheritedStateWidget(
child: new Builder ( builder: (context) => new MyApp()))
);
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'App One',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final PageStorageBucket bucket = PageStorageBucket();
int currentTab;
Widget currentPage;
List<Widget> pages;
HomePage one;
ProfilePage two;
@override
void initState() {
super.initState();
one = HomePage(
key: exploreKey,
);
two = Profile(
key: myTasksKey,
);
pages = [one, two];
currentPage = one;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Testing"),),
body: new PageStorage(
bucket: bucket,
child: currentPage,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentTab,
onTap: (int index) {
setState(() {
currentTab = index;
currentPage = pages[index];
});
},
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem> [
BottomNavigationBarItem(
title: Text("Home"),
icon: null
),
BottomNavigationBarItem(
title: Text("Profile"),
icon: null
)
],
)
);
}
}
void main()=>runApp(InheritedStateWidget(
子级:新建生成器(生成器:(上下文)=>new MyApp())
);
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“应用程序一”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
final PageStorageBucket bucket=PageStorageBucket();
int currentTab;
窗口小部件当前页面;
列表页;
首页一;
简介第二页;
@凌驾
void initState(){
super.initState();
一=主页(
钥匙:探索基,
);
二=外形(
关键字:myTasksKey,
);
页码=[1,2];
当前页面=一个;
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:new appBar(标题:新文本(“测试”),),
正文:新页面存储(
桶:桶,
孩子:当前页面,
),
底部导航栏:底部导航栏(
currentIndex:currentTab,
onTap:(int索引){
设置状态(){
currentTab=索引;
currentPage=页面[索引];
});
},
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
标题:文本(“主页”),
图标:空
),
底部导航气压计(
标题:文本(“简介”),
图标:空
)
],
)
);
}
}
根据我从一些指南中得到的信息,这是我应该如何将继承的小部件传递到我的Material应用程序及其所有路径,但它似乎没有将继承的小部件传递到脚手架上。从脚手架上看,我似乎可以做如下事情:
InheritedStateWidget.of(上下文)
它将成功地带回我继承的小部件,但在当前页面小部件中,即主页(有状态小部件)。我似乎无法访问它,而我
未处理的异常:NoSuchMethodError:类“HomePageState”已存在
没有实例获取程序“InheritedStateWidget”。接收者:实例
“主页状态”
我做错了什么?我稍微调整了你的前几门课:
import 'package:flutter/material.dart';
void main() => runApp(InheritedStateWidget());
class InheritedStateWidget extends StatefulWidget {
@override
InheritedStateWidgetState createState() => InheritedStateWidgetState();
}
class InheritedStateWidgetState extends State<InheritedStateWidget> {
String _userName;
// Getter methods
String get tasks => _userName;
void changeUserName(String name) {
setState(() {
_userName = name;
});
}
@override
Widget build(BuildContext context) {
return new MyInheritedWidget(
data: this,
child: MyApp(),
);
}
}
class MyInheritedWidget extends InheritedWidget {
final InheritedStateWidgetState data;
MyInheritedWidget({Key key, this.data, Widget child})
: super(key: key, child: child);
static MyInheritedWidget of(BuildContext context) =>
context.inheritFromWidgetOfExactType(MyInheritedWidget);
@override
bool updateShouldNotify(MyInheritedWidget old) => true;
}
class MyApp extends StatelessWidget {
// in MyApp and below you can refer to MyInheritedWidget.of(context).data
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'App One',
home: new MyHomePage(),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(InheritedStateWidget());
类继承的StateWidget扩展了StatefulWidget{
@凌驾
InheritedStateWidgetState createState()=>InheritedStateWidgetState();
}
类InheritedStateWidgetState扩展了状态{
字符串\u用户名;
//吸气剂法
字符串get tasks=>\u用户名;
void changeUserName(字符串名称){
设置状态(){
_用户名=名称;
});
}
@凌驾
小部件构建(构建上下文){
返回新的MyInheritedWidget(
资料:这,,
子项:MyApp(),
);
}
}
类MyInheritedWidget扩展了InheritedWidget{
最终继承的StateWidgetState数据;
MyInheritedWidget({Key-Key,this.data,Widget-child})
:super(key:key,child:child);
的静态MyInheritedWidget(BuildContext上下文)=>
inheritFromWidgetOfExactType(MyInheritedWidget);
@凌驾
bool updateShouldNotify(MyInheritedWidget old)=>true;
}
类MyApp扩展了无状态小部件{
//在MyApp和下面,您可以参考MyInheritedWidget.of(context).data
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“应用程序一”,
主页:新建MyHomePage(),
);
}
}
我更喜欢像这样加强状态和继承小部件之间的接口,但只是一个建议
void main() => runApp(new Controller());
class Controller extends StatefulWidget {
@override
State<StatefulWidget> createState() => ControllerState();
}
typedef void VoidIntFunction(int i);
class ControllerState extends State<Controller> {
String someString = '';
void someFunction(int v) {
print('function called with $v');
}
@override
Widget build(BuildContext context) {
return StateContainer(
someString: someString,
someFunction: someFunction,
child: new ExampleApp(),
);
}
}
class StateContainer extends InheritedWidget {
final String someString;
final VoidIntFunction someFunction;
const StateContainer({
this.someString,
this.someFunction,
Widget child,
}) : super(child: child);
static StateContainer of(BuildContext context) =>
context.inheritFromWidgetOfExactType(StateContainer);
@override
bool updateShouldNotify(StateContainer oldWidget) => true;
}
void main()=>runApp(新控制器());
类控制器扩展StatefulWidget{
@凌驾
State createState()=>ControllerState();
}
typedef void VoidIntFunction(inti);
类ControllerState扩展状态{
字符串someString='';
void函数(int v){
print($v调用的函数);
}
@凌驾
小部件构建(构建上下文){
返回状态容器(
someString:someString,
someFunction:someFunction,
子项:新的ExampleApp(),
);
}
}
类StateContainer扩展了InheritedWidget{
最后一个字符串someString;
最终函数someFunction;
常量状态容器({
这个,什么字符串,
这个功能,,
孩子,
}):super(child:child);
的静态StateContainer(BuildContext上下文)=>
继承自WidgeToFexactType(StateContainer);
@凌驾
bool updateShouldNotify(StateContainer oldWidget)=>true;
}
现在使用InheritedWidget的任何子级都只有对状态(someString)的读取权限<