在一个类中可以有两个扩展吗?在我的例子中,我有一个class AddCreditsState extends State<AddCredits>
类,我只想在我的class AddCreditsState extends State<AddCredits>
中扩展HookWidget
,
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],
)),
),
);
}
发布于 2021-03-03 14:49:26
在Dart中,您可以implements
多个接口,但Dart仅支持单一继承。因此,您不能从多个类执行extends
。
不过,您可以在with
中使用多个Mixin。
但在本例中,您可能需要一个StatefulHookWidget,一个可以在其StatefulWidget方法中使用钩子的构建。
完整的源代码
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'),
)
],
),
),
);
}
}
https://stackoverflow.com/questions/66459297
复制