我试图在第二页上打开 DraggableScrollableSheet,同时从第一页导航到第二页(点击)。它有效,但该表没有超过 Appbar,即全高。
所以我在这里找到了将其设置为全高的解决方案。 如何将 showModalBottomSheet 设置为全高
解决方案是将 DraggableScrollableSheet 包裹在 showModalBottomSheet 下。但是当我尝试实施这个解决方案时,它给了我一个错误。“在构建过程中调用了 setState() 或 markNeedsBuild()。 ”
工作表下的背景小部件未构建。但是 DraggableScrollableSheet 工作表已创建。在此处查看快照: 主页, 有错误的第二页
由于我是新来的,我无法根据上面引用的解决方案修改我的代码。所以请求通过修改我的代码来提供解决方案。谢谢 !!
这是我的代码-
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main(){
runApp(MaterialApp(
title: 'HomePage',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: HomePage(),
debugShowCheckedModeBanner: false,
));
}
class HomePage extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page1'),
), //AppBar,
body: InkWell(
onTap: ()
{
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => OpenPageWithSheet()));
},
child: Center(
child: Container(
color: Colors.red,
height: 100,
width: 100,
child: Align(
child: Text("Go To Page2",)),
),
),
),
);
}
}
class OpenPageWithSheet extends StatefulWidget {
@override
_OpenPageWithSheetState createState() => _OpenPageWithSheetState();
}
class _OpenPageWithSheetState extends State<OpenPageWithSheet> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('DraggableScrollableSheet'),
),
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.grey,
child: Stack(
alignment: Alignment.topCenter,
children: [
Container(
color: Colors.blueAccent,
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width,
),
showMySheet(context),
],
),
),
);
}
}
showMySheet(context)
{
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
isScrollControlled: true,
isDismissible: true,
builder: (context) {
return SafeArea(
child: DraggableScrollableSheet(
initialChildSize: 0.5,
minChildSize: 0.5,
maxChildSize: 1.0,
expand: true,
builder: (BuildContext context, scrollController) {
return SingleChildScrollView(
controller: scrollController,
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
color: Colors.grey,
child: Align(
alignment: Alignment.topCenter,
child: (
Text("Sheet Opened but Background Not built")),
),
),
);
}
),
);
}
);
}