Mehdi Haghgoo
09/25/2020, 7:41 AMclass HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = LinearLayout(context).apply{
addView(ComposeView(context).apply {
setContent {
BodyContent(this.findFragment())
}
})
}
}
@Composable
fun BodyContent(fragment: Fragment) {
val data = listOf("One", "Two", "Three", "Four", "Five")
Scaffold(
modifier = Modifier.background(Color.Black).fillMaxSize()
.background(Color.Blue),
topBar = {
MyTopAppBar()
},
bottomBar = {
MyBottomBar()
},
floatingActionButton = { MyFab(fragment) },
floatingActionButtonPosition = FabPosition.End,
snackbarHost = { },
drawerContent = { MyDrawer() }
){
LazyColumnFor(items = data, modifier = Modifier
.background(Color.Green),
itemContent = {item->
Text(item, modifier = Modifier.padding(8.dp))
}
)
}
}
@Composable
fun MyDrawer() {
val items = arrayOf("My Account", "Monthly Cost", "Savings Report")
Box(paddingStart = 32.dp) {
Column(modifier = Modifier.align(Alignment.CenterVertically)) {
for (item in items) {
Text(item, modifier = Modifier.padding(8.dp))
}
}
}
}
@Composable
fun MyTopAppBar() {
TopAppBar(modifier = Modifier.padding(8.dp, 16.dp, 8.dp, 8.dp)) {
if(gCost != null){
Text("Cost is $gCost", modifier = Modifier.border(1.dp, Color.Red))
}
Icon(Icons.Default.Add, modifier = Modifier.clip(RoundedCornerShape(8.dp)))
Icon(Icons.Default.AccountBox, modifier = Modifier.clip(RoundedCornerShape(8.dp)))
Icon(Icons.Default.ExitToApp, modifier = Modifier.clip(RoundedCornerShape(8.dp)))
Icon(Icons.Default.ArrowBack, modifier = Modifier.clip(RoundedCornerShape(8.dp)))
}
}
@Composable
fun MyBottomBar() {
BottomAppBar() {
Row(){
Icon(Icons.Default.Person, modifier = Modifier.clip(RoundedCornerShape(8.dp)).weight(0.25f))
Icon(Icons.Default.Call, modifier = Modifier.clip(RoundedCornerShape(8.dp)).weight(0.25f))
Icon(Icons.Default.Send, modifier = Modifier.clip(RoundedCornerShape(8.dp)).weight(0.25f))
Icon(Icons.Default.Refresh, modifier = Modifier.clip(RoundedCornerShape(8.dp)).weight(0.25f))
}
}
}
@Composable
fun MyFab(fragment: Fragment) {
FloatingActionButton(
icon = { Icon(Icons.Default.Add) },
onClick = {
findNavController(fragment).navigate(HomeFragmentDirections.actionHomeFragmentToAddCostActivity())
launchActivity = true
},
modifier = Modifier.padding(8.dp)
)
}
Yann Badoual
09/25/2020, 7:45 AMYann Badoual
09/25/2020, 7:47 AMTopAppBar(modifier = Modifier.padding(8.dp, 16.dp, 8.dp, 8.dp))
You're settings padding arround the toolbar, which is applied first in this case, before the background etc (the order of modifiers matters).
It's like if you were setting margins with views hereYann Badoual
09/25/2020, 7:49 AMfillMaxSize
modifier in LazyColumnFor.
By the way, scaffold is passing a PaddingValues
for your content that you should forward to your lazycolumnMehdi Haghgoo
09/25/2020, 7:56 AMYann Badoual
09/25/2020, 8:02 AMYann Badoual
09/25/2020, 8:10 AMComposeView
from onCreateView
without using a LienarLayout?Mehdi Haghgoo
09/25/2020, 12:46 PMIan Arbuckle
09/25/2020, 1:31 PMScaffold(
topBar = {
// top bar content
},
bodyContent = { innerPadding -> LazyColumnFor(
contentPadding = innerPadding,
items = data,
modifier = Modifier
.background(Color.Green),
itemContent = {item->
Text(item, modifier = Modifier.padding(8.dp))
}
)
},
bottomBar = {
// bottom bar content
}
)
Ian Arbuckle
09/25/2020, 1:32 PMMehdi Haghgoo
09/25/2020, 1:59 PMIan Arbuckle
09/25/2020, 2:02 PMYann Badoual
09/25/2020, 2:02 PMModifier.padding
instead. Using it as contentPadding can lead to weird effectsIan Arbuckle
09/25/2020, 2:08 PMYann Badoual
09/25/2020, 2:10 PM