How to use a switch statement in a widget class - dart

I am trying to display a container with some designs if it gets some data from firestore but if there is no data from firestore I want it to show a text saying no data from firestore, but it is not working and I'm getting errors.
...............................................................................................................................
below is the code, is this correct? getStoriesFunction:
Widget _getStories() {
Live users;
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: getStory(users),
);
}
Widget getStory(Live user) {
return Container(
margin: EdgeInsets.all(12),
child: Column(
children: <Widget>[
user.isLive == true
? Container(
height: 55,
width: 55,
child: GestureDetector(
onTap: () {
// Join function
onJoin(
channelName: user.displayName,
channelId: user.channelId,
displayName: displayName,
hostImage: user.image,
userImage: image);
},
child: Stack(
alignment: Alignment(0, 0),
children: <Widget>[
Container(
height: 60,
width: 60,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [
Colors.red,
Colors.redAccent,
Colors.red
],
begin: Alignment.topLeft,
end: Alignment.bottomRight)),
),
),
Container(
height: 55.5,
width: 55.5,
child: CircleAvatar(
backgroundColor: Colors.black,
),
),
CachedNetworkImage(
imageUrl: user.image,
imageBuilder: (context, imageProvider) => Container(
width: 52.0,
height: 52.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider, fit: BoxFit.cover),
),
),
),
Container(
height: 70,
width: 70,
alignment: Alignment.bottomCenter,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
height: 17,
width: 25,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(
Radius.circular(
4.0) // <--- border radius here
),
gradient: LinearGradient(
colors: [Colors.black, Colors.black],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
),
Container(
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(
Radius.circular(
2.0) // <--- border radius here
),
gradient: LinearGradient(
colors: [
Colors.red,
Colors.redAccent
],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Text(
'LIVE',
style: TextStyle(
fontSize: 7,
color: Colors.white,
fontWeight: FontWeight.bold),
),
)),
],
))
],
),
))
: Text('No data in firestore',
style: TextStyle(
color: Colors.black,
fontSize: 12,
))
],
));
}

I have corrected you code and tested in the DartPad and work well for me. You can test to just copy and pase. It something wrong with user data from the firebase looks like bool isLive always true.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
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> {
// Fake user data
bool userExist = false;
void _toggleUserState() {
setState(() => userExist = !userExist);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_getStories(userExist),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggleUserState,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _getStories(bool user) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: getStory(user),
);
}
Widget getStory(bool user) {
return Container(
margin: EdgeInsets.all(12),
child: user == true
? Column(
children: <Widget>[
Container(
height: 55,
width: 55,
child: GestureDetector(
onTap: () {
// Join function
print('Join Function');
},
child: Stack(
alignment: Alignment(0, 0),
children: <Widget>[
Container(
height: 60,
width: 60,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [
Colors.red,
Colors.redAccent,
Colors.red
],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
),
),
),
Container(
height: 55.5,
width: 55.5,
child: CircleAvatar(
backgroundColor: Colors.black,
),
),
// Cached Image container here.
Container(
height: 55.5,
width: 55.5,
child: Text('Cached Image'),
),
Container(
height: 70,
width: 70,
alignment: Alignment.bottomCenter,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
height: 17,
width: 25,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
gradient: LinearGradient(
colors: [Colors.black, Colors.black],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
),
Container(
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius:
BorderRadius.all(Radius.circular(2.0)),
gradient: LinearGradient(
colors: [Colors.red, Colors.redAccent],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Text(
'LIVE',
style: TextStyle(
fontSize: 7,
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
),
],
),
)
],
),
),
)
],
)
: Text(
'No data in firestore',
style: TextStyle(
color: Colors.black,
fontSize: 12,
),
),
);
}
}

Related

Flutter other widget doesn`t show when I call my new created custom widget

Hello when ever I tried to call the bottombar widget (the custom widget that I created) The other widget elements dissapears
here is the home_page.dart code:
import 'package:clothing_app/utils/bottom_bar.dart';
import 'package:flutter/material.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int buttonSelected = 1;
#override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
bottomNavigationBar: BottomBar(),
appBar: AppBar(
elevation: 0,
backgroundColor: SCAFFOLD_BG_COLOR,
leading: IconButton(
onPressed: () {},
icon: Icon(
LineAwesomeIcons.bars,
color: MAIN_BLACK_COLOR,
),
),
actions: <Widget>[
IconButton(
onPressed: () {},
icon: Icon(
LineAwesomeIcons.search,
color: MAIN_BLACK_COLOR,
),
),
],
),
body: Column(
children: <Widget>[
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.symmetric(
vertical: 12, horizontal: 16),
scrollDirection: Axis.horizontal,
child: Row(
children: <Widget>[
_topSlideItem(),
SizedBox(
width: 24,
),
_topSlideItem(),
SizedBox(
width: 24,
),
_topSlideItem(),
SizedBox(
width: 24,
),
_topSlideItem(),
SizedBox(
width: 24,
),
_topSlideItem(),
SizedBox(
width: 24,
),
_topSlideItem(),
],
),
),
),
],
),
Padding(
padding:
EdgeInsets.symmetric(vertical: 12, horizontal: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Best of the week',
style: TextStyle(
fontSize: 22, fontWeight: FontWeight.bold),
),
SizedBox(
height: 24,
),
verticalSlideItem(),
SizedBox(height: 16),
verticalSlideItem(),
SizedBox(height: 16),
verticalSlideItem(),
SizedBox(height: 16),
verticalSlideItem(),
SizedBox(height: 16),
verticalSlideItem(),
SizedBox(height: 16),
verticalSlideItem(),
],
),
)
],
),
),
),
],
),
),
);
}
Widget _topSlideItem() {
return Material(
borderRadius: BorderRadius.circular(32),
color: Colors.white,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 32),
width: MediaQuery.of(context).size.width * .55,
child: Row(
children: <Widget>[
Image.asset(
'assets/img.jpeg',
height: 140,
width: 50,
fit: BoxFit.fitHeight,
),
SizedBox(width: 24),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Flower patterned khaki dress',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 12,
),
Row(
children: <Widget>[
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(4),
),
),
SizedBox(
width: 8,
),
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(4),
),
),
SizedBox(
width: 8,
),
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(4),
),
)
],
),
SizedBox(
height: 12,
),
Text(
'\$89,99',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Color.fromRGBO(180, 184, 190, 1),
),
),
],
),
)
],
),
),
);
}
Widget verticalSlideItem() {
return Material(
borderRadius: BorderRadius.circular(32),
color: Colors.transparent,
child: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(24)),
child: Center(
child: Image.asset(
'assets/img.jpeg',
height: 110,
width: 80,
fit: BoxFit.fitHeight,
),
),
),
SizedBox(width: 24),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Flower patterned\nkhaki dress',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 12,
),
Row(
children: <Widget>[
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(4),
),
),
SizedBox(
width: 8,
),
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(4),
),
),
SizedBox(
width: 8,
),
Container(
height: 12,
width: 12,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(4),
),
)
],
),
SizedBox(
height: 12,
),
Text(
'\$89,99',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Color.fromRGBO(180, 184, 190, 1),
),
),
],
),
)
],
),
),
);
}
}
here is the custom widget I created (bottom_bar.dart);
#override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.call),
label: 'Calls',
),
BottomNavigationBarItem(
icon: Icon(Icons.camera),
label: 'Camera',
),
BottomNavigationBarItem(
icon: Icon(Icons.chat),
label: 'Chats',
),
],
),
),
);
}
}
here is the main.dart file;
import 'package:flutter/material.dart';
void main() {
runApp(App());
}
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(fontFamily: 'Cera'),
home: HomePage(),
);
}
}
here is the image when I call the bottom navigationbar;
and here is an image when I remove the bottom navigationbar code line (bottomNavigationBar: BottomBar(),);
You are wrapping your custom BottomNavigationBar with Scaffold and it covers you HomePage Scaffold, so that's why the other widgets couldn't be shown.
Remove the Scaffold from the custom BottomNavigationBar:
#override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.call),
label: 'Calls',
),
BottomNavigationBarItem(
icon: Icon(Icons.camera),
label: 'Camera',
),
BottomNavigationBarItem(
icon: Icon(Icons.chat),
label: 'Chats',
),
],
);
}

Flitter: Display chosen item from list

So I got some cars with all the technical data (top speed, horsepower, etc.) saved in my backend and my app gets all this data. Like you can see in my first screenshot (SC1) the user chooses what kind of car he is looking for and when he presses on this yellow button a bottomsheet appears (SC2) (SC3) and he can select the different characteristics of the car.
What I'm trying to achieve is that the item which was chosen by the user is displayed when he closes the bottomsheet again. So basically it shall be displayed what was chosen, for example on (SC1) right behind the text "Brand", "Price", etc.
This is the code of the first Screenshot (SC1):
class SearchTab extends StatefulWidget {
const SearchTab({Key key}) : super(key: key);
#override
_SearchTabState createState() => _SearchTabState();
}
class _SearchTabState extends State<SearchTab> {
ScrollController _scrollController;
#override
void initState() {
_scrollController = ScrollController();
super.initState();
}
bool _more = false;
#override
Widget build(BuildContext context) {
final FilterProvider filterProvider = Provider.of<FilterProvider>(context);
return filterProvider.isLoading // --- DAS WAR DAVOR DA! ---
? Container(
constraints: const BoxConstraints.expand(),
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Color(0xffFEFDFD),
Color(0xffBDBDB2),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: CupertinoActivityIndicator(
// CircularProgressIndicator(
// color: Colors.grey,
// ),
),
),
)
: Container(
constraints: const BoxConstraints.expand(),
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0,
0), //Original: const EdgeInsets.fromLTRB(15, 20, 15, 0),
child: Scrollbar(
controller: _scrollController,
child: SingleChildScrollView(
physics: BouncingScrollPhysics(),
controller: _scrollController,
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: <Widget>[
FilterButtonWidget(
title: 'Brand',
item: constants.brand,
type: 'brand',
),
FilterButtonWidget(
title: 'Price',
item: constants.price,
type: 'price',
),
FilterButtonWidget(
title: 'Engine Power [kW]',
item: constants.enginepower,
type: 'engine',
),
FilterButtonWidget(
title: 'Range',
item: constants.range,
type: 'range',
),
FilterButtonWidget(
title: 'Battery [kWh]',
item: constants.battery,
type: 'battery',
),
FilterButtonWidget(
title: 'Loading Power [kW]',
item: constants.chargingpower,
type: 'charging',
),
FilterButtonWidget(
title: 'Top speed',
item: constants.topspeed,
type: 'top',
),
GestureDetector(
onTap: () {
setState(() => _more = !_more);
_scrollController.animateTo(
_more
? _scrollController.offset +
MediaQuery.of(context).size.height * .3
: _scrollController
.position.minScrollExtent,
curve: _more ? Curves.easeIn : Curves.easeOut,
duration: const Duration(milliseconds: 500));
},
const SizedBox(height: 20),
],
),
),
),
),
),
);
}
}
And this is the code of my FilterButtonWidget:
class FilterButtonWidget extends StatelessWidget {
const FilterButtonWidget({
Key key,
this.title,
this.item,
this.type,
}) : super(key: key);
final String title;
final dynamic item;
final String type;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Stack(
children: <Widget>[
Container(
height: 48,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: const LinearGradient(
colors: [Colors.white, Colors.white],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
boxShadow: const [
BoxShadow(
color: Colors.grey,
blurRadius: 0,
offset: Offset(0, 0),
),
],
),
),
Row(
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
),
Expanded(
flex: 1,
child: Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.fromLTRB(15, 0, 0, 0),
child: Text(
title,
style: const TextStyle(
fontFamily: 'Avenir',
fontSize: 16.0,
fontWeight: FontWeight.w500,
color: Colors.black,
),
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 4, 0),
child: TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(const Color(0xffF2F2F2)),
padding:
MaterialStateProperty.all(const EdgeInsets.all(0.0)),
shape: MaterialStateProperty.all(
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(7.0),
),
),
),
overlayColor: MaterialStateColor.resolveWith(
(states) => Colors.transparent),
foregroundColor: MaterialStateColor.resolveWith(
(states) => Color(0xff424242)),
),
//minWidth: 70,
child: Ink(
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [
Color(0xffFBD23E),
Color(0xffF6BE03),
],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
borderRadius: BorderRadius.circular(7.0)),
child: Container(
constraints:
const BoxConstraints(maxHeight: 40, maxWidth: 70),
alignment: Alignment.center,
child: const Icon(
Icons.arrow_forward_ios_rounded,
size: 22,
),
),
),
onPressed: () {
_bottomsheet(context);
},
),
),
],
),
],
),
);
}
void _bottomsheet(context) {
final FilterProvider filterProvider =
Provider.of<FilterProvider>(context, listen: false);
final _type = type;
showModalBottomSheet(
context: context,
isDismissible: true,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.60,
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(22.0),
topRight: Radius.circular(22.0),
),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 15, 20, 12),
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: const TextStyle(
color: Color(0xffF6BE03),
fontFamily: 'Avenir',
fontWeight: FontWeight.w600,
fontSize: 24.0,
),
),
// IconButton(
// onPressed: () {
// Navigator.pop(context);
// },
// icon: const Icon(Icons.arrow_downward),
// iconSize: 30,
// ),
],
),
),
if (type == 'body' || type == 'drive' || type == 'brand')
CheckboxWidget(
item: item,
type: type,
state: filterProvider.filter,
)
else if (type == 'seats')
CupertinoMultiPicker(
state: filterProvider.filter['seats'],
)
else
CupertinoPickerWidget(
item: item,
type: type,
)
],
),
),
),
).whenComplete(() => filterProvider.apply = true);
}
}

Resize Flutter FloatingActionButton

I can't figure out how to make the FAB smaller inside the first Container in the list. It seems to want to occupy the complete container, no matter what I try. Ive even tried a container within a container. The complete Orange area is clickable. I tried SizedBox, same result. Here is the code.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final title = 'Horizontal List';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Container(
margin: EdgeInsets.symmetric(vertical: 20.0),
padding: EdgeInsets.all(30),
height: 200.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Container(
width: 160,
color: Colors.yellowAccent,
child: SizedBox(
height: 50,
width: 50,
child: FittedBox(
child: FloatingActionButton(
backgroundColor: Colors.deepOrange,
foregroundColor: Colors.indigo,
child: Icon(Icons.add, size: 20),
onPressed: () {},
),
),
),
),
Container(
width: 160.0,
color: Colors.red,
),
Container(
width: 160.0,
color: Colors.blue,
),
Container(
width: 160.0,
color: Colors.green,
),
Container(
width: 160.0,
color: Colors.yellow,
),
Container(
width: 160.0,
color: Colors.orange,
),
],
),
),
),
);
}
}
I have 2 samples. And Please refer Basic Widgets for more detail.
Use margin of Container. Please refer the code as below:
child: SizedBox(
height: 50,
width: 50,
child: Container(
margin: EdgeInsets.only(left:80.0, top:80.0, bottom: 10.0) ,
child: FloatingActionButton(
backgroundColor: Colors.deepOrange,
foregroundColor: Colors.indigo,
child: Icon(Icons.add),
onPressed: () {},
),
),
),
Use Row or Column. Please refer the code as below:
child: SizedBox(
height: 50,
width: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[ FloatingActionButton(
backgroundColor: Colors.deepOrange,
foregroundColor: Colors.indigo,
child: Icon(Icons.add),
onPressed: () {},
),
]),
),

How do I add SingleChildScrollView to a column

I'm developing an app and was following a sample from this link and ran into a problem where it says bottom overflowed by xx pixels as shown below:
Now, I encountered this before and solved it by adding SingleChildScrollView as shown below:
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Academy',
theme: new ThemeData(
primaryColor: Color.fromRGBO(58, 66, 86, 1.0)
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: new Container(
child: SingleChildScrollView(
child: LoginScreen(),
)
),
),
);
}
How do I do it when I'm not using a container but a column instead ? As that is what I'm using in the image above (column at the bottom of code, where the author returns the scaffold):
class DetailPage extends StatelessWidget {
final Lesson lesson;
DetailPage({Key key, this.lesson}) : super(key: key);
#override
Widget build(BuildContext context) {
final levelIndicator = Container(
child: Container(
child: LinearProgressIndicator(
backgroundColor: Color.fromRGBO(209, 224, 224, 0.2),
value: lesson.indicatorValue,
valueColor: AlwaysStoppedAnimation(Colors.green)),
),
);
final coursePrice = Container(
padding: const EdgeInsets.all(7.0),
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(5.0)),
child: new Text(
"\$" + lesson.price.toString(),
style: TextStyle(color: Colors.white),
),
);
final topContentText = Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 120.0),
Icon(
Icons.directions_car,
color: Colors.white,
size: 40.0,
),
Container(
width: 90.0,
child: new Divider(color: Colors.green),
),
SizedBox(height: 10.0),
Text(
lesson.title,
style: TextStyle(color: Colors.white, fontSize: 45.0),
),
SizedBox(height: 30.0),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(flex: 1, child: levelIndicator),
Expanded(
flex: 6,
child: Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text(
lesson.level,
style: TextStyle(color: Colors.white),
))),
Expanded(flex: 1, child: coursePrice)
],
),
],
);
final topContent = Stack(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 10.0),
height: MediaQuery.of(context).size.height * 0.5,
decoration: new BoxDecoration(
image: new DecorationImage(
image: new AssetImage("assets/images/drones1.jpg"),
fit: BoxFit.cover,
),
)),
Container(
height: MediaQuery.of(context).size.height * 0.5,
padding: EdgeInsets.all(40.0),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Color.fromRGBO(58, 66, 86, .9)),
child: Center(
child: topContentText,
),
),
Positioned(
left: 8.0,
top: 60.0,
child: InkWell(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back, color: Colors.white),
),
)
],
);
final bottomContentText = Text(
lesson.content,
style: TextStyle(fontSize: 18.0),
);
final readButton = Container(
padding: EdgeInsets.symmetric(vertical: 16.0),
width: MediaQuery.of(context).size.width,
child: RaisedButton(
onPressed: () => {},
color: Color.fromRGBO(58, 66, 86, 1.0),
child:
Text("TAKE THIS LESSON", style: TextStyle(color: Colors.white)),
));
final bottomContent = Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(40.0),
child: Center(
child: Column(
children: <Widget>[bottomContentText, readButton],
),
),
);
return Scaffold(
body: Column( //here is the column
children: <Widget>[topContent, bottomContent],
),
);
}
}
I tried these but it does not work, giving me the same result as before:
return Scaffold(
body: Container(child: SingleChildScrollView(child:Column(children: <Widget>[topContent, bottomContent],),),),
);
return Scaffold(
body: Column(
children: <Widget>[Expanded(child: Column(children: <Widget>[topContent, bottomContent],))],
),
);
return Scaffold(
body: SingleChildScrollView(child: Container(child:Column(children: <Widget>[topContent, bottomContent],),),),
);
My outcome when I tried this as suggested:
return Scaffold(
body: Column( //here is the column
children: <Widget>[Expanded(child: SingleChildScrollView(child: Text("Test"))), Text("Data")],
),
);
I solved it by adding the SingleChildScrollView to the following in topContent:
Container(
height: MediaQuery.of(context).size.height * 0.5,
padding: EdgeInsets.all(40.0),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(color: Color.fromRGBO(58, 66, 86, .9)),
child: SingleChildScrollView(
child: Center(
child: topContentText,
),
),
),

Inkwell not showing ripple when used with Container decoration

I want to add a ripple on an item, it is working fine until I add a gradient on the item using BoxDecoration.
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
shadowColor: Colors.grey[50],
child: InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
Colors.yellow[800],
Colors.yellow[700],
],
),
),
padding: EdgeInsets.all(16.0),
child: Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
);
}
Update in 2019:
You should use Ink widget inside Material, instead of Container.
It takes decoration parameter as well:
Material(
child: Ink(
decoration: BoxDecoration(
// ...
),
child: InkWell(
onTap: () {},
child: child, // other widget
),
),
);
I found the solution:
I need one Material for Inkwell, and one Material for elevation and rounded borders.
The inner Material has a type of MaterialType.transparency so that it doesn't draw anything over the box decoration of its parent and still preserve the ink effect. The shadow and borders are controlled by outer Material.
Container(
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material( // <----------------------------- Outer Material
shadowColor: Colors.grey[50],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
elevation: 6.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
colors: [
AppColors.pinkDark,
AppColors.pink,
],
),
),
child: Material( // <------------------------- Inner Material
type: MaterialType.transparency,
elevation: 6.0,
color: Colors.transparent,
shadowColor: Colors.grey[50],
child: InkWell( //<------------------------- InkWell
splashColor: Colors.white30,
onTap: () {},
child: Container(
padding: EdgeInsets.all(16.0),
child: Row(
children: <Widget>[
Icon(
Icons.work,
size: 40.0,
color: Colors.white,
),
SizedBox(
width: 20.0,
),
Column(
children: <Widget>[
Text(
widget.title,
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
],
)
],
),
),
),
),
),
),
);
Simple splash effect widget I created that works perfect.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class SplashEffect extends StatelessWidget {
final Widget child;
final Function onTap;
const SplashEffect({Key key, this.child, this.onTap}) : super(key: key);
#override
Widget build(BuildContext context) {
return Material(
type: MaterialType.transparency,
child: InkWell(
borderRadius: BorderRadius.all(Radius.circular(16)),
child: child,
onTap: onTap,
),
);
}
}
Splash color is overlap by container BoxDecoration
Try this
Widget build(BuildContext context) {
return new Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(new Radius.circular(4.0)),
gradient: LinearGradient(
begin: AlignmentDirectional.bottomStart,
end: AlignmentDirectional.topEnd,
tileMode: TileMode.repeated,
colors: [
Colors.yellow[800],
Colors.yellow[700],
],
),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.grey[50],
//blurRadius: 0.3,
blurRadius: 6.0,
offset: new Offset(0.0, 4.0)
)
]
),
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Material(
color: Colors.transparent,
//shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
//elevation: 6.0,
//shadowColor: Colors.grey[50],
child: InkWell(
splashColor: const Color(0x8034b0fc),
onTap: () {},
child: Container(
//decoration: ,
padding: EdgeInsets.all(16.0),
child: Text(
'Click',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
),
),
),
);
}
If anyone came here looking to do use an inkwell with a circle decoration (like I did), I used the accepted answer to come up with this.
Material(
child: Ink(
width: 150,
height: 150,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[350],
border: Border.all(
color: Colors.red,
width: 4.0,
),
),
child: InkWell(
customBorder: const CircleBorder(),
onTap: onTap,
child: const Icon(Icons.add, size: 48, color: Colors.white),
),
));

Resources