1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter call child widget method',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ParentWidget(),
);
}
}
class ChildWidget extends StatefulWidget {
const ChildWidget({Key? key}) : super(key: key);
@override
State<ChildWidget> createState() => _ChildWidgetState();
}
class _ChildWidgetState extends State<ChildWidget> {
int value = 0;
void click() {
setState(() {
value += 1;
});
}
@override
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
color: Colors.blueAccent,
child: Center(
child: Text(
'$value',
style: const TextStyle(color: Colors.white),
),
),
);
}
}
class ParentWidget extends StatelessWidget {
ParentWidget({Key? key}) : super(key: key);
GlobalKey<_ChildWidgetState> childController = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
children: [
const Spacer(),
ChildWidget(
key: childController,
),
const SizedBox(
height: 10,
),
const Text('lickscreen.com'),
const SizedBox(
height: 10,
),
TextButton(
onPressed: () {
childController.currentState?.click();
},
child: const Text('点击+1'),
),
const Spacer(),
],
),
)),
);
}
}
|