ScrollBar and SingleChildScrollView not working - flutter

I am trying to display list of titles and subtitles in scrollview but the scrollview is not working. I am getting this error:
RenderBox was not laid out: RenderRepaintBoundary#d93c1 relayoutBoundary=up4 NEEDS-PAINT
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1940 pos 12: 'hasSize'
Full code of my dart file:
//import 'package:flappy_search_bar/flappy_search_bar.dart';
import 'package:flappy_search_bar/flappy_search_bar.dart';
import 'package:flappy_search_bar/search_bar_style.dart';
import 'package:flutter/material.dart';
import 'body.dart';
class DefinationBody extends StatelessWidget {
const DefinationBody({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
centerTitle: true,
leading: Padding(
padding: EdgeInsets.only(left: 12),
child: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => BusinessBody()),
),
),
),
title: Text(
"Definations",
style: TextStyle(
color: Colors.orange[900],
),
),
),
//backgroundColor: Colors.yellow,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/main_bottom.png"),
alignment: Alignment.bottomCenter,
fit: BoxFit.cover,
),
),
child: MyCardWidget(),
),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyCardWidget extends StatelessWidget {
MyCardWidget({Key key}) : super(key: key);
//final ScrollController _scrollController = ScrollController();
#override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Container(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.only(top: 10, left: 50.0),
child: Column(
children: [
SizedBox(
child: Row(
children: [
Text(
'Definations',
style: TextStyle(
fontFamily: 'Arial',
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black,
height: 2,
),
textAlign: TextAlign.center,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Container(
margin: EdgeInsets.only(top: 10),
height: 3.0,
width: 200.0,
color: Colors.orange[900],
),
),
],
),
),
],
),
),
),
Padding(
padding: const EdgeInsets.only(top: 20.0, left: 20, right: 20),
child: Container(
height: 82,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange[900]),
borderRadius: BorderRadius.all(Radius.circular(40))),
// ignore: missing_required_param
child: SearchBar(
searchBarStyle: SearchBarStyle(
backgroundColor: Colors.transparent,
padding: EdgeInsets.all(20),
borderRadius: BorderRadius.circular(40),
),
hintText: "Search",
hintStyle: TextStyle(
color: Colors.grey[100],
),
textStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
mainAxisSpacing: 10,
crossAxisSpacing: 10,
)),
),
Container(
child: new Expanded(
flex: 1,
child: Scrollbar(
isAlwaysShown: true,
child: SingleChildScrollView(
child: ListView(
children: [
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
]
)
)
)
),
),
],
),
);
}
}

Make your listview shrinkwrap: true and ensure scroll physics
shrinkWrap: true,
physics: ScrollPhysics()

I think your ListView and SingleChildScrollView are conflict.
SingleChildScrollView(
child: Column(
children: [
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
],
),
),
or you can setting your height
SingleChildScrollView(
child: SizedBox(
height: 200,
child: ListView(
children: [
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
),
],
),
),
),

you cant do Expanded+SingleChildScroll view, SingleChildScrollView has infinite height, so it wont work, you need constrained/fixed height for SingleChildScrollView,
#EDIT
Scrollbar(
isAlwaysShown: true,
child: ListView(
children: List.generate(100, (index) => ListTile(
title: Text('this is title'),
subtitle: Text('this is sub title'),
))
)
)

Related

How I put Blur image in the scaffold background in flutter

I want to put the transparent image on the background with text on it. I tried with container in return but it's not working. Also I tried container in the body, but unfortunately that is also not working for me. It's a single screen app. Below is my code. Your answer will be very helpful for me.
Thanks in advance.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
"assets/tree.png",
),
),
centerTitle: true,
title: Text(
"Welcome To Plants",
style: TextStyle(
color: Colors.green,
fontWeight: FontWeight.w800,
),
)),
body: Column(
children: [
Flexible(
child: ListView.builder(
itemCount: _messages.length,
reverse: true,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(8.0), child: _messages[index]),
),
),
const Divider(
color: Color.fromARGB(255, 51, 223, 56),
thickness: 1,
),
_istyping ? LinearProgressIndicator() : Container(),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _controller,
onSubmitted: (value) => _sendMessage(),
decoration: const InputDecoration.collapsed(
hintText: "Type Here",
hintStyle: TextStyle(fontSize: 20)),
),
)),
ButtonBar(children: [
IconButton(
onPressed: () {
_isImageSearch = false;
_sendMessage();
},
icon: Icon(
Icons.send,
color: Theme.of(context).primaryColor,
)),
TextButton(
onPressed: () {
_isImageSearch = true;
_sendMessage();
},
child: Text("Show Image"))
]),
],
),
)
],
)
Import the following module
import 'dart:ui';
make sure you included the asset in your yaml file
eg: used lib/utils/images/test.jpg
Example Scaffold
Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
Image.asset('lib/utils/images/test.jpg', fit: BoxFit.fill),
BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
child: Container(
color: Colors.black.withOpacity(0.5),
),
),
// Add your UI component here
],
),
);
Example screenshot

How use flexible widget to divide my screen into 2

I would like to divide my screen into 2 using the Flexible Widget. The first flexible widget I would like to set is 80% as yellow and blue for the remaining 20%. Is there any way of dividing it using the flexible widget as I would like to have the remaining 20% of empty space in blue colour.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("BitCoin cryptocurrency ")),
body: SingleChildScrollView(
child: Column(
children: [
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
"BitCoin cryptocurrency",
style:
TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: valueEditingController,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: 'Bitcoin Value',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0)))),
),
DropdownButton(
itemHeight: 60,
value: selectLoc,
onChanged: (newValue) {
setState(() {
selectLoc = newValue.toString();
});
},
items: locList.map((selectLoc) {
return DropdownMenuItem(
child: Text(
selectLoc,
),
value: selectLoc,
);
}).toList(),
),
ElevatedButton(
onPressed: _loadWeather, child: const Text("Load Value")),
const SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
FittedBox(
child: Text(
// description,
nu,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 25),
),
),
FittedBox(
child: Text(
nu2,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 25),
),
),
FittedBox(
child: Text(
nu3,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 25),
),
),//FittedBox
],
),//Column
),//Padding
],
)),//Cloumn //Centre
],
),//Column
)// SingleChildScrollView
);//Scaffold
}
Sorry for any inconvenience.
You can do it by using the Flexible widget with the flex attribute at flex 8 and flex 2
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Fexible widget'),
), //AppBar
body: CustomScrollView(
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: <Widget>[
Flexible(
flex: 8,
fit: FlexFit.loose,
child: Container(
color: Colors.yellow,
), //Container
), //Flexible
Flexible(
flex: 2,
fit: FlexFit.loose,
child: Container(
color: Colors.blue,
), //Container
) //Flexible
//Row
//Row
], //<Widget>[]
),
),
],
),
), //Scaffold
debugShowCheckedModeBanner: false,
),
); //MaterialApp
}

How to change the whole background color of Drawer in Flutter

first of all i'm new to flutter and so i am quite confused how to change the "whole" background color in drawer flutter.
i managed to change my ListTile and my so called DrawerHeader to the color i want but the rest (below the ListTile is all white). And there is a line below the DrawerHeader that i don't really want but i can't get rid of it.
here is the code
import 'package:flutter/material.dart';
import '../style/theme.dart' as style;
class MyDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
// width: MediaQuery.of(context).,
child: Drawer(
elevation: 0,
child: ListView(
children: <Widget>[
Container(
height: 170,
width: 170,
padding: EdgeInsets.only(top:30),
color: style.Colors.secondColor,
child: Column(children: <Widget>[
Material(
borderRadius: BorderRadius.all(Radius.circular(100)),
child: Padding(padding: EdgeInsets.all(20.0),
child: Image.asset('images/circle.png',width: 80, height: 80,),),
//TODO ganti logo
),
],),
),
Container(
color: style.Colors.secondColor,
child: Column(
children: <Widget>[
ListTile(
leading: Icon(Icons.help_outline_sharp),
title: Text('Help', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
ListTile(
leading: Icon(Icons.person),
title: Text('About us', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
],
),
),
],
),
),
);
}
}
here is the photo of the current situation
Here is the photo
Another Question
what if i want to put a single ListTile on the bottom? (like log out)
For changing the background color of drawer you can just swap the Drawer Widget with Container and give color to container
class MyDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
elevation: 0,
child: Container(
color: Colors.blue,
child: ListView(
children: <Widget>[
Container(
height: 170,
width: 170,
padding: EdgeInsets.only(top:30),
color: Colors.red,
child: Column(children: <Widget>[
Material(
borderRadius: BorderRadius.all(Radius.circular(100)),
child: Padding(padding: EdgeInsets.all(20.0),
child: Image.asset('images/circle.png',width: 80, height: 80,),),
),
],),
),
Container(
color: Colors.red,
child: Column(
children: <Widget>[
ListTile(
leading: Icon(Icons.help_outline_sharp),
title: Text('Help', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
ListTile(
leading: Icon(Icons.person),
title: Text('About us', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
],
),
),
],
),
),
);
}
}
This can be the final code you can use, it has answer to all your three questions
class MyDrawer extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
Expanded(
child: ListView(
children: <Widget>[
Container(
height: 170,
width: 170,
padding: EdgeInsets.only(top:30),
color: Colors.blue,
child: Column(children: <Widget>[
Material(
borderRadius: BorderRadius.all(Radius.circular(100)),
child: Padding(padding: EdgeInsets.all(20.0),
child: Image.asset('images/circle.png',width: 80, height: 80,),),
),
],
),
),
ListTile(
leading: Icon(Icons.help_outline_sharp),
title: Text('Help', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
ListTile(
leading: Icon(Icons.person),
title: Text('About us', style: TextStyle(fontSize: 18,),),
onTap: () {},
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: ListTile(
leading: Icon(Icons.logout),
title: Text('Logout')
),
),
],
),
);
}
}
To change the color, wrap the Drawer widget in a Theme widget as follows:
Scaffold(
drawer:Theme(
data:Theme.of(context).copyWith(
canvasColor://the desired color will go here
),
child:Drawer(/*drawer content*/)
)
)
Hope this is useful for someone

How to make image centerInParent in flutter

Facing some difficulties in flutter as I am new to it, how to centerInParent for imageView for flutter? The image is not center, I had use Align but still not working. And how to construct the image and text like this photo ?
appBar: PreferredSize(
preferredSize: Size.fromHeight(45.0),
child: AppBar(
leading: Row(
children: [
Container(
child: GestureDetector(
onTap: () {/* open left menu */},
child: Image.asset("assets/images/ic_title_menu.png", width: 18.0, height: 18.0,)),
padding: EdgeInsets.fromLTRB(14.0, 14.0, 14.0, 14.0),
),
],
),
title: Container(
child: GestureDetector(
onTap: () {/* open search */},
child: Image.asset("assets/images/ic_title_search.png", width: 18.0, height: 18.0,)),
),
actions: <Widget>[
Center(
child: Container(
child: Stack(children: <Widget>[
Align(
alignment: Alignment.center,
child: Image.network(""),)
],),
),
)
],
titleSpacing: 0.0,
automaticallyImplyLeading: false,
),
),
I hope this is something you wanted!
import 'package:flutter/material.dart';
const Color kRed = Colors.red;
class Test extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(
Icons.menu,
color: kRed,
size: 30,
),
SizedBox(width: 10),
Icon(
Icons.search,
color: kRed,
size: 30,
)
],
),
Text(
'Test Text',
style: TextStyle(color: kRed, fontSize: 30),
),
Row(
children: <Widget>[
Column(
children: <Widget>[
Text(
'Test Text',
style: TextStyle(color: kRed, fontSize: 15),
),
Text(
'Test Text',
style: TextStyle(color: kRed, fontSize: 15),
),
],
),
SizedBox(width: 10),
Icon(
Icons.person,
color: kRed,
size: 30,
)
],
)
],
),
),
);
}
}
Just replace the Text Widget in the middle with Image Widget. And tweak with your colors.
App Bar has a property called centerTitle, it will add title on the Appbar center.
centerTitle: true,
Try This
flexibleSpace: Container(
margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Center(
child: Image(
image: AssetImage('assets/img/icon_1.png'),
))),

Flutter : Image Lost in SliverAppBar

I'm Working with SliverAppBar.
Previous I have 2 Page Let's Say (Search,and Detail) ,
In Detail Page,I have problem When i Scroll Down and make Image not visible after that I'm press button Back , My Image lost in Search Page. But if i Scroll down and then little Scroll Up to make Image visible after that i press Back button , My image not lost and still stay there.
It's may be confusing, but you can see the GIF for more detail.
Can you help Me with this ?
Thank's
It's Detail.dart :
import 'package:flutter/material.dart';
import 'package:flutter_news/model/Berita_model.dart';
import 'package:flutter_news/widget/Widget_builder.dart';
class WartawanDetail extends StatefulWidget {
final int idWartawan;
final String gambarWartawan;
final String namaWartawan;
WartawanDetail({this.idWartawan, this.gambarWartawan, this.namaWartawan});
#override
_WartawanDetailState createState() => _WartawanDetailState();
}
class _WartawanDetailState extends State<WartawanDetail> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
floating: true,
pinned: false,
leading: IconButton(
icon: Icon(Icons.arrow_left),
onPressed: () => Navigator.pop(context),
),
expandedHeight: 300.0,
centerTitle: true,
flexibleSpace: FlexibleSpaceBar(
background: Hero(
tag: widget.idWartawan,
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(widget.gambarWartawan),
fit: BoxFit.cover),
),
child: Stack(
children: <Widget>[
Positioned(
bottom: 10,
left: 10,
child: Material(
color: Colors.transparent,
child: Chip(
backgroundColor: Colors.blue,
labelStyle: TextStyle(color: Colors.white),
label: Text(widget.namaWartawan),
),
),
),
Positioned(
bottom: 10,
right: 10,
child: Material(
color: Colors.transparent,
child: Chip(
backgroundColor: Colors.blue,
labelStyle: TextStyle(color: Colors.white),
label: Text("2 Berita"),
),
),
),
],
),
),
),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.more_horiz),
onPressed: () => "",
),
],
),
SliverFixedExtentList(
itemExtent: MediaQuery.of(context).size.height,
delegate: SliverChildListDelegate(
[
Container(
padding: EdgeInsets.all(8),
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: beritas.length,
itemBuilder: newsBuilderHome),
),
],
),
)
],
),
);
}
}
It's My Search.dart
Widget wartawanBuilderSearch(BuildContext context, int index) {
ThemeData localTheme = Theme.of(context);
return Container(
margin: EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: Container(
child: Hero(
tag: wartawans[index].idWartawan,
child: Material(
color: Colors.transparent,
child: InkWell(
onTap: () => Navigator.push(
context,
PageTransition(
type: PageTransitionType.downToUp,
child: WartawanDetail(
idWartawan:wartawans[index].idWartawan,
gambarWartawan: wartawans[index].gambarWartawan,
namaWartawan: wartawans[index].namaWartawan,
),
),
),
child: CircleAvatar(
backgroundColor: Colors.red,
radius: 50,
backgroundImage:
AssetImage(wartawans[index].gambarWartawan),
),
),
),
),
),
),
Container(
child: Align(
alignment: Alignment.bottomLeft,
child: Text(
wartawans[index].namaWartawan,
style: localTheme.textTheme.caption
.copyWith(fontSize: 14, fontWeight: FontWeight.bold),
overflow: TextOverflow.ellipsis,
),
),
)
],
),
);
}