Related
I am Getting An error (overflowed by 4.8 pixels on the bottom). I was creating an emailAuthSheet with showModalBottomSheet while debugging the app it shows the error of overflowed. Can u please guide me...And If Possible can you please also guide me on how to make it responsive for all devices as per their device screen size
The following code is below:
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child:
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Log in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
color: constantColors.redColor,
child: Text(
'Sign in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
]),
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0),
topRight: Radius.circular(15.0))));
});
}
Below is the pic of the flutter app and error
Wrap your container with singleChildScrollView
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return SingleChildScrollView(
child: Container(
child:
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Log in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
color: constantColors.redColor,
child: Text(
'Sign in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
]),
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0),
topRight: Radius.circular(15.0)))),
);
});
}
Just add 'isScrollControlled' parameter true to showModalBottomSheet.
And changed Column widget to Wrap widget.
(I removed some defines and widget because of leak of data)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (context) {
return Container(
child: Wrap(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
),
),
Container(height: 450, color: Colors.blue[100]),
// Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
child: Text(
'Log in',
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
child: Text(
'Sign in',
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
],
),
);
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {
emailAuthSheet(context);
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Container();
}
}
I am a newbie in Flutter and in programming as well.
I have created a ListView with several Cards inside each card points to separate PDF documents which are loaded using the package 'advance_pdf_viewer'. I have done all this hardcoded. But I would like to generate the Cards and the pdf document dynamically. How can I do that?
Here are my existing Codes:
Main.dart file:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'c9Bban.dart';
import 'c9Bbanbakoron.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'HSC Textbooks',
theme: ThemeData(
primarySwatch: Colors.cyan,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'১ম - ১০ম শ্রেণী পাঠ্যবই',
style: TextStyle(
//fontSize: 14,
fontFamily: 'Baloo Da',
),
),
centerTitle: true,
),
body: ListView(children: [
Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child: Image.asset('images/bangla bakaron.jpg',
fit: BoxFit.cover),
),
title: Text(
'বাংলা ব্যাকরণ',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 4, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Bbanbakoron()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ও ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
]),
),
Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child: Image.asset('images/BGS.jpg', fit: BoxFit.cover),
),
title: Text(
'বাংলাদেশ ও বিশ্বপরিচয়',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Bbgs()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)
),
],
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Ebgs()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)
),
],
),
),
),
),
]),
),
]),
);
}
}
c9Bban.dart file (one of the separate files for loading separate PDF documents. Rest of the files are exactly the same except the PDF URL is different):
import 'package:flutter/material.dart';
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
class C9Bban extends StatefulWidget {
#override
_MyC9Bban createState() => _MyC9Bban();
}
class _MyC9Bban extends State<C9Bban> {
bool _isLoading = true;
PDFDocument document;
#override
void initState() {
super.initState();
loadDocument();
}
loadDocument() async {
document = await PDFDocument.fromURL('https://storage.googleapis.com/school_books/Class9/ban/Bangla%20Sahitto.pdf');
setState(() => _isLoading = false);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 20,
),
body: Center(
child: _isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: document,
zoomSteps: 1,
),
),
bottomNavigationBar: BottomAppBar(
child: Container(
height: 85.0,
),
),
),
);
}
}
So far I was able to create this which works for creating the ListView Cards dynamically.
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
import 'c9Bban.dart';
import 'c9Bbansohopath.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'HSC Textbooks',
theme: ThemeData(
primarySwatch: Colors.cyan,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
List booklist = [
Books(
image: 'Bangla.jpg',
bookname: 'Bangla',
banbookname: 'C9Bban',
),
Books(
image: 'bangla shapath.jpg',
bookname: 'Bangla Sahapath',
banbookname: 'C9Bbansohopath',
),
];
void updateBook(index) {
Navigator.push(context, MaterialPageRoute(builder: (context) => booklist[index].banbookname));
}
return Scaffold(
appBar: AppBar(
title: Text('Book App'),
centerTitle: true,
),
body: ListView.builder(
itemCount: booklist.length,
itemBuilder: (context, index) {
return Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child:
Image.asset('images/islam.jpg', fit: BoxFit.cover),
),
title: Text(
booklist[index].bookname,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
updateBook(index);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
updateBook(index);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
]),
);
}),
);
}
}
class Books {
String image;
String bookname;
String banbookname;
String engbook;
Books({this.image, this.bookname, this.banbookname});
}
I am new to flutter. I have two buttons in a row and I want to change the colour and the text of the first button when it is pressed to something else and if the second button is pressed the first button should go back to the default colour and the second button should go to the new colour. Is there any way I can achieve this by indexing?
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed: () {},
child: Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Column(
children: [
Text('Stunning Solo', style: TextStyle(fontSize: 15,color: Colors.white)),
Text('Current Plan', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$$dollars3",style: TextStyle(fontSize: 20,color: Colors.white)),
),
Text(
"Free forever",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: whiteColor),
),
],
),
),
),
),
SizedBox(height: 20),
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed:
() {},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Startup Pack', style: TextStyle(fontSize: 15,color: Colors.white)),
),
Text('Introductory Offer', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$$dollars",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
decoration: TextDecoration.lineThrough),
),
SizedBox(width: 5),
Text(
"\$$dollars2",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: whiteColor),
),
]),
You can use ToggleButton for this.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<bool> buttonsState = [
true,
false
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: ToggleButtons(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
color: Colors.blue,
fillColor: Colors.blue,
isSelected: buttonsState,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 8,
right:8,
),
child: Text(
"Free forever", style: _getTextStyle(0),
textAlign: TextAlign.center,
),
),
Padding(
padding: EdgeInsets.only(
left: 8,
right: 8,
),
child: Text(
"Startup Pack", style: _getTextStyle(1),
textAlign: TextAlign.center,
),
),
],
onPressed: _updateButtonState,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
);
}
_updateButtonState(int index) {
setState(() {
for (int buttonIndex = 0; buttonIndex < buttonsState.length; buttonIndex++) {
if (buttonIndex == index) {
buttonsState[buttonIndex] = true;
} else {
buttonsState[buttonIndex] = false;
}
}
});
}
TextStyle _getTextStyle(int index) {
return buttonsState[index]
? TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white) : TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.blue);
}
}
You can just use TextButton since it provides state type. It is also the replacement of FlatButton.
Example if you want to change foregroundColor which are text or icon color:
TextButton(
onPressed: onPressed,
child: Text("label"),
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
return pressedColor;
} else if (states.contains(MaterialState.selected)) {
return pressedColor;
} else if (states.contains(MaterialState.error)) {
return redPrimary;
} else if (states.contains(MaterialState.disabled)) {
return darkSecondary;
}
return darkSecondary;
},
),
...
),
);
I want BottomNavigationBar as well as TabBar in flutter without having appBar, Individually both widgets are working but when combined, it is saying The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Haate Khori',
theme: ThemeData.light().copyWith(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions = <Widget>[
Donation(),
Text(
'EXPENSES',
style: optionStyle,
),
Text(
'ACTIVITIES',
style: optionStyle,
),
Text(
'GALLERY',
style: optionStyle,
),
Text(
'DONATE US',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.all(40.0),
child: SizedBox(
height: 70.0,
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.teal], // whitish to gray
stops: [0.0, 0.8]),
),
),
Container(
padding: EdgeInsets.all(10.0),
child: Text(
'Title goes here',
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
color: Colors.teal,
),
_widgetOptions.elementAt(_selectedIndex),
],
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.receipt),
title: Text('Expenses'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.fiber_smart_record),
title: Text('Activities'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.collections),
title: Text('Gallery'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text('Donate us'),
backgroundColor: Colors.teal,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
onTap: _onItemTapped,
),
),
);
}
}
class Donation extends StatefulWidget {
#override
_DonationState createState() => _DonationState();
}
class _DonationState extends State<Donation>
with SingleTickerProviderStateMixin {
TabController tabController;
#override
void initState() {
// TODO: implement initState
super.initState();
tabController = TabController(length: 2, vsync: this);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
SizedBox(
height: 10.0,
),
TabBar(
labelColor: Colors.teal,
controller: tabController,
tabs: <Widget>[
Tab(
child: Text('Tab1'),
),
Tab(
child: Text('Tab2'),
)
],
),
Expanded(
child: Container(
child: TabBarView(
controller: tabController,
children: <Widget>[
Text(
'Hello',
style: TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
),
Text(
'World',
style: TextStyle(
fontSize: 20.0,
),
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
);
}
}
Target : What I want
&
This is what I am getting
I have attached a code snippet which is from my unComplete web app same can be useful for you I haven't used TabBar in the scaffold tab instead created a row inside the scaffold and used it, you can look into my code and can figure out something for you hope it helps.
import 'package:flutter/material.dart';
import 'package:transportWeb/provider/dimensions.dart';
import 'package:transportWeb/screens/mainScreenViews/aboutUs.dart';
import 'package:transportWeb/screens/mainScreenViews/contactUs.dart';
import 'package:transportWeb/screens/mainScreenViews/home.dart';
import 'package:url_launcher/url_launcher.dart';
import '../constants.dart';
class MainScreen extends StatefulWidget {
#override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen>
with SingleTickerProviderStateMixin {
TabController _tabController;
var _currentDateTime = DateTime.now();
var _currentHours;
#override
void initState() {
super.initState();
_currentHours = _currentDateTime.hour;
print(_currentHours);
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
}
#override
void dispose() {
// TODO: implement dispose
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
Dimensions(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
tooltip: "Request Call",
backgroundColor: Colors.deepPurpleAccent,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
"Enter your Phone no",
style: kh3,
),
);
},
);
},
child: Icon(
Icons.phone,
color: Colors.white,
size: 30,
),
),
body: ListView(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 0),
height: 70,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border(
bottom: BorderSide(
color: Colors.grey.shade200,
width: 3,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 15),
child: RichText(
text: TextSpan(
style: kh2.copyWith(letterSpacing: 10),
children: <TextSpan>[
TextSpan(text: "JSSR "),
TextSpan(text: "Road"),
TextSpan(
text: "L",
style: kh2.copyWith(color: Colors.red),
),
TextSpan(text: "ines"),
],
),
),
),
Expanded(
child: SizedBox(
width: 0,
height: 0,
),
),
TabBar(
isScrollable: true,
indicatorColor: Colors.black,
indicatorSize: TabBarIndicatorSize.label,
labelColor: Colors.black,
unselectedLabelColor: Colors.grey.withOpacity(0.6),
controller: _tabController,
tabs: [
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"HOME",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"ABOUT US",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"CONTACT US",
style: kh5,
),
),
),
],
),
GestureDetector(
onTap: () {
launch(
"https://pub.dev/packages/url_launcher#-installing-tab-");
},
child: Container(
margin: EdgeInsets.only(left: 15, bottom: 12),
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.deepPurpleAccent,
),
child: Text(
"DOWNLOAD APP",
style: kh5.copyWith(color: Colors.white),
),
),
),
],
),
),
Container(
width: double.maxFinite,
height: double.maxFinite,
child: TabBarView(
controller: _tabController,
children: [
Home(),
AboutUs(),
ContactUs(),
],
),
)
],
),
);
}
}
I am creating a multi-paged app on flutter. When I am using the navigation in it, I am getting a black screen.
import 'package:flutter/material.dart';
void main() => runApp(MyHomePage());
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Page0(),
);
}
}
class Page0 extends StatefulWidget {
#override
_Page0State createState() => _Page0State();
}
class _Page0State extends State<Page0> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF493597),
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0, left: 10.0),
),
SizedBox(
height: 25.0,
),
Padding(
padding: EdgeInsets.only(left: 40.0),
child: Row(
children: <Widget>[
Text(
'Expense',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25.0),
),
SizedBox(
width: 10.0,
),
Text(
'What',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontSize: 25.0,
),
),
],
),
),
SizedBox(height: 60.0),
Container(
margin: EdgeInsets.only(
left: 10.0,
right: 10.0,
),
height: MediaQuery.of(context).size.height - 150,
decoration: BoxDecoration(
color: Color(0xFFFCFCFC),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(75.0),
topRight: Radius.circular(75.0),
),
),
child: ListView(
primary: false,
padding: EdgeInsets.only(
left: 15.0,
right: 20.0,
top: 25.0,
),
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
top: 30.0,
),
child: Column(
children: <Widget>[
//greeting text
Row(
children: <Widget>[
Expanded(
child: Center(
child: Text(
'Hello! :)',
style: TextStyle(
fontFamily: 'Permanent-Marker',
color: Colors.black,
fontSize: 30.0,
),
),
),
),
],
),
SizedBox(
height: 30.0,
),
//add button
Row(children: <Widget>[
Expanded(
flex: 1,
child: Container(
height: 100.0,
width: 100.0,
child: FittedBox(
child: FloatingActionButton(
elevation: 10.0,
backgroundColor: Colors.white,
child: Icon(
Icons.add,
color: Colors.black,
),
onPressed: () {
Navigator.push(context,MaterialPageRoute(builder: (context) => NewTrip()),);
},
),``
),
),
),
//add text
Expanded(
flex: 1,
child: Text(
'New trip',
style: TextStyle(
fontFamily: 'Nanum',
fontSize: 30.0,
),
),
),
]),
SizedBox(
height: 30.0,
),
//previous trip button
Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(
height: 100.0,
width: 100.0,
child: FittedBox(
child: FloatingActionButton(
elevation: 10.0,
backgroundColor: Colors.white,
onPressed: () {},
child: Icon(
Icons.assessment,
color: Colors.black,
),
),
),
),
),
//previous trip text
Expanded(
flex: 1,
child: Text(
'Previous trips',
style: TextStyle(
fontFamily: 'Nanum',
fontSize: 30.0,
),
),
)
],
),
SizedBox(
height: 50.0,
),
],
),
),
],
),
),
],
),
);
}
}
And the NewTrip widget is as follows
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Text('NEW TRIP'),
),
);
}
}
The home page is loading fine but as soon as I click the new trip button, it shows a black screen. Probably there is an issue with MaterialApp or Scaffold but I am not able to fix it yet. Can anyone tell me what's the problem and how to fix it?
Updated the full code as requested in the comments.
So in NewTrip() remove MaterialApp since it inherits from the parent. Just return Scaffold.
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('NEW TRIP'),
);
}
}
Okay so after some research on the internet, I found out that it's the FloatingActionButton that is causing the trouble.
I replaced the FloatingActionButton with MaterialButton and this fixed my issue.
Please check the full code edit from your code. Actually you are using two FloatingActionButton. So you need to use two heroTag with different name. I already added in the code. No problem with NewTrip class.
import 'package:flutter/material.dart';
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Page0(),
);
}
}
class Page0 extends StatefulWidget {
#override
_Page0State createState() => _Page0State();
}
class _Page0State extends State {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF493597),
body: ListView(
children: [
Padding(
padding: EdgeInsets.only(top: 15.0, left: 10.0),
),
SizedBox(
height: 25.0,
),
Padding(
padding: EdgeInsets.only(left: 40.0),
child: Row(
children: [
Text(
'Expense',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 25.0),
),
SizedBox(
width: 10.0,
),
Text(
'What',
style: TextStyle(
fontFamily: 'Montserrat',
color: Colors.white,
fontSize: 25.0,
),
),
],
),
),
SizedBox(height: 60.0),
Container(
margin: EdgeInsets.only(
left: 10.0,
right: 10.0,
),
height: MediaQuery.of(context).size.height - 150,
decoration: BoxDecoration(
color: Color(0xFFFCFCFC),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(75.0),
topRight: Radius.circular(75.0),
),
),
child: ListView(
primary: false,
padding: EdgeInsets.only(
left: 15.0,
right: 20.0,
top: 25.0,
),
children: [
Padding(
padding: const EdgeInsets.only(
top: 30.0,
),
child: Column(
children: [
//greeting text
Row(
children: [
Expanded(
child: Center(
child: Text(
'Hello! :)',
style: TextStyle(
fontFamily: 'Permanent-Marker',
color: Colors.black,
fontSize: 30.0,
),
),
),
),
],
),
SizedBox(
height: 30.0,
),
//add button
Row(children: [
Expanded(
flex: 1,
child: Container(
height: 100.0,
width: 100.0,
child: FittedBox(
child: FloatingActionButton(
heroTag: "btn",
elevation: 10.0,
backgroundColor: Colors.white,
child: Icon(
Icons.add,
color: Colors.black,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewTrip()),
);
},
),
),
),
),
//add text
Expanded(
flex: 1,
child: Text(
'New trip',
style: TextStyle(
fontFamily: 'Nanum',
fontSize: 30.0,
),
),
),
]),
SizedBox(
height: 30.0,
),
//previous trip button
Row(
children: [
Expanded(
flex: 1,
child: Container(
height: 100.0,
width: 100.0,
child: FittedBox(
child: FloatingActionButton(
heroTag: "btn1",
elevation: 10.0,
backgroundColor: Colors.white,
onPressed: () {},
child: Icon(
Icons.assessment,
color: Colors.black,
),
),
),
),
),
//previous trip text
Expanded(
flex: 1,
child: Text(
'Previous trips',
style: TextStyle(
fontFamily: 'Nanum',
fontSize: 30.0,
),
),
)
],
),
SizedBox(
height: 50.0,
),
],
),
),
],
),
),
],
),
);
}
}
NewTrip class
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('NEW TRIP'),
);
}
}
Before:
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Text('NEW TRIP'),
);
}
}
So to Solve this problem we wrap the new navigate widget with scaffold widget
Solution After:
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Material(
child: Text('NEW TRIP'),
),
);
}
}
The issue is that you are using a materialApp inside another materialApp
Navigator just changes the pages and we don't need separate materialApp in NewTrip().
So the NewTrip should be as follow
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('NEW TRIP'),
);
}
}
I believe there can only be one MaterialApp Widget in the entire Flutter application because MaterialApp Widget is likely to be the main or core component of Flutter. So when you try to navigate to a new screen try returning Scaffold Widget instead of MaterialApp Widget.
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('NEW TRIP'),
);
}
}
Use ThemeData, along with the parameter scaffoldBackgroundColor.
For example:
ThemeData (
scaffoldBackgroundColor: Colors.black,
)
In my case, it is caused by:
// to set portait screen orientation
SystemChrome.setPreferredOrientation([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
// Instead i use `android:screenOrientation="portrait"` on my AndroidManifest.xml.
That I declared when the page widget render. Removed it solve my black screen issue.
Change scaffoldBackgroundColor
theme: ThemeData(
primarySwatch: Colors.blue,
scaffoldBackgroundColor: Colors.white, //here
visualDensity: VisualDensity.adaptivePlatformDensity,
),
class NewTrip extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Text('NEW TRIP'),
);
}
}
simply just put scaffold in next page that's it.
If the page you navigate does not contain Scaffold the page will be black since Scaffold Implements the basic material design visual layout structure so apply Scaffold and the parent widget material app.
The problem you are facing because the widget you are navigating does not have any material widget.
Just wrap the navigated screen's return widget with Scaffold as a parent.