Flutter text and stack widget spaced evenly - flutter

I am trying to create a status indicator in flutter with text label on left side and stack widget on right side, I manage to arrange them using Row but the width of each widgets is not separated evenly and the stack max width overflows with the container.
Here is my code so far.
import 'package:flutter/material.dart';
class CustomIndicatorBar extends StatelessWidget {
const CustomIndicatorBar({
Key key,
#required this.percent,
#required this.title,
}) : super(key: key);
final double percent;
final String title;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final double maxBarWidth = constraints.maxWidth;
double barWidth = percent * maxBarWidth;
if (barWidth < 0) {
barWidth = 0;
}
return Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
Stack(
children: [
Container(
height: 20.0,
decoration: const BoxDecoration(
color: Color(0xFF555454),
),
),
Container(
height: 20.0,
width: barWidth,
decoration: const BoxDecoration(
color: Color(0xFFFA9F1D),
),
)
],
)
],
),
);
},
),
);
}
}
Here is the output
What I want to achieve is to space them out evenly and stack width will not overflow from the container.

Please try my solution:
The result:
Full code:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
title: 'Demo',
home: FirstRoute(),
));
}
class FirstRoute extends StatefulWidget {
const FirstRoute({Key? key}) : super(key: key);
#override
State<FirstRoute> createState() => _FirstRouteState();
}
class _FirstRouteState extends State<FirstRoute> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Route'),
),
body: Column(
children: const [
SizedBox(height: 16),
CustomIndicatorBar(
title: 'Mood',
percent: 0.5,
),
SizedBox(height: 16),
CustomIndicatorBar(
title: 'Hunger',
percent: 0.7,
),
],
),
);
}
}
class CustomIndicatorBar extends StatelessWidget {
const CustomIndicatorBar({
Key? key,
required this.percent,
required this.title,
}) : super(key: key);
final double percent;
final String title;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Text(
title,
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Expanded(
child: Stack(
children: [
Container(
height: 20.0,
decoration: const BoxDecoration(
color: Color(0xFF555454),
),
),
LayoutBuilder(builder: (context, constraints) {
return Container(
height: 20.0,
width: constraints.maxWidth * percent,
decoration: const BoxDecoration(
color: Color(0xFFFA9F1D),
),
);
})
],
),
)
],
),
);
}
}

Related

How can I create a ListView correctly?

I'm not able to create a Listview in Flutter because of when I create a Listview of widgets the screen stays empty, it's something like that 1
This is the Code that I wrote and returns a list view:
import 'package:dietapp/pages/homepage.dart';
import 'package:dietapp/pages/list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:dietapp/pages/profile.dart';
import 'package:dietapp/pages/createReg.dart';
import 'package:percent_indicator/percent_indicator.dart';
void main() {}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
const SafeArea(child: TopBar()),
const Align(
alignment: Alignment.topLeft,
child: Padding(
padding: EdgeInsets.only(left: 25, bottom: 20),
child: Text('Seguiment Diari', style: TextStyle(fontSize: 20)),
)),
Align(alignment: Alignment.center, child: TypesListView()),
],
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const CreateReg()));
},
label: const Text('Crear'),
icon: const Icon(Icons.add),
),
);
}
}
class TopBar extends StatelessWidget {
const TopBar({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(25.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
"Dietapp",
style: TextStyle(
color: Colors.black, fontSize: 30, fontWeight: FontWeight.bold),
),
],
),
);
}
}
class TotalLabel extends StatefulWidget {
final String typeOf;
final String subtitle;
final Function() onPressed;
final double fillBar;
const TotalLabel(
{required this.typeOf,
required this.subtitle,
required this.onPressed,
required this.fillBar,
Key? key})
: super(key: key);
#override
State<TotalLabel> createState() => _TotalLabelState();
}
class _TotalLabelState extends State<TotalLabel> {
Color getColor(double fillBar) {
if (fillBar < 0.5) {
return Colors.orange;
} else {
return Colors.green;
}
}
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onPressed,
child: Container(
width: 350,
height: 125,
padding: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12.5),
boxShadow: [
BoxShadow(
offset: const Offset(10, 20),
blurRadius: 10,
spreadRadius: 0,
color: Colors.grey.withOpacity(.05)),
],
),
child: Column(
children: [
Text(widget.typeOf,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20,
)),
const SizedBox(
height: 5,
),
Text(
widget.subtitle,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.grey,
fontWeight: FontWeight.normal,
fontSize: 12),
),
const SizedBox(
height: 10,
),
const Spacer(),
LinearPercentIndicator(
width: 300,
lineHeight: 10,
barRadius: const Radius.circular(50),
backgroundColor: Colors.black12,
progressColor: getColor(widget.fillBar),
percent: widget.fillBar,
),
const Spacer()
],
),
),
);
}
}
class TypesListView extends StatelessWidget {
const TypesListView({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return ListView(
padding: const EdgeInsets.all(8),
children: <Widget>[
TotalLabel(
typeOf: 'Proteines',
subtitle: 'Range',
onPressed: () {},
fillBar: 0.2),
],
);
}
}
When I run the code, the error view is the following:
I have also tried to use a Stateless widget returning a list view but didn't worked.
Thanks you so much :)
The following is an example of how to use a ListView. Note that I created a MaterialApp since ListView is a Material Widget. You can replace ListViewExample with your own Widget containing a ListView.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ListView Example',
home: ListViewExample(),
);
}
}
class ListViewExample extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ListView(
padding: const EdgeInsets.all(8),
children: <Widget>[
Text('Text Widget 1'),
Text('Text Widget 2'),
Text('Text Widget 3'),
],
);
}
}
ListView.builder(
itemCount: 5
itemBuilder: (context, index) {
return Card(
child: Padding(
padding: const EdgeInsets.all(10),
child: Text("Some text $index")
),
);
}),
More about listview

Tried creating tabs in product detail page in flutter. But tabs is in stateful widget. When I inserted tab class name in home class, getting error

I need to insert tabs below elevated button. When I am using it as separate file, its working fine. Please suggest any ways to insert tabs in this code. I am in the learning stage of flutter.Here is the code link on codepen.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(home: Home());
}
}
class TabsPart extends StatefulWidget {
#override
_TabsPartState createState() => _TabsPartState();
}
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
Positioned(
child: Container(
alignment: Alignment.topCenter,
decoration: const BoxDecoration(
image: DecorationImage(
alignment: Alignment.bottomRight,
fit: BoxFit.cover,
image: AssetImage('assets/images/img1.png'))),
),
),
Positioned(
bottom: 0,
child: Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(14),
),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Container(
margin: const EdgeInsets.only(bottom: 16),
width: 12 * 1.5,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(3),
),
),
),
const ProductNameAndPrice(),
const Spacing(),
const ProductDesc(),
const Spacing(),
const SizedBox(
height: 16,
),
ElevatedButton(
style: ElevatedButton.styleFrom(
onSurface: Colors.white),
onPressed: null,
child: Text('Add to Cart',
style: TextStyle(
color: Colors.white, fontSize: 16)),
),
const Spacing(),
],
),
),
))
],
),
),
),
),
);
}
}
class Spacing extends StatelessWidget {
const Spacing({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return const SizedBox(
height: 16,
);
}
}
class RectButton extends StatelessWidget {
final String label;
const RectButton({
Key? key,
required this.label,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 32,
width: 32,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9),
border: Border.all(color: Colors.white)),
child: Center(
child: Text(
label,
style: TextStyle(color: Colors.white, fontSize: 20),
)),
);
}
}
class ProductNameAndPrice extends StatelessWidget {
const ProductNameAndPrice({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Product 1',
style: TextStyle(
color: Color.fromARGB(255, 250, 235, 235), fontSize: 30),
),
Text(
'\u{20B9}${150}',
style: TextStyle(color: Colors.white, fontSize: 20),
),
],
);
}
}
class ProductDesc extends StatelessWidget {
const ProductDesc({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'This is a good product with \nexcellent quality. purchase it as soon as possible! \nFew numbers only Left in stocks',
style: TextStyle(color: Colors.white, fontSize: 16, height: 1.6)),
],
);
}
}
class _TabsPartState extends State<TabsPart> {
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(
icon: Icon(Icons.android),
text: "Tab 1",
),
Tab(icon: Icon(Icons.phone_iphone), text: "Tab 2"),
],
),
title: Text('TutorialKart - TabBar & TabBarView'),
),
body: TabBarView(
children: [
Center(child: Text("Page 1")),
Center(child: Text("Page 2")),
],
),
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(home: Home());
}
}
class Home extends StatefulWidget {
#override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
late TabController _tabController;
#override
void initState() {
_tabController = TabController(length: 2, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
const ProductNameAndPrice(),
const ProductDesc(),
const SizedBox(height: 16),
ElevatedButton(
style: ElevatedButton.styleFrom(onSurface: Colors.black),
onPressed: null,
child: Text('Add to Cart',
style: TextStyle(color: Colors.black, fontSize: 16)),
),
TabBar(
indicatorColor: Colors.grey,
controller: _tabController,
tabs: [
const Tab(
icon: Icon(
Icons.widgets,
color: Colors.black,
),
),
Tab(
icon: Icon(
Icons.widgets,
color: Colors.black,
),
),
],
),
Expanded(
child: TabBarView(
controller: _tabController,
children: const [
Text("POSTS"),
Text("REELS"),
],
),
),
]),
);
}
}
class RectButton extends StatelessWidget {
final String label;
const RectButton({
Key? key,
required this.label,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 32,
width: 32,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(9),
border: Border.all(color: Colors.white)),
child: Center(
child: Text(
label,
style: TextStyle(color: Colors.white, fontSize: 20),
)),
);
}
}
class ProductNameAndPrice extends StatelessWidget {
const ProductNameAndPrice({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Product 1',
style: TextStyle(color: Colors.black, fontSize: 30),
),
Text(
'\u{20B9}${150}',
style: TextStyle(color: Colors.black, fontSize: 20),
),
],
);
}
}
class ProductDesc extends StatelessWidget {
const ProductDesc({
Key? key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'This is a good product with \nexcellent quality. purchase it as soon as possible! \nFew numbers only Left in stocks',
style: TextStyle(color: Colors.black, fontSize: 16, height: 1.6)),
],
);
}
}
try this

No errors in Flutter but custom widgets not showing

I am trying to make a ResuseableCard custom widget and IconContent custom widget.
Problem: Reuseable card are empty. IconContent is not showing inside ReusueableCard
What I have tried:
I also failed at creating a cardChild property within Reuseable Card that has a Column containing the icon, sized box and label. I did not see any errors when I tried this but the cards remained empty!
What has worked: When everything is spelled out within ReuseableCard (no separate IconContent widget or cardChild property) it works. But then all the cards look the same.
Another thing that works is to separately code each card. I don't want repeated code. The tutorial I follow works so why doesn't my code?
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
const bottomContainerHeight = 80.0;
const activeCardColor = Color(0xFF1D1E33);
const bottomContainerColor = Color(0xFFEB1555);
const labelColor = Color(0xFF76FF03);
class InputPage extends StatefulWidget {
#override
_ InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF0A0E21),
title: Text('BMI CALCULATOR'),
centerTitle: true,
),
body: Column(
children: [
Expanded(
child: Row(children: [
Expanded(
child: ReuseableCard(
colour: activeCardColor,
cardChild: IconContent(FontAwesomeIcons.venus, 'Female'),
),
),
Expanded(
child: ReuseableCard(
colour: activeCardColor,
cardChild: Column(children: [
Icon(FontAwesomeIcons.mars, size: 80.0),
SizedBox(height: 15.0),
Text('Male',
style: TextStyle(
fontSize: 18.0, color: (Color(0xFF8d8E98)))),
]),
)),
])),
Expanded(
child: ReuseableCard(colour: activeCardColor),
),
Expanded(
child: Row(
children: [
Expanded(
child: ReuseableCard(colour: activeCardColor),
),
Expanded(
child: ReuseableCard(colour: activeCardColor),
)
],
),
),
Container(
color: bottomContainerColor,
margin: EdgeInsets.only(top: 10.0),
width: double.infinity,
height: 80.0,
)
],
));
}
}
class ReuseableCard extends StatelessWidget {
ReuseableCard({#required colour, cardChild});
Color? colour;
Widget? IconContent;
#override
Widget build(BuildContext context) {
return Container(
child: IconContent,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: Color(0xFF1D1E33),
borderRadius: BorderRadius.circular(10.0)));
}
}
class IconContent extends StatelessWidget {
IconData? data;
String label = 'label';
IconContent(data, label);
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(data, size: 80.0),
SizedBox(height: 15.0),
Text(label,
style: TextStyle(
fontSize: 18.0,
color: labelColor,
))
]);
}
}
You have not defined cardChild properly and are not calling it in the ReuseableCard. I have made all the fields as required in the below code.
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
const bottomContainerHeight = 80.0;
const activeCardColor = Color(0xFF1D1E33);
const bottomContainerColor = Color(0xFFEB1555);
const labelColor = Color(0xFF76FF03);
class InputPage extends StatefulWidget {
#override
_ InputPageState createState() => _InputPageState(); }
class _InputPageState extends State<InputPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF0A0E21),
title: Text('BMI CALCULATOR'),
centerTitle: true,
),
body: Column(
children: [
Expanded(
child: Row(children: [
Expanded(
child: ReuseableCard(
colour: activeCardColor,
cardChild: IconContent(data:FontAwesomeIcons.venus,label: 'Female'),
),
),
Expanded(
child: ReuseableCard(
colour: activeCardColor,
cardChild: Column(children: [
Icon(FontAwesomeIcons.mars, size: 80.0),
SizedBox(height: 15.0),
Text('Male',
style: TextStyle(
fontSize: 18.0, color: (Color(0xFF8d8E98)))),
]),
)),
])),
Expanded(
child: ReuseableCard(colour: activeCardColor),
),
Expanded(
child: Row(
children: [
Expanded(
child: ReuseableCard(colour: activeCardColor),
),
Expanded(
child: ReuseableCard(colour: activeCardColor),
)
],
),
),
Container(
color: bottomContainerColor,
margin: EdgeInsets.only(top: 10.0),
width: double.infinity,
height: 80.0,
)
],
));
}
}
class ReuseableCard extends StatelessWidget {
ReuseableCard({#required this.colour,#required this.cardChild});
Color? colour;
Widget? cardChild;//cardChild defined here so the ReuseableCard can use it
#override
Widget build(BuildContext context) {
return Container(
child: cardChild,//called here
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: Color(0xFF1D1E33),
borderRadius: BorderRadius.circular(10.0)));
}
}
class IconContent extends StatelessWidget {
IconData? data;
String? label;
IconContent({#required this.data, #required this.label});
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(data, size: 80.0),
SizedBox(height: 15.0),
Text(label??"label",
style: TextStyle(
fontSize: 18.0,
color: labelColor,
))
]);
}
}
I think this will do.
class ReuseableCard extends StatelessWidget {
ReuseableCard({Key? key,required this.colour, this.cardChild}) : super(key: key);
final Color colour;
final Widget? cardChild;
#override
Widget build(BuildContext context) {
return Container(
child: cardChild?? const SizedBox(),
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: Color(0xFF1D1E33),
borderRadius: BorderRadius.circular(10.0)));
}
}
when you use
required
you don't have to say it can be null, also don't user widgets directly if it can be null.
if you want more help with your design, provide us your UI design what you trying to create.

How to remove InkWell hover color overflow outside parent ListView in Flutter?

I have a page with some static content and a ListView with custom list tiles using InkWell. Here's my issue: if you hover the cursor over an InkWell near the top or bottom of the ListView and start to scroll, the hover color shows up outside the bounds of the ListView even though the other content is clipped as you'd expect. I need a hover state on the list items, but don't want the hover color extending past the intended bounds of the ListView. Is there a way to make sure the ListView clips the hover color for those InkWells too or should I be looking at a different/custom widget solution? I've tried wrapping the InkWell with different parents (Container, etc) and experimented with setting clipBehavior in a couple places, but no avail.
Here's a simple case replicated from a random Flutter example. Note the InkWell in CustomListItem and the ListView in MyStatelessWidget.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatelessWidget(),
),
);
}
}
class CustomListItem extends StatelessWidget {
const CustomListItem({
Key? key,
required this.thumbnail,
required this.title,
required this.user,
required this.viewCount,
}) : super(key: key);
final Widget thumbnail;
final String title;
final String user;
final int viewCount;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0),
child: InkWell(
onTap: () {},
hoverColor: Colors.pink,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 2,
child: thumbnail,
),
Expanded(
flex: 3,
child: _VideoDescription(
title: title,
user: user,
viewCount: viewCount,
),
),
const Icon(
Icons.more_vert,
size: 16.0,
),
],
),),
);
}
}
class _VideoDescription extends StatelessWidget {
const _VideoDescription({
Key? key,
required this.title,
required this.user,
required this.viewCount,
}) : super(key: key);
final String title;
final String user;
final int viewCount;
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 0.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
title,
style: const TextStyle(
fontWeight: FontWeight.w500,
fontSize: 14.0,
),
),
const Padding(padding: EdgeInsets.symmetric(vertical: 2.0)),
Text(
user,
style: const TextStyle(fontSize: 10.0),
),
const Padding(padding: EdgeInsets.symmetric(vertical: 1.0)),
Text(
'$viewCount views',
style: const TextStyle(fontSize: 10.0),
),
],
),
);
}
}
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Column(
children: [
Container(
padding: const EdgeInsets.only(bottom: 20),
child: const Text('Hover over the first list item and scroll... the content is clipped, but not the hover color...'),
),
Expanded(
child: ListView(
padding: const EdgeInsets.all(8.0),
itemExtent: 106.0,
children: <CustomListItem>[
CustomListItem(
user: 'Flutter1',
viewCount: 999001,
thumbnail: Container(
decoration: const BoxDecoration(color: Colors.blue),
),
title: 'Test One...',
),
CustomListItem(
user: 'Flutter2',
viewCount: 999002,
thumbnail: Container(
decoration: const BoxDecoration(color: Colors.orange),
),
title: 'Test Two...',
),
CustomListItem(
user: 'Flutter3',
viewCount: 999003,
thumbnail: Container(
decoration: const BoxDecoration(color: Colors.blue),
),
title: 'Test Three...',
),
CustomListItem(
user: 'Flutter4',
viewCount: 999004,
thumbnail: Container(
decoration: const BoxDecoration(color: Colors.orange),
),
title: 'Test Four...',
),
CustomListItem(
user: 'Flutter5',
viewCount: 999005,
thumbnail: Container(
decoration: const BoxDecoration(color: Colors.blue),
),
title: 'Test Five...',
),
],
),
),
],
);
}
}
Images for visual reference:
you can use HoverWidget and HoverContainerfor specified List view.
HoverWidget(
hoverChild: Container(
height: 200,
width: 200,
color: Colors.green,
child: Center(child: Text('Hover Me..')),
)
HoverContainer(
width: 200,
height: 200,
hoverHeight: 220,
hoverWidth: 220,
color: Colors.red,
hoverColor: Colors.green,
)
More details on its official site:https://pub.dev/packages/hovering/example
wrap Ink or InkWell with Material.
return Padding(
padding: const EdgeInsets.symmetric(vertical: 5.0),
child: Material(
child: InkWell(
onTap: () {},
hoverColor: Colors.pink,
child: Row
Refer to #73315
Set hoverColor: Colors.transparent

Horizontal bar charts in Flutter

I'm trying to achieve a horizontal bar chart in flutter. I came across these packages fl_charts and charts_flutter. but I need to customise the looks to match with the following image I attached.
If any champ can support me in giving me steps to how to achieve this or an exemplary code will be so much helpful.
Thank you so much in advance.
Use LayoutBuilder to know the possible width, and IntrinsicWidth to calculate the place of title and numbers, but at the same time don't shrink the long title text.
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,
),
home: Scaffold(
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_title('Food Weight(grams)'),
Padding(
padding: const EdgeInsets.only(left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChartLine(title: 'Fat', number: 1800, rate: 1),
ChartLine(title: 'Protein', number: 600, rate: 0.4)
],
),
),
_title('Ratios'),
Padding(
padding: const EdgeInsets.only(left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ChartLine(
title: 'Calculum/Phosporous ratio = 2:1',
rate: 0.5,
),
ChartLine(
title: 'Omega3/6 ratio = 1:4',
rate: 0.4,
),
],
),
),
],
),
);
}
Widget _title(String title) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0, top: 4),
child: Text(
title,
style: TextStyle(fontSize: 18),
),
);
}
}
class ChartLine extends StatelessWidget {
const ChartLine({
Key key,
#required this.rate,
#required this.title,
this.number,
}) : assert(title != null),
assert(rate != null),
assert(rate > 0),
assert(rate <= 1),
super(key: key);
final double rate;
final String title;
final int number;
#override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
final lineWidget = constraints.maxWidth * rate;
return Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
constraints: BoxConstraints(minWidth: lineWidget),
child: IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
),
),
if (number != null)
Text(
number.toString(),
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
],
),
),
),
Container(
color: Colors.blue,
height: 60,
width: lineWidget,
),
],
),
);
});
}
}
You can use the Syncfusion's Flutter charts package, as I am using now for my application. Using Syncfusion's SfCartesianChart widget, render the bar chart along with data labels and by changing the data label's default position your output can be achieved. This widget works on Android, iOS, and web platforms.
Package- https://pub.dev/packages/syncfusion_flutter_charts
Features- https://www.syncfusion.com/flutter-widgets/flutter-charts
Documentation- https://help.syncfusion.com/flutter/cartesian-charts/overview