Flutter /如何在Statefulwidget之间传递和获取数据

时间:2018-05-09 04:05:30

标签: dart flutter

我是一名大三学生(或不到一个月的学习者)飞镖/扑动的开发者。 我尝试创建一个样本proflie应用程序 - 在主屏幕上显示用户的个人资料图像。 - 用户可以使用应用内相机拍照或从相册中选择并显示为个人资料图片。

我创建了2个主viewController。

  1. main.dart

    void main() {
     runApp(new MaterialApp(
     home: new MyApp(),
     routes: <String, WidgetBuilder>{
      '/screen1': (BuildContext context) => new MyApp(),
      '/screen3': (BuildContext context) => new UploadAvatarButton()
      },
     ));
    }
    
     class MyApp extends StatefulWidget {
      final String _images =  "";
      MyApp(String _images, {Key key}) : super(key: key);
      @override
      State<StatefulWidget> createState() {
       return new MyHomePage();
      }
     }
    
     class MyHomePage extends State<MyApp> {
     @override
     Widget build(BuildContext context) {
    
     Widget myPhoto = new Container(
     //Attribute
     child: new FlatButton(
      child: new Container(
       decoration: new BoxDecoration(
        shape: BoxShape.circle,
        image: new DecorationImage(
         fit: BoxFit.cover, image: new AssetImage('images/sample.jpg')),
        ),
       ),
     onPressed: _showPersBottomSheetCallBack,
      ),
     );
    
     return new MaterialApp(
     home: new Scaffold(
     //Header
     body: new Center(
      child: new Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          new Container(child: myPhoto),
    
  2. 和2. cameraView.dart

    class UploadAvatarButton extends StatefulWidget {
      @override
      State<StatefulWidget> createState() {
        return new MakeUploadAvatarButtonState();
      }
    class MakeUploadAvatarButtonState extends State<UploadAvatarButton> {
      File _image;
      Future getImage() async {
        var image = await ImagePicker.pickImage(source: ImageSource.camera);
        setState(() {
          _image = image;
    //Try to send data or image back to main screen
          List<int> imageBytes = image.readAsBytesSync();
          String base64Image = BASE64.encode(imageBytes);
    //send data back
          Navigator.push(context, new MaterialPageRoute(builder: (_) => 
      new MyApp(_image.)));
        });
      }
    

    如您所见,我使用方法将图像发回 从相机

    List<int> imageBytes = image.readAsBytesSync();
      String base64Image = BASE64.encode(imageBytes);
      Navigator.push(context, new MaterialPageRoute(builder: (_) => 
      new MyApp(_image.)));
    

    进入主要

    final String _images =  "";
    MyApp(String _images, {Key key}) : super(key: key);
    

    然而,我收到了错误

    [dart] The constructor returns type 'dynamic' that isn't of expected type 'Widget'.
    

    你可以告诉我吗?

    祝你好运 谢谢

1 个答案:

答案 0 :(得分:0)

将构造函数修改为

  

MyApp(this._images)

访问MyHomePage类中的图像

  

widget._images