Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何从导航抽屉颤振重置任何类的状态?_Dart_Flutter - Fatal编程技术网

Dart 如何从导航抽屉颤振重置任何类的状态?

Dart 如何从导航抽屉颤振重置任何类的状态?,dart,flutter,Dart,Flutter,我正在编写一个具有导航抽屉的应用程序,每当我尝试从导航抽屉打开活动时,我的类第一次成功加载并提供所需的输出,因为我已经在initState方法中编写了从库中选择图像的代码,它允许我第一次从库中选择图像。但是,当我再次单击导航抽屉中的同一选项卡时,它不会重置此类的状态,每次再次单击导航抽屉中的同一选项卡时,我都需要访问图像拾取代码。这是我的密码 导航抽屉类 _getDrawerItemWidget(int pos, String title) { switch (pos) { cas

我正在编写一个具有导航抽屉的应用程序,每当我尝试从导航抽屉打开活动时,我的类第一次成功加载并提供所需的输出,因为我已经在
initState
方法中编写了从库中选择图像的代码,它允许我第一次从库中选择图像。但是,当我再次单击导航抽屉中的同一选项卡时,它不会重置此类的状态,每次再次单击导航抽屉中的同一选项卡时,我都需要访问图像拾取代码。这是我的密码

导航抽屉类

  _getDrawerItemWidget(int pos, String title) {



switch (pos) {
  case 0:
    if(title.contains("From Gallery"))

          return new TextRobo();
    else
      return new TranslateLangue();

    break;
  case 1:
    return new BarCodeRobo();
  case 2:
    return new TranslateLangue();

  default:
    return new Text("Error");
}
}




return new Scaffold(
appBar: new AppBar(
  iconTheme: new IconThemeData(color: Colors.white),
  title: new Text("App title",
    style: new TextStyle(color: Colors.white),),
),

  drawer: new Drawer(
    child: new ListView(
      children: <Widget>[
        new Container( height: 140.0, color: Colors.orange,
        child: new Center(child:
          new Text('App title', style: new TextStyle(color: Colors.white,
          fontSize:25.0, fontWeight: FontWeight.bold),
          ),
        ),
        ),
        new Column(
            children: drawerOptions)
      ],
    ),

  ),
  body: _getDrawerItemWidget(_selectedDrawerIndex, fragment_class  ),
);
}
}
class TextRobo extends StatefulWidget {

TextRobo1()
{

  createState();
}
  @override
 _TextRoboState createState() => new _TextRoboState();
}

class _TextRoboState extends State<TextRobo> {



 File _imageFile;



 @override
 void initState() {
 // TODO: implement initState
  super.initState();
  _getAndScanImage();

  }

  Future<void> _getAndScanImage() async {
   setState(() {
    _imageFile = null;

  });

  final File imageFile =
  await ImagePicker.pickImage(source: ImageSource.gallery);


 setState(() {
   _imageFile = imageFile;
  });
}

}
\u getDrawerItemWidget(int-pos,字符串标题){
开关(pos){
案例0:
如果(标题包含(“来自画廊”))
返回新的TextRobo();
其他的
返回新的TranslateLangue();
打破
案例1:
返回新的BarCodeRobo();
案例2:
返回新的TranslateLangue();
违约:
返回新文本(“错误”);
}
}
归还新脚手架(
appBar:新的appBar(
iconTheme:new IconThemeData(颜色:Colors.white),
标题:新文本(“应用程序标题”,
样式:新文本样式(颜色:Colors.white),),
),
抽屉:新抽屉(
子:新列表视图(
儿童:[
新容器(高度:140.0,颜色:Colors.orange,
儿童:新中心(儿童:
新文本(“应用程序标题”,样式:新文本样式(颜色:Colors.white,
fontSize:25.0,fontWeight:fontWeight.bold),
),
),
),
新专栏(
儿童:抽屉(续)
],
),
),
正文:_getDrawerItemWidget(_selectedDrawerIndex,fragment_class),
);
}
}
TextRobo类

  _getDrawerItemWidget(int pos, String title) {



switch (pos) {
  case 0:
    if(title.contains("From Gallery"))

          return new TextRobo();
    else
      return new TranslateLangue();

    break;
  case 1:
    return new BarCodeRobo();
  case 2:
    return new TranslateLangue();

  default:
    return new Text("Error");
}
}




return new Scaffold(
appBar: new AppBar(
  iconTheme: new IconThemeData(color: Colors.white),
  title: new Text("App title",
    style: new TextStyle(color: Colors.white),),
),

  drawer: new Drawer(
    child: new ListView(
      children: <Widget>[
        new Container( height: 140.0, color: Colors.orange,
        child: new Center(child:
          new Text('App title', style: new TextStyle(color: Colors.white,
          fontSize:25.0, fontWeight: FontWeight.bold),
          ),
        ),
        ),
        new Column(
            children: drawerOptions)
      ],
    ),

  ),
  body: _getDrawerItemWidget(_selectedDrawerIndex, fragment_class  ),
);
}
}
class TextRobo extends StatefulWidget {

TextRobo1()
{

  createState();
}
  @override
 _TextRoboState createState() => new _TextRoboState();
}

class _TextRoboState extends State<TextRobo> {



 File _imageFile;



 @override
 void initState() {
 // TODO: implement initState
  super.initState();
  _getAndScanImage();

  }

  Future<void> _getAndScanImage() async {
   setState(() {
    _imageFile = null;

  });

  final File imageFile =
  await ImagePicker.pickImage(source: ImageSource.gallery);


 setState(() {
   _imageFile = imageFile;
  });
}

}
class TextRobo扩展StatefulWidget{
TextRobo1()
{
createState();
}
@凌驾
_TextRoboState createState()=>新建_TextRoboState();
}
类_TextRoboState扩展状态{
文件imageFile;
@凌驾
void initState(){
//TODO:实现initState
super.initState();
_getAndScanImage();
}
Future\u getAndScanImage()异步{
设置状态(){
_imageFile=null;
});
最终文件映像文件=
等待ImagePicker.pickImage(来源:ImageSource.gallery);
设置状态(){
_imageFile=imageFile;
});
}
}

有几种不同的方法可以实现这一点。但首先,你跟随了吗?它是。。。。不太好

主要的问题是,它没有使用Flatter的内置导航机制,而是更改了现有页面。这意味着你不能利用颤振为你做的许多事情

至于为什么initState只被调用一次,很可能是因为flatter智能地为您缓存小部件。因为即使创建了一个新实例,您的小部件表面上也是“相同”的,所以它不会麻烦重新创建状态,而是会以性能的名义使用现有的状态

我的建议是重新编写和重构

如果你的应用程序相对简单(例如,你可以使用抽屉在一组页面之间切换),那么应该这样做:

import 'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: pageName(Pages.red),
      routes: {
        pageName(Pages.red): (context) => PageBase(title: pageName(Pages.red), page: ColouredPage(color: Colors.red)),
        pageName(Pages.green): (context) =>
            PageBase(title: pageName(Pages.green), page: ColouredPage(color: Colors.green)),
        pageName(Pages.blue): (context) =>
            PageBase(title: pageName(Pages.blue), page: ColouredPage(color: Colors.blue)),
      },
      // not actually used, needed in case flutter started with intent specifying unsupported route
      home: Container(),
    );
  }
}

enum Pages { red, green, blue }

String pageName(Pages page) {
  switch (page) {
    case Pages.red:
      return "Red";
    case Pages.green:
      return "Green";
    case Pages.blue:
      return "Blue";
  }
}

class PageBase extends StatelessWidget {
  final String title;
  final Widget page;

  const PageBase({Key key, @required this.page, @required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: page,
      drawer: Drawer(
        child: ListView(
          children: [
            ListTile(
              title: Text(pageName(Pages.red)),
              onTap: () {
                Navigator.of(context).popAndPushNamed(pageName(Pages.red));
              },
            ),
            ListTile(
              title: Text(pageName(Pages.blue)),
              onTap: () {
                Navigator.of(context).popAndPushNamed(pageName(Pages.blue));
              },
            ),
            ListTile(
              title: Text(pageName(Pages.green)),
              onTap: () {
                Navigator.of(context).popAndPushNamed(pageName(Pages.green));
              },
            )
          ],
        ),
      ),
    );
  }
}

class ColouredPage extends StatefulWidget {
  final Color color;

  const ColouredPage({Key key, this.color}) : super(key: key);

  @override
  _ColouredPageState createState() => _ColouredPageState();
}

class _ColouredPageState extends State<ColouredPage> {
  Color color;

  @override
  void initState() {
    super.initState();
    color = Colors.white;
    new Timer(Duration(seconds: 2), () {
      setState(() {
        color = widget.color;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color,
    );
  }
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
MyAppState createState(){
返回新的MyAppState();
}
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
initialRoute:pageName(红色页),
路线:{
pageName(Pages.red):(上下文)=>PageBase(标题:pageName(Pages.red),页面:彩色页面(颜色:Colors.red)),
页面名称(绿色页面):(上下文)=>
PageBase(标题:pageName(Pages.green),页面:彩色页面(color:Colors.green)),
pageName(Pages.blue):(上下文)=>
PageBase(标题:pageName(Pages.blue),页面:彩色页面(color:Colors.blue)),
},
//未实际使用,需要在颤振开始时指定不受支持的路线
主页:Container(),
);
}
}
枚举页面{红色、绿色、蓝色}
字符串pageName(第页){
交换机(第页){
案例页。红色:
返回“红色”;
case Pages.green:
返回“绿色”;
case Pages.blue:
返回“蓝色”;
}
}
类PageBase扩展了无状态小部件{
最后的字符串标题;
最终窗口小部件页面;
const PageBase({Key Key,@required this.page,@required this.title}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(标题)),
正文:第页,
抽屉(
子:ListView(
儿童:[
列表砖(
标题:文本(pageName(Pages.red)),
onTap:(){
Navigator.of(context.popandpushname(pageName(Pages.red));
},
),
列表砖(
标题:文本(页面名称(Pages.blue)),
onTap:(){
Navigator.of(context.popandpushname(pageName(Pages.blue));
},
),
列表砖(
标题:文本(pageName(Pages.green)),
onTap:(){
Navigator.of(context.popandpushname(pageName(Pages.green));
},
)
],
),
),
);
}
}
类coloredPage扩展StatefulWidget{
最终颜色;
constcoloredpage({Key-Key,this.color}):super(Key:Key);
@凌驾
_coloredPageState createState()=>_coloredPageState();
}
类_coloredPageState扩展状态{
颜色;
@凌驾
void initState(){
super.initState();
颜色=颜色。白色;
新计时器(持续时间(秒数:2),(){
设置状态(){
color=widget.color;
});
});
}
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色,
);
}
}
如果将一个页面推到一个现有页面的顶部,然后最终返回,则可以使用
Navigator.push()
和一个处理程序来确定它何时返回,然后重新返回