本文介绍了扑类双重延伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在一个类中进行两次扩展?在我的情况下,我有类 AddCreditsState扩展了状态< AddCredits> ,而我只是想扩展我的类AddCreditsState扩展了状态< AddCredits> 中的 HookWidget >,

  Class AddCreditsState扩展了State< AddCredits> {{//如何在此处插入`HookWidget`?返回脚手架(backgroundColor:Color.fromRGBO(10,15,39,1),正文:SingleChildScrollView(子代:集装箱(宽度:globals.screenWidth,高度:globals.screenHeight,边距:EdgeInsets.only(top:25),子:列(孩子:[headercol,身体],)),),); 

}

解决方案

在Dart中,您可以实现多个接口,但是Dart仅支持单继承.因此,您不能扩展多个类.

尽管可以将多个Mixins与 with 一起使用.

但是在这种情况下,您可能想要的是

完整的源代码

  import'package:flutter/material.dart';导入'package:flutter_hooks/flutter_hooks.dart';void main(){runApp(MaterialApp(debugShowCheckedModeBanner:否,标题:"StatefulHookWidget示例",主页:HomePage(),),);}类HomePage扩展了StatefulHookWidget {@override_HomePageState createState()=>_HomePageState();}类_HomePageState扩展了State< HomePage>{字符串_state ='Hello';@override窗口小部件build(BuildContext context){final _hookedVar = useState('Hello');返回脚手架(appBar:AppBar(标题:文本('StatefulHookWidget示例')),身体:容器(填充:EdgeInsets.all(16.0),对齐方式:Alignment.center,子:列(mainAxisSize:MainAxisSize.min,孩子们: [文字('STATE:$ _state'),const SizedBox(高度:16.0),文字("HOOK:$ {_ hookedVar.value}"),const SizedBox(高度:16.0),升高按钮(onPressed:(){setState(()=> _state ='再见');_hookedVar.value ='再见';},子代:文字('CLICK ME'),)],),),);}} 

Is it possible to have double extends in a single class? in my case, i have class class AddCreditsState extends State<AddCredits> i just want to extends the HookWidget in my class AddCreditsState extends State<AddCredits>,

class AddCreditsState extends State<AddCredits>{ // how do i insert the `HookWidget` here?
    return Scaffold(
      backgroundColor: Color.fromRGBO(10, 15, 39, 1),
      body: SingleChildScrollView(
        child: Container(
            width: globals.screenWidth,
            height: globals.screenHeight,
            margin: EdgeInsets.only(top: 25),
            child: Column(
              children: [headercol, body],
            )),
      ),
    );

}

解决方案

In Dart, you can implements multiple interfaces, but Dart only supports single inheritance. So, you can not extends from multiple classes.

You can though use multiple Mixins with with.

But in this particular case, what you probably want is a StatefulHookWidget, a StatefulWidget that can use hooks inside its build method.

Full source code

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

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'StatefulHookWidget Example',
      home: HomePage(),
    ),
  );
}

class HomePage extends StatefulHookWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _state = 'Hello';
  @override
  Widget build(BuildContext context) {
    final _hookedVar = useState('Hello');
    return Scaffold(
      appBar: AppBar(title: Text('StatefulHookWidget Example')),
      body: Container(
        padding: EdgeInsets.all(16.0),
        alignment: Alignment.center,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text('STATE: $_state'),
            const SizedBox(height: 16.0),
            Text('HOOK: ${_hookedVar.value}'),
            const SizedBox(height: 16.0),
            ElevatedButton(
              onPressed: () {
                setState(() => _state = 'Goodbye');
                _hookedVar.value = 'Goodbye';
              },
              child: Text('CLICK ME'),
            )
          ],
        ),
      ),
    );
  }
}

这篇关于扑类双重延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:15