Implement WebView in modal bottom sheet in flutter - flutter

I want to implement the modal bottom sheet with a webview as a child in it. When I try to do it, the webview gets messed up and the sheet comes on the top of the screen instead of the bottom.
I tried to change the height but nothing really worked.
Pub dependency: flutter_webview_plugin: ^0.3.5
void _showModelSheet() {
showModalBottomSheet(
context: context,
builder: (builder) {
return new Container(
height: screenHeight / 4,
child: new Container(
decoration: new BoxDecoration(
color: Colors.amber,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(20.0),
topRight: const Radius.circular(20.0))),
child: Container(
child: new MaterialApp(
debugShowCheckedModeBanner: false,
routes: {
"/": (_) => new WebviewScaffold(
url: "https://www.google.com",
),
},
),
),
),
);
});
}
child: RaisedButton(
elevation: 10,
splashColor: Colors.black,
color: Colors.red.shade900.withOpacity(0.7),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(110)),
onPressed: _showModelSheet,
child: Text(
"sheet"),
),
),
I want to make the whole webview to get wrap into the height of the modal bottom sheet.

You are returning a MaterialApp inside another. And the reason is that the plugin you're using forces you to do that.
Better use the oficial Flutter WebView plugin.
webview_flutter: ^0.3.9+1
Then use it like this:
void _showModelSheet() {
showModalBottomSheet(
context: context,
builder: (builder) {
return new Container(
height: MediaQuery.of(context).size.height / 4,
child: new Container(
decoration: new BoxDecoration(color: Colors.amber, borderRadius: new BorderRadius.only(topLeft: const Radius.circular(20.0), topRight: const Radius.circular(20.0))),
child: Container(
child: WebView(
initialUrl: 'https://google.com',
)),
),
);
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
elevation: 10,
splashColor: Colors.black,
color: Colors.red.shade900.withOpacity(0.7),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(110)),
onPressed: _showModelSheet,
child: Text("sheet"),
),
),
);
}
This is the result:

Related

In Flutter, bottomSheetDialog's radius is not working

To implement flutter bottomSheetDialog with dynamic height, I wrote a code like this.
void showCustomBottomSheetDialog(Widget body, BuildContext context) {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext ctx) {
return Wrap(
children: [
Column(
children: [
Container(
height: 24.sp,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
),
),
),
body,
],
),
],
);
},
);
}
showCustomBottomSheetDialog(
Container(
child: Column(
children: [
TextFormField(),
Text('asdfasdfds'),
Text('asdfasdfds'),
Text('asdfasdfds'),
],
),
),
context,
);
But with this code, the displayed bottomSheet did not have a rounded corner...
Could you tell me what is a problem of my code...?
create rounded corner manually
eg:
showModalBottomSheet(
isScrollControlled: true,
context: context,
backgroundColor: Colors.transparent,
builder: (contex) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
const BorderRadius.vertical(top: Radius.circular(18))),
child: Column(
children: []
....
also you can wrap it with DraggableScrollableSheet to make it scrollable widget
This is because the Container with rounded corner is inside the Column widget.
For this to work ,lift up the Container.
Code now:
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (BuildContext ctx) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
),
),
child: Column(...)
),
},
);
Actually, we have shape property on showModalBottomSheet and can be used to make rounded corner, like
showModalBottomSheet(
isScrollControlled: true,
context: context,
shape: const RoundedRectangleBorder( //this
borderRadius: BorderRadius.only(
topLeft: Radius.circular(24),
topRight: Radius.circular(24),
),
),
More about showModalBottomSheet

persistent_bottom_nav_bar reclick on set screen icon, returns to that screen

I currently have a persistent tab view in my flutter application with the code below:
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffffffff),
resizeToAvoidBottomInset: false,
body: PersistentTabView.custom(
context,
controller: _tabController,
screens: <Widget>[
const page1(),
const page2(),
const page3(),
ProfileScreen(
onLogoutButtonTap: () async {
Get.deleteAll();
await StorageClass.logout();
Navigator.pushNamedAndRemoveUntil(
context, LoginScreen.routeName, (r) => false);
// Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => LoginScreen()));
},
),
// const ExploreScreen(),
],
// items: _navBarsItems(),
customWidget: _customNavBar(),
routeAndNavigatorSettings: const CutsomWidgetRouteAndNavigatorSettings(
onGenerateRoute: RouteManager.generateRoute),
itemCount: 4,
),
);
}
Widget **_customNavBar()** {
return Container(
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black12,
blurRadius: 5.0,
spreadRadius: -2,
offset: Offset(0, -5),
),
],
shape: BoxShape.rectangle,
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(25.0),
topRight: Radius.circular(25.0),
bottomLeft: Radius.zero,
bottomRight: Radius.zero,
),
),
child: SizedBox(
height: 80,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: InkWell(
onTap: () => onTabTapped(0),
child: currentTab == 0
? SvgPicture.asset("assets/icons/home_icon_fill.svg",
width: 24, height: 24)
: SvgPicture.asset("assets/icons/home_icon_nofill.svg",
width: 24, height: 24),
),
),
Expanded(
child: InkWell(
onTap: () => onTabTapped(1),
child: currentTab == 1
? SvgPicture.asset(
"assets/icons/notification_icon_fill.svg",
width: 24,
height: 24)
: SvgPicture.asset(
"assets/icons/notification_icon_nofill.svg",
width: 24,
height: 24),
),
),
Expanded(
child: InkWell(
onTap: () => onTabTapped(2),
child: currentTab == 2
? SvgPicture.asset("assets/icons/library_icon_fill.svg",
width: 24, height: 24)
: SvgPicture.asset("assets/icons/library_icon_nofill.svg",
width: 24, height: 24),
),
),
Expanded(
child: InkWell(
onTap: () => onTabTapped(3),
child: currentTab == 3
? SvgPicture.asset("assets/icons/user_icon_fill.svg",
width: 24, height: 24)
: SvgPicture.asset("assets/icons/user_icon_nofill.svg",
width: 24, height: 24),
),
),
],
),
),
);
}
void onTabTapped(int index) {
setState(() {
currentTab = index;
_tabController.index = index;
});
setState(() {});
}
All of this works, so whenever I click on any of these icons, or the bottom nav bar icons, it directs me to the proper pages: page1, page2, page3 and Profile screen AND the nav bar icons highlight properly.
What I need help is that when I am in page1() and I click on stuff and go explore page1(), going deeper and deeper in the application, when I reclick on the page1() icon, I want to be able to go back to the initial page1() screen. Right now the reclick does not work but if I click on other tabs I can go to the other tabs. I need to be able to once I reclick on the page1() icon, it builds the page1() screen again (or it just resets all of what I have explored).
Thanks

Flutter: unloaded widgets when starting the app & unresponsive widgets

for a while now, I was trying to learn Flutter for mobile development. So, everything is straight forward and easy to grasp.
But, the following issues I cannot seem to solve:
Resizing the CircleAvatar() in the AppBar: I tried using scale, size, nothing worked.
Whatever I added after the 1st ListView.builder(), the emulator does not read/ display
my flutter is up-to-date and no errors/issues are shown when I run flutter doctor or my run the app.
Thanks
Code Used:
class MessageScreen extends StatefulWidget {
static Route<dynamic> route() => MaterialPageRoute(
builder: (context) => MessageScreen(),
);
#override
_MessageScreenState createState() => _MessageScreenState();
}
class _MessageScreenState extends State<MessageScreen> {
String tempLink =
'https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=634&q=80';
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[400],
appBar: AppBar(
elevation: 0.0,
leading: CircleAvatar(
backgroundImage: NetworkImage(tempLink),
radius: 15.0,
child: tempLink == null ? Text('HH') : null,
),
title: Text('Chats'),
backgroundColor: Colors.blue[400],
actions: [
IconButton(
onPressed: () {},
icon: Icon(Icons.search),
),
],
),
body: Column(
children: [
Row(
children: [
Container(
child: ListView.builder(
itemCount: newMatching.length,
padding: EdgeInsets.only(left: 6),
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ChatScreen(
user: newMatching[index],
),
),
),
child: _profileButton(tempLink),
);
},
),
),
],
),
SizedBox(
height: 18,
),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20), topRight: Radius.circular(20)),
),
child: ListView.builder(
itemCount: chats.length,
itemBuilder: (BuildContext context, int index) {
final Message chat = chats[index];
return GestureDetector(
onTap: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ChatScreen(
user: chat.sender,
),
),
),
child: Container(
margin: EdgeInsets.only(top: 5, bottom: 5, right: 1),
padding:
EdgeInsets.symmetric(horizontal: 2, vertical: 5),
decoration: BoxDecoration(
color: chat.unread ? Color(0xFFFFEFEE) : Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(20.0),
bottomRight: Radius.circular(20.0),
),
),
child: _chatNavigatorButton(
chat.sender.imgAvatar,
chat.sender.fname,
chat.text,
chat.time,
chat.unread)),
);
}),
),
],
),
);
}
}
Try wrapping the CircleAvatar with a Container:
Container(height: 10, width: 10, child: CircleAvatar(...))
Is there a chance that chats simply has the length of 0 and no elements? Maybe the second ListView.builder() does display correctly but includes no items. At least that's what I can retrieve from the given code.

Flutter modal bottom sheet full height

I was trying things out with ModalBottomSheet. How can I achieve 90% height of device screen size for modal sheet. I did mediaquery but still it does not give me more than half of the screen size. How do I solve this?
Here is the code:
class _TestFileState extends State<TestFile> {
modalSheet() {
showModalBottomSheet(
context: context,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0), topRight: Radius.circular(15.0)),
),
builder: (context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
ListTile(
leading: Icon(Icons.email),
title: Text('Send email'),
onTap: () {
print('Send email');
},
),
ListTile(
leading: Icon(Icons.phone),
title: Text('Call phone'),
onTap: () {
print('Call phone');
},
),
],
);
});
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Center(child: Text('Testing Modal Sheet')),
),
body: Center(
child: InkWell(
onTap: () {
modalSheet();
},
child: Container(
color: Colors.indigo,
height: 40,
width: 100,
child: Center(
child: Text(
'Click Me',
style: TextStyle(color: Colors.white),
),
)),
),
),
),
);
}
}
Here is the output:
you have to pass isScrollControlled: true and use mediaquery as given below
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.5,
color: Colors.red,
//height: MediaQuery.of(context).size.height,
);
});
As I remember that's a restriction about the native implementation of Flutter modal bottom sheet.
You can use the package modal_bottom_sheet to achieve that.
Install:
dependencies:
modal_bottom_sheet: ^0.2.2
And minimal example:
showMaterialModalBottomSheet(
context: context,
expand: true, //this param expands full screen
builder: (context, scrollController) => Container(),
)

Want to Show an AlertDialog when clicking on a Button, on Pressed Method shows an error. ( Undefined name 'context' .)

Hey im New in learning Flutter and Dart. Please help me. :)
I really donĀ“t know what to do I`m a totaly beginner. :/
Now I have pastet my complete code I hope you can see where I defined my Container.
This all is a TabView because I want to train an play with some examples so I tryed out the TabView. In The TabView I packed then all in Containers. If there is a better option, you can tell me of course also. :)
This is my Code:
Future<void> _showAlert(BuildContext context) async {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Accept?'),
content: Text("Do you accept?"),
actions: <Widget>[
FlatButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('No')
),
FlatButton(onPressed: (){
Navigator.of(context).pop();
},
child: Text('Yes')
),
],
backgroundColor: Colors.deepOrange,
shape: CircleBorder(),
);
}
);
}
class MyApp extends StatelessWidget {
List<Widget> containers = [
Container(
color: Colors.orange,
padding: EdgeInsets.all(20.0),
alignment: Alignment.center,
child: Container(
height: 80,
width: 80,
child: FloatingActionButton(
child: Icon(Icons.check),
tooltip: ('"Hello World"'),
onPressed: () {
print('Hello World');
},
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
),
),
Container(
color: Colors.teal,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
print("Raised Button clicked!");
},
child: Text(
"Please click on me!",
style: TextStyle(fontSize: 18),
),
),
),
Container(
color: Colors.deepPurple,
alignment: Alignment.center,
child: RaisedButton(onPressed: () {_showAlert(context);},
color: Colors.deepOrange,
child: Icon(Icons.warning)
),
),
];
The Error says: Undefined name 'context'. (In the onPresssed section at my Button.)
Error shown
Code Snippet 1
Code Snippet 2
What you are missing in your Stateless widget is the build method(Which contains the context), but the issue there is that you can't return a List with the build method because it only returns a Widget. In order to fix this, you should first create a function for your list of widgets, then inside the Stateless widget return the function inside of a widget with a children property, like a Column
Your Widget list function
widgetList(BuildContext context) {
return [
Container(
color: Colors.orange,
padding: EdgeInsets.all(20.0),
alignment: Alignment.center,
child: Container(
height: 80,
width: 80,
child: FloatingActionButton(
child: Icon(Icons.check),
tooltip: ('"Hello World"'),
onPressed: () {
print('Hello World');
},
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
),
),
Container(
color: Colors.teal,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
print("Raised Button clicked!");
},
child: Text(
"Please click on me!",
style: TextStyle(fontSize: 18),
),
),
),
Container(
color: Colors.deepPurple,
alignment: Alignment.center,
child: RaisedButton(
onPressed: () {
_showAlert(context);
},
color: Colors.deepOrange,
child: Icon(Icons.warning)),
),
];
}
Your Stateless Widget
class MyApp extends StatelessWidget {
#override Widget build(BuildContext context) {
return Column(
children:
widgetList(context)
);
}
}