I want to my text to elippsis if it’s size is greater than the width of the container but actually it’s taking the space and giving the error of render flex.
And can someone give me a suggestion how can I improve my code I want a list view where a list is slidable .
And also how to dynamically I can set width because i needed a fixed header so that it does not move when I scroll the it should remains.
This is my code
// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors, unused_field, unused_element, unused_import
import 'dart:math';
import 'package:cryptocornars/common/widget/common_widget.dart';
import 'package:cryptocornars/constants/constants.dart';
import 'package:cryptocornars/features/market/screen/view_model/coins_view_model.dart';
import 'package:cryptocornars/model/market/coins_model.dart';
import 'package:cryptocornars/provider/filter_provider.dart';
import 'package:cryptocornars/provider/token_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:provider/provider.dart';
class CoinsPage extends StatefulWidget {
const CoinsPage({super.key});
@override
State<CoinsPage> createState() => _CoinsPageState();
}
class _CoinsPageState extends State<CoinsPage> {
final List _peopleData = List.generate(100, (index) {
return {"name": "Person #$index", "age": Random().nextInt(90) + 10};
});
late CoinsViewModel viewModel = CoinsViewModel();
List<Coins>? item;
Widget _itemsBuild(index, width, margin) {
final items = item![index];
final data = items.data![index];
return Container(
margin: EdgeInsets.all(10),
child: Row(
children: [
Container(
width: (width / 4) + 15,
margin: EdgeInsets.only(left: 2, right: 0, top: 5, bottom: 0),
child: Text(
"#$index",
style: TextStyle(color: Colors.black, fontSize: 15),
),
),
Expanded(
child: Container(
width: width + 20,
margin: EdgeInsets.only(left: 1, top: 5, bottom: 0),
child: Row(
children: [
Image.network(
data.coinLogo!,
width: 25,
height: 40,
),
Container(
margin: const EdgeInsets.only(left: 5),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
data.coinName!,
style: TextStyle(color: Colors.black, fontSize: 15),
overflow: TextOverflow.ellipsis,
),
Text(
"324.44Bn",
style: TextStyle(color: Colors.black45),
overflow: TextOverflow.ellipsis,
),
],
),
),
],
),
),
),
Expanded(
child: Container(
width: width,
margin: EdgeInsets.only(left: 20, top: 5, bottom: 0),
child: Center(
child: Text(
data.currentPrice.toString(),
style: TextStyle(color: Colors.black, fontSize: 15),
overflow: TextOverflow.ellipsis,
),
),
),
),
Expanded(
child: Container(
width: width - 15,
margin: EdgeInsets.only(left: 40, top: 5, bottom: 0),
child: Text(
data.priceChange24hPercentage!.toString(),
style: TextStyle(color: Colors.black, fontSize: 15),
overflow: TextOverflow.ellipsis,
),
),
),
],
),
);
}
@override
void initState() {
super.initState();
viewModel.loadCoinData("marketCap", "desc", "100", "1");
}
@override
Widget build(BuildContext context) {
var token = Provider.of<AccessToken>(context, listen: false).token;
double screenWidth = MediaQuery.of(context).size.width;
double columnWidth = screenWidth / 5;
double mar = (columnWidth / 5) + 1;
var margin = EdgeInsets.all(mar);
return ChangeNotifierProvider<CoinsViewModel>(
create: (BuildContext context) => viewModel,
child: Consumer<CoinsViewModel>(
builder: (context, viewModel, _) {
debugPrint("length ${viewModel.coinsModel.length}");
item = viewModel.coinsModel;
return Scaffold(
body: Column(children: [
Container(
padding: const EdgeInsets.only(
top: 10, bottom: 0, left: 10, right: 10),
color: Colors.white,
child: Row(
children: [
Container(
width: columnWidth / 4,
margin: EdgeInsets.only(
left: 6, top: 0, right: 0, bottom: 0),
child: Text(
'#',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto-Bold',
fontWeight: FontWeight.bold,
),
),
),
Container(
width: columnWidth + 10,
margin: EdgeInsets.only(
left: mar + 10, top: 0, right: mar, bottom: 0),
child: Text(
'Market Cap',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto-Bold',
fontWeight: FontWeight.bold),
),
),
Container(
width: columnWidth + 10,
margin: EdgeInsets.only(
left: mar + 2, top: 0, right: mar, bottom: 0),
child: Text(
'Price(USD)',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto-Bold',
fontWeight: FontWeight.bold,
),
),
),
Container(
// width: columnWidth,
margin: EdgeInsets.only(
left: mar + 10, top: 0, right: mar, bottom: 0),
child: Text(
'24h%',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto-Bold',
fontWeight: FontWeight.bold,
),
),
),
],
),
),
Expanded(
child: viewModel.getCoinDataResponse.isLoading &&
viewModel.coinsModel.isEmpty
? const Center(
child: CircularProgressIndicator(),
)
: viewModel.getCoinDataResponse.isError
? Center(
child: emptyWidget(
viewModel.getCoinDataResponse.message ??
someThingWentWrong,
'',
'assets/images/blank_report.png'),
)
: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: viewModel.coinsModel.length,
itemBuilder: (ctx, index) {
return Slidable(
endActionPane: ActionPane(
motion: const ScrollMotion(),
children: [
SlidableAction(
onPressed: (ctx) {
doNothing(ctx, "Alerts");
},
backgroundColor: Color(0xff1C2B5E),
foregroundColor: Colors.white,
icon: Icons.notifications,
label: 'Alerts',
padding: EdgeInsets.all(0),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8),
bottomLeft: Radius.circular(8)),
),
SlidableAction(
onPressed: (ctx) {
doNothing(ctx, "Watchlist");
},
backgroundColor:
const Color(0xFF0096FF),
foregroundColor: Colors.white,
icon: Icons.star,
label: 'Watchlist',
borderRadius: BorderRadius.only(
topRight: Radius.circular(8),
bottomRight: Radius.circular(8)),
padding: EdgeInsets.all(0),
spacing: 5,
),
],
),
child: _itemsBuild(index, columnWidth, mar),
);
},
),
)
]),
);
},
));
}
void doNothing(BuildContext context, String text) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: const Text("Alert Dialog Box"),
content: Text("You have raised a Alert Dialog Box, $text "),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Container(
color: Colors.black45,
padding: const EdgeInsets.all(14),
child: const Text(
"okay",
style: TextStyle(color: Colors.white),
),
),
),
],
),
);
}
}
2
Answers
You can use
Expanded and flex
Try this!
on your Widget build(BuildContext context) and before retrurn :
on list view builder :