我想從另一個類呼叫該方法,我使用了這種方法,但方法呼叫不正確。`
class PostModel extends StatefulWidget {
final profilename;
final bool isVideoUrl;
final int urlsource;
final videoUrl;
const PostModel(
{this.profilename,
required this.isVideoUrl,
required this.urlsource,
this.videoUrl});
@override
State<PostModel> createState() => _PostModelState();
}
class _PostModelState extends State<PostModel> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
NameSection(),
InkWell(
onDoubleTap: , // **toggleLike()** Want to call from here
child: (widget.isVideoUrl)
? SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.width,
child: VideoController(videosUrl[widget.urlsource % 6]))
: Image(
image: NetworkImage(
"https://picsum.photos/seed/${widget.urlsource}/400/400"),
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
),
PostIcons(), // here is the widget .
Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
SizedBox(
height: 5,
),
Text(
"11,536 likes",
style: GoogleFonts.roboto(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 5,
),
Text(
"akashbanerjee ???♀?",
style: GoogleFonts.roboto(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 5,
),
Text(
"View all 244 comments ",
style: GoogleFonts.roboto(
color: Color.fromARGB(255, 186, 186, 186), fontSize: 12),
),
SizedBox(
height: 5,
),
Text(
"5 hours ago",
style: GoogleFonts.roboto(
color: Color.fromARGB(255, 186, 186, 186), fontSize: 10),
),
SizedBox(
height: 10,
),
Divider(
color: Color.fromARGB(255, 77, 77, 77),
indent: 0,
endIndent: 0,
)
]))
],
);
}
}
```
```
class PostIcons extends StatefulWidget {
const PostIcons({Key? key}) : super(key: key);
@override
State<PostIcons> createState() => PostIconsState();
}
class PostIconsState extends State<PostIcons> {
bool liked = false;
toggleLike() { // i want to call this function from above class.
setState(() {
liked = !liked;
});
}
bool saved = false;
toggleSavePost() {
setState(() {
saved = !saved;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: (saved) ? Text("Post saved") : Text("Post unsaved"),
duration: Duration(milliseconds: 1000),
));
});
}
showShareBottomModal() {
// setState(() {
showModalBottomSheet<void>(
// context and builder are
// required properties in this widget
backgroundColor: Color.fromARGB(255, 38, 38, 38),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(24), topRight: Radius.circular(24)),
),
context: context,
builder: (BuildContext context) {
// we set up a container inside which
// we create center column and display text
// Returning SizedBox instead of a Container
return SharePost();
});
// });
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
IconButton(
onPressed: toggleLike,
iconSize: 28,
icon: Icon(
liked ? Icons.favorite : Icons.favorite_outline,
color: liked ? Colors.red : Colors.white,
)),
// SizedBox(
// width: 0,
// ),
Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: IconButton(
iconSize: 28,
onPressed: null,
icon: Icon(
FeatherIcons.messageCircle,
color: Colors.white,
)),
),
IconButton(
onPressed: showShareBottomModal,
iconSize: 28,
icon: Icon(
FeatherIcons.send,
color: Colors.white,
)),
],
),
IconButton(
onPressed: toggleSavePost,
iconSize: 28,
icon: Icon(saved ? Icons.bookmark : Icons.bookmark_border_outlined),
color: Colors.white,
)
],
);
}
}
PostModel 類
class Feeds extends StatefulWidget {
@override
State<Feeds> createState() => _FeedsState();
}
class _FeedsState extends State<Feeds> {
@override
Widget build(BuildContext context) {
return Column(
children: List.generate(storyList.length, (index) {
return PostModel(isVideoUrl: false, urlsource: index);
}),
);
}
}
Postmodel 小部件位于另一個類的 List 中。
所以任何人都可以建議我一些很好的實用方法來呼叫其他類的 setState (不是 child 或 parent ,完全不同的類)。
uj5u.com熱心網友回復:
當您使用GlobalKey
like 時,您還需要將該鍵提供給您嘗試使用它控制的小部件。所以在你的情況下
PostIcons(key: globalKey)
這是一個使用此類鍵控制另一個小部件的小部件的最小作業示例:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
final GlobalKey<AState> globalKey = GlobalKey();
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(children: [
A(key: globalKey),
TextButton(
onPressed: () {
globalKey.currentState?.increaseI();
},
child: Text("click"))
]))));
}
}
class A extends StatefulWidget {
const A({Key? key}) : super(key: key);
@override
State<A> createState() => AState();
}
class AState extends State<A> {
int i = 1;
void increaseI() {
setState(() {
i ;
});
}
@override
Widget build(BuildContext context) {
return Text(i.toString());
}
}
編輯:
因此,對于您的代碼,請嘗試添加
final GlobalKey<PostIconsState> globalKey = GlobalKey();
在你的_PostModelState
. 所以就在下面
class _PostModelState extends State<PostModel> {
然后這一行:
PostIcons(), // here is the widget .
你改成
PostIcons(key: globalKey), // here is the widget .
然后你可以改變
onDoubleTap: , // **toggleLike()** Want to call from here
至
onDoubleTap: () => globalKey.currentState?.toggleLike(),
我認為您之前遇到的問題是 globalKey 是在類之外定義的。通過在類中定義它,您可以確保每個實體都有不同的鍵
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/529420.html
標籤:安卓扑镖