Flutter:跨多个屏幕发送数据

时间:2019-12-22 10:30:21

标签: flutter dart

我有3个小部件MyApp WidgetHome WidgetSliver Appbar Widget,它们相互连接。示例 MyApp小部件-> 家庭小部件-> SliverAppbar小部件

我的问题是,如何将数据从我的应用程序小部件直接传递到 SliverAppBar小部件

enter image description here

我发现我认为可以解决我的情况的Inherited Widget。但是我很迷糊地使用此小部件。

我已经尝试将Inherited Widget用作此类文档:

MyApp小部件

class SettingsApp extends InheritedWidget {
  SettingsApp({Key key, this.isDarkMode = false, Widget child})
      : super(key: key, child: child);

  final bool isDarkMode;

  static SettingsApp of(BuildContext context) {
    return (context.dependOnInheritedWidgetOfExactType<SettingsApp>());
  }

  @override
  bool updateShouldNotify(SettingsApp oldWidget) {
    return true;
  }
}

SliverAppBar小部件

class SliverAppBarCustom extends StatelessWidget {
  final Box detbBox = Hive.box("debt_box");
  final UserModelHive userModelHive = Hive.box("user_box").get("userSession");
  @override
  Widget build(BuildContext context) {
    final isDarkMode =
        context.dependOnInheritedWidgetOfExactType<SettingsApp>().isDarkMode;
    print(isDarkMode.toString());
    var mediaQuery = MediaQuery.of(context);
    var textTheme = Theme.of(context).textTheme;
     return Text(isDarkMode.toString());
  }
}

但是我得到这个错误:

日志

The following NoSuchMethodError was thrown building SliverAppBarCustom(dirty):
The getter 'isDarkMode' was called on null.
Receiver: null
Tried calling: isDarkMode

1 个答案:

答案 0 :(得分:0)

使用ScopedModel

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

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

class SettingsModel extends Model {
  bool _isDarkMode;

  SettingsModel({bool isDarkMode}) : _isDarkMode = isDarkMode ?? false;

  bool get isDarkModel => _isDarkMode;

  set isDarkModel(bool value) {
    _isDarkMode = value;
    notifyListeners();
  }

  void switchTheme() {
    _isDarkMode = !_isDarkMode;
    notifyListeners();
  }

  static SettingsModel of(BuildContext context) {
    return ScopedModel.of<SettingsModel>(context);
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel<SettingsModel>(
      model: SettingsModel(isDarkMode: true),
      child: MaterialApp(
        home: InitPage(),
      ),
    );
  }
}

class InitPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Init Page")),
      body: SizedBox.expand(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ScopedModelDescendant<SettingsModel>(
              builder: (context, child, model) {
                return Text('Is Dark Mode: ${model.isDarkModel}');
              },
            ),
            RaisedButton(
              child: Text("Next Page"),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SecondPage(),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Page"),
      ),
      body: SizedBox.expand(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ScopedModelDescendant<SettingsModel>(
              builder: (context, child, model) {
                return Text('Is Dark Mode: ${model.isDarkModel}');
              },
            ),
            RaisedButton(
              child: Text("Switch Theme"),
              onPressed: SettingsModel.of(context).switchTheme,
            ),
          ],
        ),
      ),
    );
  }
}

重要提示:没有_isDarkModel的情况下,您不应更改notifyListeners()。如果这样做,则UI可能不会更新。