如何在下面给出的示例代码中显示没有 Buildcontext
的 HookWidget
,该代码包含在扩展 Undefined name 'context'.
的类中?
目前,我收到了 flutter_hooks: ^0.10.0
hooks_riverpod: ^0.3.0
我正在使用以下包来管理我的应用程序:
class HomeScreen extends HookWidget {
static String routeName = "/home";
PageController _pageController = PageController(initialPage: 1);
dynamic balanceAvailable = 0.0;
List<TransactionModel> transactionList = [];
_onRefresh(DragEndDetails details) async {
showDialog(
context: context,
builder: (BuildContext context) {
return Center(
child: SizedBox(
height: MediaQuery.of(context).size.height / 4,
width: MediaQuery.of(context).size.width / 2,
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
);
});
await Future.delayed(const Duration(seconds: 2));
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onVerticalDragEnd: _onRefresh,
child: PageView(
controller: _pageController,
children: [
MenuScreen(),
HomeBody(
value: balanceAvailable,
),
TransactionScreen(
transactionList: transactionList,
value: balanceAvailable,
),
],
),
),
);
}
}
示例代码:
Sub load_form(c As Control, Optional parent_id As Integer = 0)
i = i + 1
Dim frmid As Integer
If Request("formid") Is Nothing Then
frmid = 1
Else
frmid = Request("formid")
End If
Dim dt As DataTable
Dim sql As String = ""
If parent_id = 0 Then
sql = "SELECT * from form_fields where form_id=" & frmid & " order by parentid, order_number"
dt = GetData(sql)
Else
sql = "SELECT * from form_fields where form_id=" & frmid & " and parentid=" & parent_id & " order by parentid, order_number"
dt = GetData(sql)
End If
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
' MsgBox(sql)
Dim p As New Panel()
If row(2).ToString.Trim(" ") = "panel" Then
' MsgBox(dt.Rows(0)(0))
Dim pnl As New Panel()
pnl.ID = "pnl" & row(0)
pnl.GroupingText = row(9)
p = pnl
load_form(p, row(0))
End If
If row(2).ToString.Trim() = "textbox" Then
Dim t As New TextBox With {
.CssClass = "form-control"}
t.Attributes.Add("showto", "true")
t.Attributes.Add("dbdata", "true")
t.Attributes.Add("placeholder", "Enter O2 Level...")
t.ID = "Txt" & i
p.Controls.Add(t)
End If
If row(2).ToString.Trim() = "checkbox" Then
Dim cb As New CheckBox With {
.CssClass = "cb form-control"}
cb.Attributes.Add("showto", "All")
cb.Attributes.Add("dbdata", "true")
cb.Attributes.Add("title", row("title").ToString.Trim())
cb.Attributes.Add("altValue", row("altvalue").ToString.Trim())
cb.Text = row(7).ToString.Trim()
cb.ID = "cb" & i
p.Controls.Add(cb)
End If
c.Controls.Add(p)
Next
End If
End Sub
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以将 context
传递给 _onRefresh
第 1 步:_onRefresh(DragEndDetails details, BuildContext context)
第 2 步:onVerticalDragEnd: (details) => {_onRefresh(details, context)},
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
class TransactionModel {}
class HomeScreen extends HookWidget {
static String routeName = "/home";
PageController _pageController = PageController(initialPage: 1);
dynamic balanceAvailable = 0.0;
List<TransactionModel> transactionList = [];
_onRefresh(DragEndDetails details, BuildContext context) async {
showDialog(
context: context,
builder: (BuildContext context) {
return Center(
child: SizedBox(
height: MediaQuery.of(context).size.height / 4,
width: MediaQuery.of(context).size.width / 2,
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
);
});
await Future.delayed(const Duration(seconds: 2));
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onVerticalDragEnd: (details) => {_onRefresh(details, context)},
child: PageView(
controller: _pageController,
children: [
MenuScreen(),
HomeBody(),
TransactionScreen(),
],
),
),
);
}
}
class MenuScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MenuScreen"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'MenuScreen',
),
],
),
));
}
}
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("HomeBody"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'HomeBody',
),
],
),
));
}
}
class TransactionScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("TransactionScreen"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'TransactionScreen',
),
],
),
));
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
答案 1 :(得分:0)
您是否尝试过钩子的 useContext()
例程?