官术网_书友最值得收藏!

Building the ShoppingList screen

Our first screen will contain a list of the items we need to buy, so it will contain one list item per item we need to buy, including a button to mark that item as already bought. Moreover, we need a button to navigate to the AddProduct screen, which will allow us to add products to our list. Finally, we will add a button to clear the list of products, in case we want to start a new shopping list:

Let's start by creating ShoppingList.js inside the screens folder and importing all the UI components we will need from native-base and react-native (we will use an alert popup to warn the user before clearing all items). The main UI components we will be using are Fab (the blue and red round buttons), List, ListItem, CheckBox, Text, and Icon. To support our layout, we will be using Body, Container, Content, and Right, which are layout containers for the rest of our components.

Having all these components, we can create a simple version of our ShoppingList component:

/*** ShoppingList.js ***/

import React from 'react';
import { Alert } from 'react-native';
import {
Body,
Container,
Content,
Right,
Text,
CheckBox,
List,
ListItem,
Fab,
Icon
} from 'native-base';

export default class ShoppingList extends React.Component {
static navigationOptions = {
title: 'My Groceries List'
};
/*** Render ***/
render() {
return (
<Container>
<Content>
<List>
<ListItem>
<Body>
<Text>'Name of the product'</Text>
</Body>
<Right>
<CheckBox
checked={false}
/>
</Right>
</ListItem>
</List>
</Content>
<Fab
style={{ backgroundColor: '#5067FF' }}
position="bottomRight"
>
<Icon name="add" />
</Fab>
<Fab
style={{ backgroundColor: 'red' }}
position="bottomLeft"
>
<Icon ios="ios-remove" android="md-remove" />
</Fab>
</Container>
);
}
}

This is just a dumb component statically displaying the components we will be using on this screen. Some things to note:

  • navigationOptions is a static attribute which will be used by <Navigator> to configure how the navigation would behave. In our case, we want to display My Groceries List as the title for this screen.
  • For native-base to do its magic, we need to use <Container> and <Content> to properly form the layout.
  • Fab buttons are placed outside <Content>, so they can float over the left and right-bottom corners.
  • Each ListItem contains a <Body> (main text) and a <Right> (icons aligned to the right).

Since we enabled Live Reload in our first steps, we should see the app reloading after saving our newly created file. All the UI elements are now in place, but they are not functional since we didn't add any state. This should be our next step.

主站蜘蛛池模板: 龙陵县| 兴安盟| 扶沟县| 博爱县| 汉寿县| 宁南县| 沾益县| 永丰县| 临高县| 兰西县| 东城区| 五常市| 修水县| 图们市| 紫阳县| 东兰县| 成安县| 府谷县| 原平市| 太仆寺旗| 洛扎县| 绥芬河市| 洛阳市| 泸西县| 太仆寺旗| 牡丹江市| 绥中县| 会同县| 仁布县| 建平县| 鄂伦春自治旗| 曲周县| 子长县| 革吉县| 景宁| 伊金霍洛旗| 郎溪县| 侯马市| 阿鲁科尔沁旗| 曲周县| 贵州省|