【问题标题】:SingleTickerProviderStateMixin and custom mixinSingleTickerProviderStateMixin 和自定义 mixin
【发布时间】:2020-09-16 12:42:02
【问题描述】:

我对 Flutter 和 Dart 有点陌生,Google 无法帮助我解决这个问题。

说我有这个:

class _MapPageState extends BaseState<MapPage> with SingleTickerProviderStateMixin

我想在此之上添加另一个 mixin (BasePage),其中包含可重用的应用栏、抽屉等。布局在 this article 中进行了描述。

我知道这是不可能的,我的 IDE 抛出一个错误,告诉我集成它们,但我不知道如何集成。有解决办法吗?我需要SingleTickerProviderStateMixin,因为我正在使用的 AnimationController 需要它。

如果需要,这里是自定义 mixin 代码:

abstract class Base extends StatefulWidget {
  Base({Key key}) : super(key: key);
}

abstract class BaseState<Page extends Base> extends State<Page> {
  String screenName();
}

mixin BasePage<Page extends Base> on BaseState<Page> {

  MapFunctions functions = MapFunctions();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Guidey'),
          backgroundColor: Colors.deepOrangeAccent,
          centerTitle: true,
        ),
        drawer: Theme(
          data: Theme.of(context).copyWith(
            canvasColor: Colors.black.withOpacity(0.5)
          ),
          child: Drawer(
            child: ListView(
              padding: EdgeInsets.fromLTRB(40.0, 10.0, 40.0, 10.0),
              children: <Widget>[
                DrawerHeader(
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(0, 35, 0, 0),
                    child: Text('Navigation', textAlign: TextAlign.center, style: TextStyle(fontSize: 20, color: Colors.white))
                  )
                ),
                ListTile(
                  title: Text('Profile', style: TextStyle(color: Colors.white)),
                  trailing: Icon(Icons.account_circle, color: Colors.white70),
                  onTap: (){
                    Navigator.of(context).pop();
                  },
                ),
                ListTile(
                  title: Text('Map', style: TextStyle(color: Colors.white)),
                  trailing: Icon(Icons.drive_eta, color: Colors.white70),
                  onTap: (){
                    Navigator.of(context).pop();
                  },
                ),
                ListTile(
                  title: Text('My Location', style: TextStyle(color: Colors.white)),
                  trailing: Icon(Icons.store, color: Colors.white70),
                  onTap: (){
                  },
                )
              ],
            )
          ),
        ),
    );
  }

  Widget body();
}

【问题讨论】:

    标签: flutter dart mixins


    【解决方案1】:

    原来我想的太大了,组合mixins可以用一个简单的逗号来完成。

    ... with SingleTickProviderMixin, BasePageMixin

    【讨论】:

      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      相关资源
      最近更新 更多