Flutter Problem: How to use Marquee text in flutter? - flutter

I want to use Marquee Where is my Text Widget.
When I am using marquee by removing text widget After using marquee then my text is disappearing and getting more error in console.
so please help me to use marquee.
import 'package:flutter/material.dart';
import 'package:hospital/CartIcon/cart_icon.dart';
import 'package:hospital/Drawer/dropdown_menu.dart';
import 'package:hospital/MyOrderPage/MyOrderDetailsViewPage/myOrderDetailsView.dart';
import 'package:hospital/constant.dart';
import 'package:intl/intl.dart';
import 'package:marquee/marquee.dart';
class MyOrderPage extends StatefulWidget {
const MyOrderPage({Key key}) : super(key: key);
#override
_MyOrderPageState createState() => _MyOrderPageState();
}
class _MyOrderPageState extends State<MyOrderPage> {
// final DateTime orderDate = DateTime.now();
final DateTime orderDate = DateTime.now();
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: kGreen,
title: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"My Order",
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
actions: [CartIcon(), DropDownMenu()],
),
body: Column(
children: [
Container(
// height: 200,
padding: EdgeInsets.all(8),
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Theme.of(context).primaryColor.withOpacity(0.3),
blurRadius: 8,
)
],
borderRadius: BorderRadius.circular(8),
color: Colors.white,
),
child: Padding(
padding: EdgeInsets.all(15),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Flexible(
//I want to use marquee user instead of text widget here
//Marquee(
// text: 'GeeksforGeeks is a one-stop destination for programmers.',
// style: TextStyle(fontWeight: FontWeight.bold),
//scrollAxis: Axis.horizontal,
// crossAxisAlignment: CrossAxisAlignment.start,
// blankSpace: 20.0,
// velocity: 100.0,
// pauseAfterRound: Duration(seconds: 1),
//showFadingOnlyWhenScrolling: true,
// fadingEdgeStartFraction: 0.1,
// fadingEdgeEndFraction: 0.1,
// numberOfRounds: 3,
//startPadding: 10.0,
// accelerationDuration: Duration(seconds: 1),
// accelerationCurve: Curves.linear,
// decelerationDuration: Duration(milliseconds: 500),
// decelerationCurve: Curves.easeOut,
//)
child: Text(
'Spondylolysis,Pittasnadhanak,D_stone,Natural Tulsi',
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
),
),
],
),
SizedBox(
height: 10,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Text('20-07-2021',
style: TextStyle(
color: Colors.black54,
// fontWeight: FontWeight.bold,
fontSize: 20)),
],
),
SizedBox(
height: 6,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Delivery Status',
style: TextStyle(
color: Colors.black54,
// fontWeight: FontWeight.bold,
fontSize: 20)),
Text('Delivered',
style: Theme.of(context)
.textTheme
.headline6
.copyWith(color: Colors.green)),
],
)
],
),
SizedBox(
height: 4,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Text('Quantity: ',
style: TextStyle(
color: Colors.black54,
// fontWeight: FontWeight.bold,
fontSize: 20)),
Padding(
padding: const EdgeInsets.only(left: 4),
child: Text('3',
style: TextStyle(
color: Colors.black,
// fontWeight: FontWeight.bold,
fontSize: 18)),
),
],
),
Row(
children: <Widget>[
Text('Totat Amount: ',
style: TextStyle(
color: Colors.black54,
// fontWeight: FontWeight.bold,
fontSize: 20)),
Padding(
padding: const EdgeInsets.only(left: 15),
child: Text('324' + '\$',
//total amount
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 20)),
),
],
)
],
)
],
),
),
)),
SizedBox(
height: 5,
),
],
),
);
}
}
I want to use Marquee Where is my Text Widget.
When I am using marquee by removing text widget After using marquee then my text is disappearing and getting more error in console.

class MarqueeWidget extends StatefulWidget {
final Widget child;
final Axis direction;
final Duration animationDuration, backDuration, pauseDuration;
MarqueeWidget({
#required this.child,
this.direction: Axis.horizontal,
this.animationDuration: const Duration(milliseconds: 3000),
this.backDuration: const Duration(milliseconds: 800),
this.pauseDuration: const Duration(milliseconds: 800),
});
#override
_MarqueeWidgetState createState() => _MarqueeWidgetState();
}
class _MarqueeWidgetState extends State<MarqueeWidget> {
ScrollController scrollController;
#override
void initState() {
scrollController = ScrollController(initialScrollOffset: 50.0);
WidgetsBinding.instance.addPostFrameCallback(scroll);
super.initState();
}
#override
void dispose(){
scrollController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: widget.child,
scrollDirection: widget.direction,
controller: scrollController,
);
}
void scroll(_) async {
while (scrollController.hasClients) {
await Future.delayed(widget.pauseDuration);
if(scrollController.hasClients)
await scrollController.animateTo(
scrollController.position.maxScrollExtent,
duration: widget.animationDuration,
curve: Curves.ease);
await Future.delayed(widget.pauseDuration);
if(scrollController.hasClients)
await scrollController.animateTo(0.0,
duration: widget.backDuration, curve: Curves.easeOut);
}
}
}
class MarqueeText extends StatefulWidget {
#override
_MarqueeTextState createState() => _MarqueeTextState();
}
class _MarqueeTextState extends State<MarqueeText> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Marquee Text"),
),
body: Center(
child: SizedBox(
width: 200.0,
child: MarqueeWidget(
direction: Axis.horizontal,
child: Text(
"your text"))),
));
}
}

Related

Dynamically created widgets using json Data in listvew builder the TextFeild not working in flutter

I have created a list of the widgets using JSON data, I have five types of widgets, and each widget is added to the list depending on the JSON data, but the issue is with the widget includes a text field, All widget displays ok but once I click on the text field the keyboard appears and disappears immediately and gives this error "Exception caught by widgets library" => "Incorrect use of ParentDataWidget."
I try removing everything and adding just this text field widget in the list, but it still not working right. Please guide me on where am doing wrong.
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:my_car/AppUI/CustomWidgets/DateQuestionBox.dart';
import 'package:my_car/AppUI/CustomWidgets/MultiSelectQuestionBox.dart';
import 'package:my_car/AppUI/CustomWidgets/RadioQuestionBox.dart';
import 'package:my_car/AppUI/CustomWidgets/SelectQuestionBox.dart';
import 'package:my_car/AppUI/CustomWidgets/TextQuestionBox.dart';
import 'package:my_car/LocalData/AppColors.dart';
import 'package:flutter/services.dart';
import 'package:my_car/Models/MyClaimQuestionsResponse.dart';
import 'package:my_car/Models/VehiclesResponse.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../Models/VehiclesResponse.dart';
class SubmitClaimQuestionsPage extends StatefulWidget {
String vehicle;
String coverage;
SubmitClaimQuestionsPage(this.vehicle, this.coverage, {Key? key})
: super(key: key);
#override
SubmitClaimQuestionsState createState() =>
SubmitClaimQuestionsState(this.coverage, this.vehicle);
}
class SubmitClaimQuestionsState extends State {
String vehicle;
String coverage;
SubmitClaimQuestionsState(this.coverage, this.vehicle);
Future getMyVehicles() async {
final prefs = await SharedPreferences.getInstance();
final String? action = prefs.getString('vehiclesList');
VehiclesResponse myVehiclesResponse =
VehiclesResponse.fromJson(jsonDecode(action!));
return myVehiclesResponse.vehicles;
}
static List<MyCarClaimType> myCarClaims = <MyCarClaimType>[];
// Fetch content from the json file
Future generateQuestionsFromJson() async {
final String response = await rootBundle
.loadString('lib/Assets/JsonDataFiles/MyCarDataClaimQuestions.json');
MyClaimQuestionsResponse myClaimQuestionsResponse =
MyClaimQuestionsResponse.fromJson(jsonDecode(response));
if (myClaimQuestionsResponse.myCarClaimTypes.isNotEmpty) {
myCarClaims.clear();
myCarClaims = myClaimQuestionsResponse.myCarClaimTypes
.where((element) =>
element.claimType.toLowerCase() == coverage.toLowerCase())
.toList();
}
if (myCarClaims.isNotEmpty) {
setState(() {
for (var i = 0; i < myCarClaims.length; i++) {
if (myCarClaims[i].questionType == "TEXT") {
allQuestions.add(TextQuestionBox(myCarClaims[i]));
} else if (myCarClaims[i].questionType == "SELECT") {
allQuestions.add(SelectQuestionBox(myCarClaims[i]));
} else if (myCarClaims[i].questionType == "RADIO") {
allQuestions.add(RadioQuestionBox(myCarClaims[i]));
} else if (myCarClaims[i].questionType == "MULTI_SELECT") {
allQuestions.add(MultiSelectQuestionBox(myCarClaims[i]));
} else if (myCarClaims[i].questionType == "DATE") {
allQuestions.add(DateQuestionBox(myCarClaims[i]));
}
}
});
}
return allQuestions;
}
#override
void initState() {
super.initState();
generateQuestionsFromJson();
}
bool isVehicleSelected = false;
// ignore: unused_field
List<Widget> allQuestions = <Widget>[];
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
fontFamily: 'Lato',
),
home: Scaffold(
backgroundColor: Color(AppColors.bgColor),
body: SafeArea(
child: SingleChildScrollView(
physics: const ClampingScrollPhysics(),
child: Container(
margin: const EdgeInsets.only(top: 30, bottom: 20),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: const EdgeInsets.only(
bottom: 15,
left: 20,
right: 20,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Align(
alignment: Alignment.centerLeft,
child: SvgPicture.asset(
'lib/Assets/Images/backarrow.svg',
height: 20,
)),
),
Expanded(
child: Column(
children: [
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
vehicle,
textAlign: TextAlign.start,
style: const TextStyle(
fontSize: 18,
letterSpacing: -0.5,
fontWeight: FontWeight.w700,
),
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
coverage,
textAlign: TextAlign.start,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500,
color: Color(AppColors.primaryBlueColor)),
),
),
),
],
)),
],
),
),
Flexible(
fit: FlexFit.loose,
child: ListView.builder(
physics: const ClampingScrollPhysics(),
shrinkWrap: true,
key: UniqueKey(),
itemCount: allQuestions.length,
itemBuilder: (BuildContext context, int index) {
return allQuestions[index];
},
),
),
const SizedBox(
width: 20,
),
],
),
),
Container(
width: double.infinity,
margin: const EdgeInsets.only(
left: 20,
right: 20,
top: 15,
),
child: TextButton(
style: ButtonStyle(
padding: MaterialStateProperty.all(
const EdgeInsets.symmetric(vertical: 16)),
backgroundColor: MaterialStateProperty.all(
Color(AppColors.primaryBlueColor)),
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
)),
),
child: Text(
'Submit Claim',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 15,
color:
Color(AppColors.primaryWhiteButtomTextColor)),
),
onPressed: () {},
),
),
],
),
),
),
),
),
);
}
}
My TextFeild widget is this:
import 'package:flutter/material.dart';
import 'package:my_car/LocalData/AppColors.dart';
import 'package:my_car/Models/MyClaimQuestionsResponse.dart';
class TextQuestionBox extends StatefulWidget {
MyCarClaimType claimObj;
TextQuestionBox(this.claimObj, {Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return TextQuestionBoxState(claimObj);
}
}
class TextQuestionBoxState extends State<TextQuestionBox> {
MyCarClaimType claimObj;
TextQuestionBoxState(this.claimObj);
TextEditingController txtControler = TextEditingController();
Widget get questionTxtBox {
return Container(
//width: double.infinity,
//height: 200,
margin: const EdgeInsets.symmetric(horizontal: 10),
padding: const EdgeInsets.all(10),
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"${claimObj.order + 1}. ",
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
),
),
Expanded(
child: Text.rich(
//softWrap: false,
//overflow: TextOverflow.fade,
TextSpan(
text: claimObj.question,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
),
children: <InlineSpan>[
TextSpan(
text: claimObj.isMandatory == "YES" ? "*" : "",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
color: Color(AppColors.primaryBlueColor)),
),
])),
),
],
),
const SizedBox(
height: 10,
),
Container(
height: 110,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(15))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: TextField(
controller: txtControler,
keyboardType: TextInputType.multiline,
//maxLines: null,
style: const TextStyle(
fontSize: 14, fontWeight: FontWeight.w500),
decoration: const InputDecoration(
border: InputBorder.none,
filled: true,
fillColor: Colors.transparent,
hintText: 'Description',
),
),
),
Container(
margin: const EdgeInsets.only(bottom: 10, right: 15),
child: Text(
'Min. 40 Letters',
style: TextStyle(
fontSize: 10,
fontWeight: FontWeight.w500,
color: Color(AppColors.greyText)),
))
],
)),
],
),
);
}
#override
Widget build(BuildContext context) {
return questionTxtBox;
}
}
TextFields usually try to expand to the available width. This can be problematic in a Column where usually only height is fixed and the Textfield tries to expand into infinity. You should try wrapping the TextField in a Widget that gives it a fixed width like a SizedBox.

How to align button in onboarding screen flutter

I wanted to add a login button below the onboarding screen: how could I do that? Below I have provided the picture of the screen and the code I have implemented so far. Appreciate your help on this.
class OnBoardingScreen extends StatefulWidget {
const OnBoardingScreen({Key? key}) : super(key: key);
#override
_OnBoardingScreenState createState() => _OnBoardingScreenState();
}
class _OnBoardingScreenState extends State<OnBoardingScreen> {
PageController _controller = PageController();
//keep track of if we are on the last page or not
bool onLastPage = false;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
PageView(
onPageChanged: (index) {
setState(() {
onLastPage = (index == 2);
});
},
controller: _controller,
children: [
IntroPageOne(),
IntroPageTwo(),
IntroPageThree(),
],
),
//dot indicators
Container(
alignment: Alignment(0, 0.6),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
//skip
GestureDetector(
onTap: () {
_controller.jumpTo(2);
},
child: Text('Skip', style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
))),
SmoothPageIndicator(
controller: _controller,
count: 3,
effect: WormEffect(
dotColor: Colors.grey,
activeDotColor: MainGreen,
),
),
//next or done
onLastPage
? GestureDetector(
onTap: () {
Get.to(() => const LoginScreen());
},
child: Text('Done',
style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
)),
)
: GestureDetector(
onTap: () {
_controller.nextPage(
duration: Duration(milliseconds: 500),
curve: Curves.easeIn,
);
},
child: Text('Next', style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
)),
)
],
)),
MainButton("Login")
],
));
}
}
Step by step:
Wrap your Stack by Expanded.
Wrap Expanded by Column.
Move MainButton("Login") from Stack children to Column children.
Like this:
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: Stack(...)),
MainButton("Login"),
]
),
);
}
Since your goal is to have 2 widgets displayed in a vertical way, then you should use a Column instead of stacking everything.
Something like this
import 'package:flutter/material.dart';
class OnBoardingScreen extends StatefulWidget {
const OnBoardingScreen({Key? key}) : super(key: key);
#override
_OnBoardingScreenState createState() => _OnBoardingScreenState();
}
class _OnBoardingScreenState extends State<OnBoardingScreen> {
PageController _controller = PageController();
//keep track of if we are on the last page or not
bool onLastPage = false;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(
children: [
PageView(
onPageChanged: (index) {
setState(() {
onLastPage = (index == 2);
});
},
controller: _controller,
children: [
IntroPageOne(),
IntroPageTwo(),
IntroPageThree(),
],
),
//dot indicators
Container(
alignment: Alignment(0, 0.6),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
//skip
GestureDetector(
onTap: () {
_controller.jumpTo(2);
},
child: Text(
'Skip',
style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
),
),
),
SmoothPageIndicator(
controller: _controller,
count: 3,
effect: WormEffect(
dotColor: Colors.grey,
activeDotColor: MainGreen,
),
),
//next or done
onLastPage
? GestureDetector(
onTap: () {
Get.to(() => const LoginScreen());
},
child: Text('Done',
style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
)),
)
: GestureDetector(
onTap: () {
_controller.nextPage(
duration: Duration(milliseconds: 500),
curve: Curves.easeIn,
);
},
child: Text(
'Next',
style: TextStyle(
height: 1.2,
fontFamily: "Roboto",
fontSize: 15,
color: MainGreen,
fontWeight: FontWeight.w500,
// fontWeight: FontWeight.w100,
),
),
)
],
),
),
],
),
MainButton("Login"),
],
),
),
);
}
}
Try below code hope its helpful to you. I have try without Stack widget Wrap your Button inside Expanded and give its to alignment
Column(
children: <Widget>[
Text('All Leads'),
Text('Widget 2'),
Text('Widget 3'),
Text('Widget 4'),
Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: ElevatedButton(
onPressed: () {},
child: Text('Login'),
),
),
),
],
),
Result Screen->

Flutter TextOverflow not working, and causing RenderOverflow

I'm trying to make a chat app UI, and thought of using TextOverflow to fix the problem where the last sent message exceedes the size of the screen and causes Render Overflow. So I simply modified my Text widget, adding these 3 lines-
Text(
widget.lastMessage,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: true,
style: const TextStyle(
fontWeight: FontWeight.w200,
),
)
However, this made no difference-
Here's the code-
Main ListView-
import 'package:project_name/widgets/chat_preview_tile_widget.dart';
import 'package:flutter/material.dart';
class ChatPreviewTileCard extends StatefulWidget {
const ChatPreviewTileCard({Key? key}) : super(key: key);
#override
_ChatPreviewTileCardState createState() => _ChatPreviewTileCardState();
}
class _ChatPreviewTileCardState extends State<ChatPreviewTileCard> {
#override
Widget build(BuildContext context) {
return Expanded(
child: ListView.builder(
itemCount: 10,
itemBuilder: (context, n) {
//Individual List Tile
return const ChatPreviewTileWidget(
username: "Person",
imageUrl:
"https://www.nme.com/wp-content/uploads/2021/07/RickAstley2021.jpg",
time: "9:30PM",
hasUnreadMessage: true,
lastMessage:
"i am susssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssSSSSSSSSSSs",
);
},
),
);
}
}
Individual Tile Widget-
import 'package:airing/screens/chat_screen/user_chat/user_chat_screen.dart';
import 'package:airing/utils/color_themes.dart';
import 'package:flutter/material.dart';
class ChatPreviewTileWidget extends StatefulWidget {
final String username;
final String imageUrl;
final String time;
final bool hasUnreadMessage;
final String lastMessage;
const ChatPreviewTileWidget({
Key? key,
required this.username,
required this.imageUrl,
required this.time,
required this.hasUnreadMessage,
required this.lastMessage,
}) : super(key: key);
#override
_ChatPreviewTileWidgetState createState() => _ChatPreviewTileWidgetState();
}
class _ChatPreviewTileWidgetState extends State<ChatPreviewTileWidget> {
static int widgetsInScreen = 12;
static int pfpPadding = 10;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return UserChatScreen(
imageUrl: widget.imageUrl, username: widget.username);
},
),
);
},
child: Column(
children: [
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height / widgetsInScreen,
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2.5),
//main row
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
//Pfp, username and last message row
Row(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(
backgroundColor: secondaryColor,
backgroundImage: NetworkImage(
widget.imageUrl,
),
radius: ((MediaQuery.of(context).size.height /
widgetsInScreen) -
(2 * pfpPadding)) /
2,
),
Padding(
padding: const EdgeInsets.only(
left: 10,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.username,
style: TextStyle(
fontWeight: FontWeight.bold,
color: secondaryMainThemeColor),
),
Text( //Here is the text message widget.
widget.lastMessage,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: true,
style: const TextStyle(
fontWeight: FontWeight.w200,
),
)
],
),
)
],
),
//sent and notification Column
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
widget.time,
style: TextStyle(
color: secondaryMainThemeColor,
fontWeight: FontWeight.w200),
),
Icon(
Icons.circle_rounded,
color: mainThemeColor,
size: 15,
)
],
)
],
),
),
const Divider()
],
));
}
}
How do I fix this? Thanks in advance!
Wrap your Text Column with Expanded widget like this:
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.username,
style: TextStyle(
fontWeight: FontWeight.bold, color: secondaryMainThemeColor),
),
Text(
//Here is the text message widget.
widget.lastMessage,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: true,
style: const TextStyle(
fontWeight: FontWeight.w200,
),
)
],
),
),
Hope It Works!
Change your code to this:
import 'package:airing/screens/chat_screen/user_chat/user_chat_screen.dart';
import 'package:airing/utils/color_themes.dart';
import 'package:flutter/material.dart';
class ChatPreviewTileWidget extends StatefulWidget {
final String username;
final String imageUrl;
final String time;
final bool hasUnreadMessage;
final String lastMessage;
const ChatPreviewTileWidget({
Key? key,
required this.username,
required this.imageUrl,
required this.time,
required this.hasUnreadMessage,
required this.lastMessage,
}) : super(key: key);
#override
_ChatPreviewTileWidgetState createState() => _ChatPreviewTileWidgetState();
}
class _ChatPreviewTileWidgetState extends State<ChatPreviewTileWidget> {
static int widgetsInScreen = 12;
static int pfpPadding = 10;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return UserChatScreen(
imageUrl: widget.imageUrl, username: widget.username);
},
),
);
},
child: Column(
children: [
Container(
width: double.infinity,
height: MediaQuery.of(context).size.height / widgetsInScreen,
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2.5),
//main row
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
//Pfp, username and last message row
Row(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(
backgroundColor: secondaryColor,
backgroundImage: NetworkImage(
widget.imageUrl,
),
radius: ((MediaQuery.of(context).size.height /
widgetsInScreen) -
(2 * pfpPadding)) /
2,
),
**//here is the changes**
Expanded(child: Padding(
padding: const EdgeInsets.only(
left: 10,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.username,
style: TextStyle(
fontWeight: FontWeight.bold,
color: secondaryMainThemeColor),
),
Text( //Here is the text message widget.
widget.lastMessage,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: true,
style: const TextStyle(
fontWeight: FontWeight.w200,
),
)
],
),),
)
],
),
//sent and notification Column
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
widget.time,
style: TextStyle(
color: secondaryMainThemeColor,
fontWeight: FontWeight.w200),
),
Icon(
Icons.circle_rounded,
color: mainThemeColor,
size: 15,
)
],
)
],
),
),
const Divider()
],
));
}
}
Nevermind! Wrapping my text widget with a SizedBox with a defined width fixed it-
SizedBox(
width: MediaQuery.of(context).size.width / 2,
child: Text(
widget.lastMessage,
overflow: TextOverflow.ellipsis,
maxLines: 1,
softWrap: true,
style: const TextStyle(
fontWeight: FontWeight.w200,
),
),
),

Tab barView doesn't automatically get updated by setstate until the tab bar is clicked on

I am using a Tabbar widget which controls a TabbarView. When I call on setstate after changing the value of a variable named "searchTerm", the anticipated change isn't updated in the tabbarView widgets until I click on any of the tabs.I need the tabviewView to update immediately i call on setstate.Please i need a response as soon as possible See screenshot here
Below is the code
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin {
String searchTerm;
List <Widget> kTabPages;
TabController tabController;
final ktabs = <Tab>[Tab(child: Text("All"),),Tab(child: Text("Popular"),),Tab(child: Text("Top"),),];
#override
void dispose() {
tabController.dispose();
super.dispose();
}
#override
void initState() {
tabController = TabController(length: ktabs.length, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
kTabPages = <Widget>[Center(child: GridWidget(category: "all",searchTerm: searchTerm,),),
Center(child: GridWidget(category: "popular",searchTerm: searchTerm,),),
Center(child: GridWidget(category: "top",searchTerm: searchTerm,),)];
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Color.fromARGB(255, 69, 22, 99),
body: SafeArea(
child: DefaultTabController(
length: ktabs.length,
child: Column(
children: [
Expanded(child: Container(
child: Padding(
padding: const EdgeInsets.only(left: 25,right: 16,top: 25,bottom: 10),
child: Column(
children: [
Expanded(
flex: 5,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
child: Icon(
Icons.apps_outlined,
color: Colors.white,
),
),
GestureDetector(
onTap: ()async{
//assigns a new value to variable searchName after returning from SearchScreen
var searchName = await Navigator.push(context,
MaterialPageRoute(builder: (context){
return SearchScreen();
}));
if(searchName != null){
setState(() {
//Assigns SearchName value to variable searchTerm
searchTerm = searchName;
});
}
},
child: Icon(
Icons.search,
color: Colors.white,
),
),
],
),
SizedBox(
height: 15,
),
Text("Hi Xeroes🖐️",style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
fontSize: 25.0
),),
SizedBox(
height: 10,
),
Text("Today is a good day",style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 13.0
),),
SizedBox(
height: 2,
),
Text("To learn something new!",style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 13.0
),),
],
),
),
Expanded(
flex: 1,
child: TabBar(
controller: tabController,
tabs:ktabs ,
labelColor: Colors.white,
indicatorColor: Colors.amberAccent,
indicatorSize: TabBarIndicatorSize.label,
labelStyle: TextStyle(
// fontSize: 16
),),
)
],
),
),
color: Color.fromARGB(255, 69, 22, 99),
),
flex: 4,),
Expanded(
child: Container(
padding: EdgeInsets.only(top: 20),
child: TabBarView(
controller: tabController,
children: kTabPages,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(topLeft: Radius.circular(30),topRight:Radius.circular(30))
),
),
flex: 8,)
],
),
),
),
);
}
}

InteractiveViewer does not hide other Widgets when panned

I used InteractiveViewer here on my ProductDetail Page.
But when I pan and Zoom the image, the other widgets such as buttons and text are coming over it.
Here is my code:
class ProductDetail extends StatelessWidget {
final Product product;
ProductDetail({this.product});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Column(
children: [
SizedBox(
height: 20
),
SizedBox(
height: 300,
width: MediaQuery.of(context).size.width -20,
child: InteractiveViewer(
child: Hero(
tag: product.id,
child: Carousel(
autoplay: false,
boxFit: BoxFit.cover,
dotBgColor: Colors.transparent,
dotColor: Colors.black.withOpacity(0.5),
images: [
AssetImage(product.imageUrl),
AssetImage(product.imageUrl),
AssetImage(product.imageUrl),
],
),
),
),
),
SizedBox(
width: MediaQuery.of(context).size.width -20,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text(
'₹ ${product.price}',
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
Text(
'₹ ${product.price}',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
decoration: TextDecoration.lineThrough,
color: Colors.grey
),
),
],
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(
child: ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(getColorFromHex('#d1d1d1')),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.add_shopping_cart),
Text("Add to Cart", style: TextStyle(fontSize: 18)),
],
),
)
),
SizedBox(
child: ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(getColorFromHex('#56a8e2')),
),
child: Row(
children: [
Icon(Icons.shopping_bag),
Text("Buy Now", style: TextStyle(fontSize: 18)),
],
),
)
),
],
),
),
],
),
),
);
}
}
How do I hide the other widgets like Text and buttons when I pan or zoom the image with the InteractiveViewer?
InteractiveViewer has two properties onInteractionStart and onInteractionEnd.
onInteractionStart → void Function(ScaleStartDetails details) : Called
when the user begins a pan or scale gesture on the widget.
onInteractionEnd → void Function(ScaleEndDetails details) : Called
when the user ends a pan or scale gesture on the widget.
You need to wrap the widgets that you wish to hide with the Visibility widget.
You can set - reset the visibility of the widgets wrapped under Visibility widget when user starts (onInteractionStart) and stops to pan or scale (onInteractionEnd).
Please see the code below, I had to make some changes to make it work :
import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ProductDetail(),
),
);
}
}
class ProductDetail extends StatefulWidget {
ProductDetail({products});
#override
_ProductDetailState createState() => _ProductDetailState();
}
class _ProductDetailState extends State<ProductDetail> {
bool _visible = true;
TransformationController controller = TransformationController();
final Products products = Products(
id: 10,
imageUrl:
"https://cdn.pixabay.com/photo/2020/10/01/17/11/temple-5619197_960_720.jpg",
price: 20.00);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: Column(
children: [
SizedBox(height: 20),
SizedBox(
height: 300,
width: MediaQuery.of(context).size.width - 20,
child: InteractiveViewer(
transformationController: controller,
onInteractionStart: (scale) {
setState(() => _visible = false);
},
onInteractionEnd: (scale) {
setState(() {
controller.value = Matrix4.identity();
_visible = true;
});
},
child: Hero(
tag: products.id,
child: Carousel(
autoplay: false,
boxFit: BoxFit.cover,
dotBgColor: Colors.transparent,
dotColor: Colors.black.withOpacity(0.5),
images: [
NetworkImage(products.imageUrl),
NetworkImage(products.imageUrl),
NetworkImage(products.imageUrl),
],
),
),
),
),
Visibility(
visible: _visible,
child: SizedBox(
width: MediaQuery.of(context).size.width - 20,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Text(
'₹ ${products.price.toString()}',
style: TextStyle(
fontSize: 25, fontWeight: FontWeight.bold),
),
Text(
'₹ ${products.price.toString()}',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
decoration: TextDecoration.lineThrough,
color: Colors.grey),
),
],
),
),
),
),
Visibility(
visible: _visible,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
SizedBox(
child: ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.cyanAccent),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.add_shopping_cart),
Text("Add to Cart", style: TextStyle(fontSize: 18)),
],
),
)),
SizedBox(
child: ElevatedButton(
onPressed: () {},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.limeAccent),
),
child: Row(
children: [
Icon(Icons.shopping_bag),
Text("Buy Now", style: TextStyle(fontSize: 18)),
],
),
)),
],
),
),
),
],
),
),
);
}
}
class Products {
final int id;
final String imageUrl;
final double price;
Products({
this.id,
this.imageUrl,
this.price,
});
Products copyWith({
int id,
String imageUrl,
double price,
}) {
return Products(
id: id ?? this.id,
imageUrl: imageUrl ?? this.imageUrl,
price: price ?? this.price,
);
}
}